Export (0) Print
Expand All

OdbcConnection.Open Method

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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

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

Implements

IDbConnection.Open

Exceptions

Exception Type Condition
InvalidOperationException The connection is already open.
OdbcException A connection-level error occurred while opening the connection.

Remarks

The OdbcConnection draws an open connection from the connection pool if one is available. Otherwise, it establishes a new connection to the data source.

Note   If the OdbcConnection 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#, C++] The following example creates an OdbcConnection, opens it, displays some of its properties, then closes the connection.

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

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

[C++] 
public:
    void CreateOdbcConnection(String* myConnString)
    {
        OdbcConnection* myConnection = new OdbcConnection(myConnString);
        myConnection->Open();
        MessageBox::Show(String::Concat(S"ServerVersion: ", myConnection->ServerVersion,
            S"\nState: ", __box(myConnection->State)->ToString()));
        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

OdbcConnection Class | OdbcConnection Members | System.Data.Odbc Namespace | Close

Show:
© 2015 Microsoft