Check if SQL Connection is Open or Closed

C#ado.netSqlconnection

C# Problem Overview


How do you check if it is open or closed I was using

 if (SQLOperator.SQLCONNECTION.State.Equals("Open"))

however, even the State is 'Open' it fails on this check.

C# Solutions


Solution 1 - C#

You should be using SqlConnection.State

e.g,

using System.Data;

if (myConnection != null && myConnection.State == ConnectionState.Closed)
{
   // do something
   // ...
}

Solution 2 - C#

Here is what I'm using:

if (mySQLConnection.State != ConnectionState.Open)
{
    mySQLConnection.Close();
    mySQLConnection.Open();
}

The reason I'm not simply using:

if (mySQLConnection.State == ConnectionState.Closed)
{
    mySQLConnection.Open();
}

Is because the ConnectionState can also be:

Broken, Connnecting, Executing, Fetching

In addition to

Open, Closed

Additionally Microsoft states that Closing, and then Re-opening the connection "will refresh the value of State." See here http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspx

Solution 3 - C#

The .NET documentation says: State Property: A bitwise combination of the ConnectionState values

So I think you should check

!myConnection.State.HasFlag(ConnectionState.Open)

instead of

myConnection.State != ConnectionState.Open

because State can have multiple flags.

Solution 4 - C#

Check if a MySQL connection is open

ConnectionState state = connection.State;
if (state == ConnectionState.Open)
{
    return true;
}
else
{
    connection.Open();
    return true;
}

Solution 5 - C#

you can also use this

if (SQLCON.State == ConnectionState.Closed)
{
     SQLCON.Open();
}

Solution 6 - C#

This code is a little more defensive, before opening a connection, check state. If connection state is Broken then we should try to close it. Broken means that the connection was previously opened and not functioning correctly. The second condition determines that connection state must be closed before attempting to open it again so the code can be called repeatedly.

// Defensive database opening logic.

if (_databaseConnection.State == ConnectionState.Broken) {
    _databaseConnection.Close();
}

if (_databaseConnection.State == ConnectionState.Closed) {
    _databaseConnection.Open();
}

Solution 7 - C#

To check the database connection state you can just simple do the following

if(con.State == ConnectionState.Open){}

Solution 8 - C#

To check OleDbConnection State use this:

if (oconn.State == ConnectionState.Open)
{
	oconn.Close();
}

State return the ConnectionState

public override ConnectionState State { get; }

Here are the other ConnectionState enum

public enum ConnectionState
    {
        //
        // Summary:
        //     The connection is closed.
        Closed = 0,
        //
        // Summary:
        //     The connection is open.
        Open = 1,
        //
        // Summary:
        //     The connection object is connecting to the data source. (This value is reserved
        //     for future versions of the product.)
        Connecting = 2,
        //
        // Summary:
        //     The connection object is executing a command. (This value is reserved for future
        //     versions of the product.)
        Executing = 4,
        //
        // Summary:
        //     The connection object is retrieving data. (This value is reserved for future
        //     versions of the product.)
        Fetching = 8,
        //
        // Summary:
        //     The connection to the data source is broken. This can occur only after the connection
        //     has been opened. A connection in this state may be closed and then re-opened.
        //     (This value is reserved for future versions of the product.)
        Broken = 16
    }

Solution 9 - C#

I use the following manner sqlconnection.state

if(conexion.state != connectionState.open())
   conexion.open();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser222427View Question on Stackoverflow
Solution 1 - C#user195488View Answer on Stackoverflow
Solution 2 - C#therealjumboView Answer on Stackoverflow
Solution 3 - C#csname1910View Answer on Stackoverflow
Solution 4 - C#Louie BacajView Answer on Stackoverflow
Solution 5 - C#bitu pascalView Answer on Stackoverflow
Solution 6 - C#GBGOLCView Answer on Stackoverflow
Solution 7 - C#ManiView Answer on Stackoverflow
Solution 8 - C#Manjunath BilwarView Answer on Stackoverflow
Solution 9 - C#williamView Answer on Stackoverflow