如何刷新 SqlConnection 的状态?

发布于 2024-11-17 20:17:04 字数 352 浏览 3 评论 0原文

我有这个代码:

if (  con.SqlConnection.State == System.Data.ConnectionState.Broken ||
      con.SqlConnection.State == System.Data.ConnectionState.Closed      
){
    con.SqlConnection.Open();
}

我失去了与网络的连接。当我来到我的 if 时,我的 SqlConnection.State 仍然显示打开。

如何刷新 SqlConnection 的状态

I have this code:

if (  con.SqlConnection.State == System.Data.ConnectionState.Broken ||
      con.SqlConnection.State == System.Data.ConnectionState.Closed      
){
    con.SqlConnection.Open();
}

I have lost connection to the network. When i come to my if, then my SqlConnection.State still says open.

How do i refresh my state of my SqlConnection

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∝单色的世界 2024-11-24 20:17:04

这是您应该依赖异常的少数情况之一。

Microsoft 实际上建议您对异常做出反应,而不是检查该状态变量。想象一下这样的情况:您检查变量,然后一纳秒后连接断开。

我相信会抛出的两个是 InvalidOperationExceptionSqlException

This is one of the few cases where you should rely on exceptions.

Microsoft actually recommends that you react to an exception versus check that state variable. Think of the case where you check the variable, and then a nanosecond later the connection goes down.

I believe the two that would get thrown are InvalidOperationException or SqlException.

怎会甘心 2024-11-24 20:17:04

在 TCP 连接上,只有一种有保证的方法可以了解连接是否仍处于活动状态,即向打开的套接字发送新数据包。

因此,只要我们假设您通过 TCP 连接连接到 Sql Server,则该规则也适用。因此,如果出现异常,您需要发送数据并等待。

    public bool IsConnected()
    {
        if (db == null) //db here is an Entity
            return false;

        //in my case Connection is an sqlconnection object so you can 
        //apply the same to your connection
        if (db.Database.Connection.State == ConnectionState.Closed)
            return false;

        try
        {
            var cmd = db.Database.Connection.CreateCommand();
            cmd.CommandText = "SELECT 0";
            cmd.ExecuteNonQuery();
        }
        catch
        {
            return false;
        }

        return true;
    }

On TCP connections, there is only one guaranteed way to understand if the connection is still alive or not, it is sending a new packet to the open socket.

So as long as we assume you're connected to Sql Server through a TCP connection, this rule applies to it also. So you need to do send data and wait if an exception is raised.

    public bool IsConnected()
    {
        if (db == null) //db here is an Entity
            return false;

        //in my case Connection is an sqlconnection object so you can 
        //apply the same to your connection
        if (db.Database.Connection.State == ConnectionState.Closed)
            return false;

        try
        {
            var cmd = db.Database.Connection.CreateCommand();
            cmd.CommandText = "SELECT 0";
            cmd.ExecuteNonQuery();
        }
        catch
        {
            return false;
        }

        return true;
    }
烟若柳尘 2024-11-24 20:17:04

您始终可以使用 SqlConnection.StateChange 事件

You can always monitor the state of your SqlConnection by using the SqlConnection.StateChange Event

金橙橙 2024-11-24 20:17:04

懒惰的方法:“SELECT 0”。如果命令失败,连接状态将更新为“已关闭”

if (DBConn.State == ConnectionState.Open)
{
   try
   {
      using(SqlCommand cmd = new SqlCommand(DBConn))
      {
         cmd.CommandText = "SELECT 0";
         cmd.ExecuteNonQuery();
      }
   }
   Catch
   {
      DBConn.Close(); \\Declare the connection dead.
   }
}

The lazy way: "SELECT 0". If the command fails, the connection state is updated to "Closed"

if (DBConn.State == ConnectionState.Open)
{
   try
   {
      using(SqlCommand cmd = new SqlCommand(DBConn))
      {
         cmd.CommandText = "SELECT 0";
         cmd.ExecuteNonQuery();
      }
   }
   Catch
   {
      DBConn.Close(); \\Declare the connection dead.
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文