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

问题描述 投票:0回答:11

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

 if (SQLOperator.SQLCONNECTION.State.Equals("Open"))

但是,即使状态为“开放”,此检查也会失败。

c# ado.net sqlconnection
11个回答
212
投票

您应该使用SqlConnection.State

例如,

using System.Data;

if (myConnection != null && myConnection.State == ConnectionState.Closed)
{
   // do something
   // ...
}

57
投票

这是我正在使用的:

if (mySQLConnection.State != ConnectionState.Open)
{
    mySQLConnection.Close();
    mySQLConnection.Open();
}

我不简单使用的原因:

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

是因为ConnectionState还可以是:

Broken, Connnecting, Executing, Fetching

除了

Open, Closed

此外,微软还指出,关闭然后重新打开连接“将刷新 State 的值”。 请参阅此处 http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspx


30
投票

.NET 文档说:State 属性:ConnectionState 值的按位组合

所以我认为你应该检查一下

!myConnection.State.HasFlag(ConnectionState.Open)

而不是

myConnection.State != ConnectionState.Open

因为 State 可以有多个标志。


13
投票

检查 MySQL 连接是否打开

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

8
投票

你也可以用这个

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

7
投票

这段代码更具防御性,在打开连接之前,检查状态。 如果连接状态为“Broken”,那么我们应该尝试关闭它。断开意味着连接之前已打开并且无法正常工作。 第二个条件确定在尝试再次打开连接状态之前必须关闭连接状态,以便可以重复调用代码。

// Defensive database opening logic.

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

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

4
投票

要检查数据库连接状态,您可以简单地执行以下操作

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

4
投票

要检查 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
    }

0
投票

检查连接状态的唯一有效方法是执行一个简单的查询(例如,从 SqlServer 中的 Table 中选择 top 1 yourfield 或者 从 mysql 中的 Table limit 1 选择 yourfield)。 我认为这是测试连接状态的唯一有效系统。 如果您不相信我,请在调试中尝试以下操作: 在 if 条件后面放置断点

if (oconn.State == ConnectionState.Open){
        oconn.Close(); //breakpoint here
    }

使程序一直运行到断点处。 现在,拔掉 LAN 电缆或关闭无线或停止 sqlserver 或 mysql 服务,将编译器返回到之前的语句并重新测试连接。它仍然会开放。 在您对数据库执行任何操作之前,连接将始终显示为打开状态。


0
投票

is_connected();

如果该值为“false”,则返回一个布尔值,但仍建立连接。如果值为“true”,则连接已关闭。


-5
投票

我使用以下方式

sqlconnection.state

if(conexion.state != connectionState.open())
   conexion.open();
© www.soinside.com 2019 - 2024. All rights reserved.