System.Data.ConnectionState Enum
The ConnectionState enumeration specifies the state of a connection to a data source.
Namespace
System.Data
Declaration
public enum ConnectionState
{
Broken,
Closed,
Connecting,
Executing,
Fetching,
Open
}
Members
| Member | Value | Description |
|---|---|---|
Broken | 1 | The connection to the data source is broken. This can occur only after the connection has been opened. This state indicates that the connection is no longer usable. |
Closed | 0 | The connection is closed. |
Connecting | 2 | The connection object is connecting to the data source. |
Executing | 4 | The connection object is executing a command. |
Fetching | 8 | The connection object is retrieving data. |
Open | 1 | The connection is open. |
Remarks
The ConnectionState values are designed to be combined as a bit field. For example, a connection might be both Executing and Fetching simultaneously.
Example
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyDB;Integrated Security=True"))
{
conn.Open();
Console.WriteLine($"Connection state: {conn.State}");
}
}
}
See also
Related topics