C# 检查SQL连接是否打开或关闭

C# 检查SQL连接是否打开或关闭,c#,ado.net,sqlconnection,C#,Ado.net,Sqlconnection,如何检查我使用的是打开还是关闭 if (SQLOperator.SQLCONNECTION.State.Equals("Open")) 但是,即使状态为“打开”,此检查也会失败。您应该使用 e、 g, 我使用以下方式sqlconnection.state if(conexion.state != connectionState.open()) conexion.open(); public override ConnectionState State { get; } 你也可以用这

如何检查我使用的是打开还是关闭

 if (SQLOperator.SQLCONNECTION.State.Equals("Open"))
但是,即使状态为“打开”,此检查也会失败。

您应该使用

e、 g,


我使用以下方式
sqlconnection.state

if(conexion.state != connectionState.open())
   conexion.open();
public override ConnectionState State { get; }
你也可以用这个

if (SQLCON.State == ConnectionState.Closed)
{
     SQLCON.Open();
}

检查MySQL连接是否打开

ConnectionState state = connection.State;
if (state == ConnectionState.Open)
{
    return true;
}
else
{
    connection.Open();
    return true;
}

以下是我正在使用的:

if (mySQLConnection.State != ConnectionState.Open)
{
    mySQLConnection.Close();
    mySQLConnection.Open();
}
我不简单使用的原因是:

if (mySQLConnection.State == ConnectionState.Closed)
{
    mySQLConnection.Open();
}
是因为ConnectionState也可以是:

Broken, Connnecting, Executing, Fetching
除了

Open, Closed
此外,Microsoft声明,关闭然后重新打开连接“将刷新状态值”
请参见此处

要检查数据库连接状态,只需执行以下简单操作

if(con.State == ConnectionState.Open){}

NET文档说明:状态属性:ConnectionState值的按位组合

所以我想你应该检查一下

!myConnection.State.HasFlag(ConnectionState.Open)
而不是

myConnection.State != ConnectionState.Open

因为状态可以有多个标志。

此代码更具防御性,在打开连接之前,请检查状态。 如果连接状态被破坏,那么我们应该尝试关闭它。断开表示该连接以前已打开,且功能不正常。 第二个条件确定在再次尝试打开连接状态之前必须关闭连接状态,以便可以重复调用代码

// Defensive database opening logic.

if (_databaseConnection.State == ConnectionState.Broken) {
    _databaseConnection.Close();
}

if (_databaseConnection.State == ConnectionState.Closed) {
    _databaseConnection.Open();
}

要检查OLEDB连接状态,请使用以下命令:

if (oconn.State == ConnectionState.Open)
{
    oconn.Close();
}
状态
返回
连接状态

if(conexion.state != connectionState.open())
   conexion.open();
public override ConnectionState State { get; }
下面是另一个
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
    }

+1完全正确-使用
SqlConnectionState
enum作为枚举,不要将其转换为字符串…..应该使用System.Data添加
在答案中,IMHO。我忘记了这个名称空间(使用System.Data.SqlClient时使用了
),在添加它之前,我无法理解如何将
ConnectionState
作为关键字。希望这对其他人有帮助。如果服务器(或本地计算机和服务器之间的某个东西)关闭了连接,这能起作用吗?如果(myConnection==null | | | myConnection.State==ConnectionState.closed){//连接已关闭}或者{//连接已以某种方式打开}
,说
会更好吗?这样,如果连接为空,它也将“关闭”。
connectionState.open()
不存在;您的意思是使用System.Data打开连接状态
对于不知道或不知道它为什么不工作的任何人,您应该测试
mySQLConnection.State!=ConnectionState.Open&&mySQLConnection.State!=ConnectionState.Connecting
为避免使用慢速连接重置,是吗?@caligari虽然为true,但不保证DbConnection存在,因此如果编程到抽象DbConnection,请小心。我个人认为这是通过避免重置来解决的问题。我可能可以在有状态的应用程序层中看到此代码片段的用例,但在Web上看不到?John,这正是此代码的用例。运行服务器应用程序的代码,该应用程序可以为页面提供服务,连接到另一个REST服务器。我看不到任何情况下我会在web应用程序的客户端代码中连接到服务器数据库。这有一个巨大的警告:1)随着本地存储现在成为一件事,使用本地存储的web应用程序迟早(已经)会使用该存储中的数据库。如果不是现在,他们很快就会。这里的另一件事是,我的代码可能没有正确地通用化,无法在大型应用程序中使用。我的主要关注点是嵌入式编程,所以我还在服务器端学习。我想知道为什么这是带有标志的枚举。由于此枚举的Close项的值为零,因此State.HasFlag(ConnectionState.Close)将为任何值返回true。对我来说,这意味着我应该检查为“!=Close”打开一个问题注:我觉得有必要提到Ivan的链接提到你不应该使用它作为标志。看看这个具体的答案:返回始终为真的目的是什么?此时,使方法无效。仅检查连接是否未打开,如果未打开,请将其打开。而且。。。为什么写2次
返回true?将它放在方法的末尾,在
if
/
else
之外!如果出现网络问题,这些将给出错误的答案。您不能确定open确实会打开。@user613326实际上,它不会打开。示例代码中没有错误处理,因此连接时出现的任何问题都会引发异常,让您自行处理异常。因此,返回的值是正确的。只是稍微有点防御性。从审阅队列:我可以请求您在您的答案周围添加一些上下文吗。只有代码的答案很难理解。如果你能在你的文章中添加更多的信息,这将有助于询问者和未来的读者。另见。