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

MemberValueDescription
Broken1The 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.
Closed0The connection is closed.
Connecting2The connection object is connecting to the data source.
Executing4The connection object is executing a command.
Fetching8The connection object is retrieving data.
Open1The 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