Export (0) Print
Expand All

OleDbConnection.Close Method

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

[Visual Basic]
Public Overridable Sub Close() Implements IDbConnection.Close
[C#]
public virtual void Close();
[C++]
public: virtual 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. No exception is generated.

CAUTION   Do not call Close or Dispose on a Connection, a DataReader, or any other managed object in the Finalize method of your class. In a finalizer, you should only release unmanaged resources that your class owns directly. If your class does not own any unmanaged resources, do not include a Finalize method in your class definition. For more information, see Programming for Garbage Collection.

Example

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

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

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

[C++] 
public:
 void CreateOleDbConnection(String* myConnString) 
 {
    OleDbConnection* myConnection = new OleDbConnection(myConnString);
    myConnection->Open();
    MessageBox::Show(String::Format( S"ServerVersion: {0}\nState: {1}", myConnection->ServerVersion, __box(myConnection->State)));
    myConnection->Close();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OleDbConnection Class | OleDbConnection Members | System.Data.OleDb Namespace | Open

Show:
© 2014 Microsoft