如何检查它是打开还是关闭,我一直在使用它。
if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
然而,即使状态是“打开”的,它仍无法通过此检查。
如何检查它是打开还是关闭,我一直在使用它。
if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
然而,即使状态是“打开”的,它仍无法通过此检查。
你应该使用SqlConnection.State
。e.g,
using System.Data;
if (myConnection != null && myConnection.State == ConnectionState.Closed)
{
// do something
// ...
}
SqlConnectionState
枚举作为枚举,而不是将其转换为字符串。 - marc_susing System.Data;
。我忘记了这个命名空间(只写了 using System.Data.SqlClient
),所以在没有添加这个命名空间之前,我无法将 ConnectionState
作为关键字来使用。希望这可以帮助到其他人。 - vapcguyif (myConnection == null || myConnection.State == ConnectionState.Closed) { //连接已关闭 } else { //连接以某种方式打开 }
?这样,如果连接为null,它也是“关闭的”。 - Arvo Bowen这是我正在使用的:
if (mySQLConnection.State != ConnectionState.Open)
{
mySQLConnection.Close();
mySQLConnection.Open();
}
我不直接使用的原因是:
if (mySQLConnection.State == ConnectionState.Closed)
{
mySQLConnection.Open();
}
是因为 ConnectionState 也可以是以下情况:
Broken, Connnecting, Executing, Fetching
除了...之外
Open, Closed
此外,微软表示关闭并重新打开连接“将刷新状态的值。” 请参见此处 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspxmySQLConnection.State != ConnectionState.Open && mySQLConnection.State != ConnectionState.Connecting
来避免在慢速连接中出现重置,不是吗? - caligari.NET文档称:State属性:ConnectionState值的按位组合
因此我认为您应该进行检查。
!myConnection.State.HasFlag(ConnectionState.Open)
而不是
myConnection.State != ConnectionState.Open
因为不同的州可能拥有多面旗帜。
检查 MySQL 连接是否打开
ConnectionState state = connection.State;
if (state == ConnectionState.Open)
{
return true;
}
else
{
connection.Open();
return true;
}
return true;
?将其放在方法末尾,if
/else
之外! - Massimiliano Krausif (SQLCON.State == ConnectionState.Closed)
{
SQLCON.Open();
}
using System.Data;
。 - Paul C这段代码的防御性更强,在打开连接之前,先检查状态。如果连接状态为Broken,则应尝试关闭它。Broken表示连接先前已打开但未正常运行。第二个条件确定在尝试重新打开连接之前必须关闭连接状态,以便可以重复调用该代码。
// Defensive database opening logic.
if (_databaseConnection.State == ConnectionState.Broken) {
_databaseConnection.Close();
}
if (_databaseConnection.State == ConnectionState.Closed) {
_databaseConnection.Open();
}
如果要检查OleDbConnection状态,请使用以下方法:
if (oconn.State == ConnectionState.Open)
{
oconn.Close();
}
State
返回 ConnectionState
public override ConnectionState State { get; }
ConnectionState
枚举值: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
}
要检查数据库连接状态,只需执行以下简单操作:
if(con.State == ConnectionState.Open){}
if (oconn.State == ConnectionState.Open){
oconn.Close(); //breakpoint here
}
is_connected();
false
,则连接仍然建立。如果值为true
,则连接已关闭。
SqlConnection.State
只是告诉你连接是否在过去被打开过,它并不能告诉你服务器是否真正可用。而且这也不是必要的,你只需要创建一个新的连接,在使用完后用using
进行释放即可。 - Charlieface