您如何检查我使用的是打开还是关闭的
if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
但是,即使状态为“打开”,它也无法通过此检查。
您如何检查我使用的是打开还是关闭的
if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
但是,即使状态为“打开”,它也无法通过此检查。
Answers:
您应该使用SqlConnection.State
例如,
using System.Data;
if (myConnection != null && myConnection.State == ConnectionState.Closed)
{
// do something
// ...
}
using System.Data;
答案,恕我直言。我忘记了这个名称空间(已using System.Data.SqlClient
),并且在ConnectionState
添加它之前不知道如何获得关键字。希望这对某人有帮助。
if (myConnection == null || myConnection.State == ConnectionState.Closed) { //Connection is closed } else { //Connection is open in some way }
吗?这样,如果连接为空,它也会被“关闭”。
这是我正在使用的:
if (mySQLConnection.State != ConnectionState.Open)
{
mySQLConnection.Close();
mySQLConnection.Open();
}
我不简单使用的原因:
if (mySQLConnection.State == ConnectionState.Closed)
{
mySQLConnection.Open();
}
是因为ConnectionState也可以是:
Broken, Connnecting, Executing, Fetching
此外
Open, Closed
此外,Microsoft声明关闭并重新打开连接“将刷新State的值”。参见此处http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspx
mySQLConnection.State != ConnectionState.Open && mySQLConnection.State != ConnectionState.Connecting
可以避免由于连接速度慢而重置,不是吗?
.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;
}
return true;
?把它放在方法的结束,外界if
/ else
!
你也可以用这个
if (SQLCON.State == ConnectionState.Closed)
{
SQLCON.Open();
}
using System.Data;
对于任何不知道或不知道为什么它不起作用的人
这段代码更具防御性,在打开连接之前,请检查状态。如果连接状态为“断开”,则应尝试将其关闭。断开意味着该连接先前已打开并且无法正常运行。第二个条件确定必须再次关闭连接状态才能尝试再次打开它,以便可以重复调用该代码。
// 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
}
SqlConnectionState
枚举作为枚举,而不是将其转换为字符串..