This documentation is archived and is not being maintained.

SqlConnection.State Property

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:

  • From Closed to Open, using the Open method of the connection object.
  • From Open to Closed, using either the Close method or the Dispose method of the connection object.

Example

[Visual Basic, C#, C++] The following example creates a SqlConnection and displays some of its read-only properties.

[Visual Basic] 
Public Sub CreateSqlConnection()
    Dim myConnectString As String = _
       "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
    Dim myConnection As New SqlConnection(myConnectString)
    myConnection.Open()
    MessageBox.Show("ServerVersion: " + myConnection.ServerVersion _
       + ControlChars.Cr + "State: " + myConnection.State.ToString())
    myConnection.Close()
End Sub

[C#] 
public void CreateSqlConnection() 
{
   string myConnectString =
      "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
   SqlConnection myConnection = new SqlConnection(myConnectString);
   myConnection.Open();
   MessageBox.Show("ServerVersion: " + myConnection.ServerVersion
      + "\nState: " + myConnection.State.ToString());
   myConnection.Close();
}

[C++] 
public:
 void CreateSqlConnection() 
 {
    String* myConnectString =
       S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlConnection* myConnection = new SqlConnection(myConnectString);
    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, .NET Compact Framework

See Also

SqlConnection Class | SqlConnection Members | System.Data.SqlClient Namespace

Show: