DB2 10.5 for Linux, UNIX, and Windows

DB2Connection.Close Method

Closes the connection to the database. This is the preferred method of closing any open connection.

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

Syntax

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

Remarks

The Close method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled. If Close is called while handling a StateChange event, no additional StateChange events are fired.

An application can call Close more than one time without generating an exception.

Note: When you use the IBM® Data Server Provider for .NET, you do not need to enable connection pooling because it is on by default.

Example

[Visual Basic, C#] The following example creates a DB2®Connection , 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("State: " + myConnection.State.ToString())
     myConnection.Close()
 End Sub

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

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 prior to the migration will be recycled on the call to Close.