IDbConnection.State Property

Definition

Gets the current state of the connection.

C#
public System.Data.ConnectionState State { get; }

Property Value

One of the ConnectionState values.

Examples

The following example creates an instance of a derived class, OleDbConnection, sets its ConnectionString, and displays its State.

C#
public void CreateOleDbConnection(){
   OleDbConnection connection = new OleDbConnection();
   connection.ConnectionString =
       "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb";
   Console.WriteLine("Connection State: " + connection.State.ToString());
}

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.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1