Scalable Database Server, HiRDB Version 8 UAP Development Guide

[Contents][Index][Back][Next]

15.8.1 Connecting to the database

The following example connects to HiRDB and then disconnects from HiRDB:

 
using System;
using Hitachi.HiRDB;
 
namespace test_C
{
    class Sample
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                // Create a Connection object
                HiRDBConnection cn = new HiRDBConnection("dsn=pc;");  ...1
 
                // Connect to the database
                cn.Open();  .............................................2
 
                // Disconnect from the database
                cn.Close();  ............................................3
            }
            catch (HiRDBException ex)
            {
                Console.WriteLine(ex);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
            }  ..........................................................4
        }
    }
}
 

Explanation
  1. First, create a HiRDBConnection object. This object manages all communications with HiRDB. Because Disconnect is called from within the HiRDBConnection: Dispose method, when this object disappears, the database is automatically disconnected.
    For this method, you must specify one string-type argument. The character string to be specified is called a connection character string. This is the same type of connection character string as those used for Connection in ADO and ADO.NET. For details about the character strings that can be specified, see 15.5.3(2)(a) ConnectionString.
  2. To connect to the database, use the Open method.
  3. To disconnect from the database, use the Close method. Using the Close method while a connection is not established does not result in an exception.
  4. An exception occurs if the server is not running, communication is disabled, the SQL statement is invalid, or in similar cases. Basically, a block using HiRDB.NET Data Provider detects exceptions by try through catch, and then displays an exception message.
    In the case of an overall HiRDB error, System.Exception occurs, and in the case of a HiRDB.NET Data Provider-specific error, HiRDBException occurs. Make sure that System.Exception is not abbreviated as Exception.
    A HiRDB Client Library or HiRDB.NET Data Provider-specific error code is stored in the ErrorCode property of the exception object that is created by HiRDB.NET Data Provider.
    A 3-digit (-XXX) or 4-digit (-XXXX) error code indicates KFPA1XXXX and a 5-digit error code (-24XXX) indicates KFPZ24XXX.