Check if SQL Connection is Open or Closed

前端 未结 9 1737
春和景丽
春和景丽 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:40

    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();
    }
    

提交回复
热议问题