DB2 10.5 for Linux, UNIX, and Windows

DB2Connection.Open Method

Opens a connection to a database with the property settings specified by the ConnectionString.

Namespace:
IBM.Data.DB2
Assembly:
IBM.Data.DB2 (in IBM.Data.DB2.dll)

Syntax

[Visual Basic]
NotOverridable Public Sub Open() Implements IDbConnection.Open
[C#]
public void Open();
[C++]
public: __sealed void Open();
[JScript]
public function Open();
Implements:
IDbConnection.Open

Exceptions

Exception type Condition
InvalidOperationException The connection is already open.
DB2®Exception A connection-level error occurred while opening the connection.

Remarks

The DB2Connection.Open Method supports the System.EnterpriseServices namespace and the System.Transactions namespace.

The DB2Connection draws an open connection from the connection pool if connection pooling is on and a connection is available. Otherwise, it establishes a new connection to the database.

Note: If the DB2Connection goes out of scope, the connection it represents does not close automatically. Therefore, you must explicitly close the connection by calling Close or Dispose.

Example

[Visual Basic, C#] The following example creates a DB2Connection , opens it, displays some of its properties, then closes the connection.

[Visual Basic]
Public Sub CreateDB2Connection(myConnString As String)
    Dim myConnection As New DB2Connection(myConnString)
    myConnection.Open()
    MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
       + ControlChars.Cr + "State: " + myConnection.State.ToString())
    myConnection.Close()
End Sub

[C#]
public void CreateDB2Connection(string myConnString)
{
   DB2Connection myConnection = new DB2Connection(myConnString);
   myConnection.Open();
   MessageBox.Show("ServerVersion: " + myConnection.ServerVersion
      + "\nState: " + myConnection.State.ToString());
   myConnection.Close();
}

In DB2 for z/OS® Version 10, if the server has migrated to a different mode during the application execution and if a call to Open has returned a connection with migrated server info, then subsequent calls to Open will always return a connection with migrated server info. All connections established before the migration will be recycled on the call to Close.

The Chaos isolation level is valid only forIBM® DB2 for IBM i servers. If the IsolationLevel, either by DB2Connection.ConnectionString property or by use of the db2dsdriver.cfg file, is set to an equivalent level of the Chaos isolation level and the server connected to is not a DB2 for i, then a ArgumentException with a message "Not supported isolationlevel - Chaos" will be thrown and the connection will not be opened.