Check if SQL Connection is Open or Closed

前端 未结 9 1738
春和景丽
春和景丽 2020-12-08 01:47

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

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

however, even the State is \'Op

9条回答
  •  既然无缘
    2020-12-08 02:27

    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

提交回复
热议问题