Check if SQL Connection is Open or Closed

user222427 picture user222427 · Aug 4, 2011 · Viewed 289.1k times · Source

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.

Answer

user195488 picture user195488 · Aug 4, 2011

You should be using SqlConnection.State

e.g,

using System.Data;

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