Nonstop Database, HiRDB Version 9 UAP Development Guide
![[Contents]](FIGURE/CONTENT.GIF)
![[Index]](FIGURE/INDEX.GIF)
![[Back]](FIGURE/FRONT.GIF)
16.11.1 Connecting to the database
The following example connects to HiRDB and then disconnects from HiRDB:
- Example of Visual C# .NET code
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
}
}
}
|
- Example of Visual Basic.NET code
Imports System
Imports System.Data
Imports Hitachi.HiRDB
Module Module1
Sub Main()
Dim cn As HiRDBConnection
Dim cm As HiRDBCommand
Try
' Create a Connection object
cn = New HiRDBConnection("dsn=pc;") ........................1
' Connect to the database
cn.Open() ..................................................2
' Disconnect from the database
cn.Close() .................................................3
Catch ex As HiRDBException
Console.WriteLine(ex)
Catch ex As System.Exception
Console.WriteLine(ex)
End Try ........................................................4
End Sub
End Module
|
- Explanation
- 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 16.5.3(2)(a) ConnectionString.
- To connect to the database, use the Open method.
- 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.
- 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.
All Rights Reserved. Copyright (C) 2011, Hitachi, Ltd.