检查SQL连接是打开还是关闭

时间:2011-08-04 15:13:15

标签: c# ado.net sqlconnection

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

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

然而,即使国家是'开放',它也会在这次检查中失败。

9 个答案:

答案 0 :(得分:148)

您应该使用SqlConnection.State

e.g,

using System.Data;

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

答案 1 :(得分:47)

以下是我正在使用的内容:

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/en-us/library/system.data.sqlclient.sqlconnection.state(v=vs.110).aspx

答案 2 :(得分:19)

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

所以我认为你应该检查

!myConnection.State.HasFlag(ConnectionState.Open)

而不是

myConnection.State != ConnectionState.Open

因为State可以有多个标志。

答案 3 :(得分:8)

检查MySQL连接是否已打开

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

答案 4 :(得分:6)

您也可以使用此

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

答案 5 :(得分:4)

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

Cursor

答案 6 :(得分:3)

要检查数据库连接状态,您只需执行以下操作即可

public int computeAvailableContainers(Resource available, Resource required) {
// Only consider memory
return available.getMemory() / required.getMemory();
  }

答案 7 :(得分:1)

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

答案 8 :(得分:-4)

我使用以下方式sqlconnection.state

if(conexion.state != connectionState.open())
   conexion.open();