.NET Framework Class Library  

OdbcConnection.State Property

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

Gets the current state of the connection.

[Visual Basic]
Public Overridable ReadOnly Property State As ConnectionState  _
   Implements IDbConnection.State
[C#]
public virtual ConnectionState State {get;}
[C++]
public: __property virtual ConnectionState get_State();
[JScript]
public function get State() : ConnectionState;

Property Value

A bitwise combination of the ConnectionState values. The default is Closed.

Implements

IDbConnection.State

Remarks

The allowed state changes are:

Note   Calling the State property on an open connection increases application overhead because each such call results in a SQL_ATTR_CONNECTION_DEAD call to the underlying ODBC driver to determine if the connection is still valid.

Example

[Visual Basic, C#, C++] The following example creates an instance of a derived class, OdbcConnection, sets its ConnectionString, and displays its State.

[Visual Basic] 
Public Sub createOdbcConnection()
    Dim myConnection As New OdbcConnection()
    myConnection.ConnectionString = _
       "DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;"
    myConnection.Open()
    MessageBox.Show("Connection State: " + myConnection.State)
    myConnection.Close()
End Sub

[C#] 
public void createOdbcConnection()
{
   OdbcConnection myConnection = new OdbcConnection();
   myConnection.ConnectionString = "DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;";
   myConnection.Open();
   MessageBox.Show("Connection State: " + myConnection.State);
   myConnection.Close();
}

[C++] 
public:
    void createOdbcConnection()
    {
        OdbcConnection* myConnection = new OdbcConnection();
        myConnection->ConnectionString = S"DRIVER= {SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;";
        myConnection->Open();
        MessageBox::Show(String::Concat(S"Connection State: ", __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

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