SQL 연결이 열려 있는지 또는 닫혀 있는지 확인
내가 사용하고 있던 열려 있는지 닫혀 있는지 어떻게 확인합니까?
if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
그러나 State가 'Open'인 경우에도이 검사에서 실패합니다.
SqlConnection.State를 사용해야합니다.
예 :
using System.Data;
if (myConnection != null && myConnection.State == ConnectionState.Closed)
{
// do something
// ...
}
내가 사용하는 것은 다음과 같습니다.
if (mySQLConnection.State != ConnectionState.Open)
{
mySQLConnection.Close();
mySQLConnection.Open();
}
내가 단순히 사용하지 않는 이유 :
if (mySQLConnection.State == ConnectionState.Closed)
{
mySQLConnection.Open();
}
ConnectionState도 다음과 같을 수 있기 때문입니다.
Broken, Connnecting, Executing, Fetching
이외에
Open, Closed
또한 Microsoft는 연결을 닫았다가 다시 열면 "상태 값이 새로 고쳐집니다"라고 말합니다. 여기를 참조하십시오 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspx
.NET 문서에 따르면 상태 속성 : ConnectionState 값의 비트 조합
그래서 확인해야 할 것 같아요
!myConnection.State.HasFlag(ConnectionState.Open)
대신에
myConnection.State != ConnectionState.Open
State는 여러 플래그를 가질 수 있기 때문입니다.
MySQL 연결이 열려 있는지 확인
ConnectionState state = connection.State;
if (state == ConnectionState.Open)
{
return true;
}
else
{
connection.Open();
return true;
}
당신은 또한 이것을 사용할 수 있습니다
if (SQLCON.State == ConnectionState.Closed)
{
SQLCON.Open();
}
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();
}
To check the database connection state you can just simple do the following
if(con.State == ConnectionState.Open){}
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
}
I use the following manner sqlconnection.state
if(conexion.state != connectionState.open())
conexion.open();
참고URL : https://stackoverflow.com/questions/6943933/check-if-sql-connection-is-open-or-closed
'Programming' 카테고리의 다른 글
전역 기능과 장치 기능의 차이점 (0) | 2020.08.27 |
---|---|
Java에서 리플렉션을 통해 개인 필드에 액세스 할 수 있습니까? (0) | 2020.08.27 |
문자열이 여러 접두사 중 하나로 시작하는지 확인하는 방법은 무엇입니까? (0) | 2020.08.27 |
Django Rest Framework는 csrf를 제거합니다. (0) | 2020.08.27 |
Java 가비지 콜렉션 로그 메시지 (0) | 2020.08.27 |