"使用"关键字不会关闭打开的SQL连接 [英] "using" keyword doesn't close an open SQL connection

查看:234
本文介绍了"使用"关键字不会关闭打开的SQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这是一个很久很久以前把堆栈溢出后。
最终是否使用关闭打开的SQL连接



我有一个问题,但是。我发现,使用没有关闭所有的SQL 2012 Express Edition的连接以及SQL 2008开发版。



下面是我使用的代码。该代码将通过每个数据库,寻找指定然而,当它完成特定的表,并运行在服务器上的sp_who,所有的连接仍然存在。状态正在睡觉而CMD是等待命令,但是当你试图创建实例的数据库,模型不能锁定,因为你还是开到它的连接。这是一类错误?



 使用(SqlConnection的康恩=新的SqlConnection(数据源=+ ServerNameCombo.Text + 初始目录=主;坚持安全信息= TRUE;用户ID =+ UserNameEdit.Text +;密码=+ PasswordEdit.Text))使用
{
(DBS的SqlCommand =新的SqlCommand( 从sysdatabases中名,康涅狄格州))
{
conn.Open();使用(SqlDataReader的读卡器= dbs.ExecuteReader())
,而(reader.Read())
{
使用
{
(SqlConnection的dbconn =新的SqlConnection(数据源=+ ServerNameCombo.Text +;初始目录=+读卡器[名称]的ToString()+;坚持安全信息= TRUE;用户ID =+ UserNameEdit.Text +;密码=+ PasswordEdit.Text))
{使用
(的SqlCommand dbscmd =新的SqlCommand('+表名+,dbconn))
{$ b $,从系统对象,其中name =选择名为' b dbconn.Open();
如果(dbscmd.ExecuteScalar()!= NULL)
{
DBNames + =(DBNames =,!?:)+读卡器[名称]的ToString。 ();
}
}
}
}
}
}
}


解决方案

这是预期的行为;它关闭的管理连接的,这意味着它释放的底层连接的在连接池。这样可以使连接人工打开,使得对于相同的连接字符串和身份下一管理连接可以使用的现有的连接(设置复位位中的TDS管道),以避免连接自旋向上延迟。



如果你不希望这样的:在连接字符串中禁用连接池(池=假)。


I'm referring to a post that was put on Stack Overflow a long, long time ago. Does End Using close an open SQL Connection

I have a problem however. I found that using does not close the connection at all on SQL 2012 Express edition as well as SQL 2008 Developer Edition.

Here is the code that I've used. The code will go through each Database and look for a specific table specified, however, when it is done, and you run an sp_who on the server, all the connections are still there. The status is sleeping and the cmd is "AWAITING COMMAND" but when you try to create a database for instance, model cannot be locked, because you still have a connection open to it. Is this a bug in the class?

using (SqlConnection conn = new SqlConnection("Data Source=" + ServerNameCombo.Text + ";Initial Catalog=master;Persist Security Info=True;User ID=" + UserNameEdit.Text + ";Password=" + PasswordEdit.Text))
{
    using (SqlCommand dbs = new SqlCommand("Select name from sysdatabases", conn))
    {
        conn.Open();
        using (SqlDataReader reader = dbs.ExecuteReader())
        {
            while (reader.Read())
            {
                using (SqlConnection dbconn = new SqlConnection("Data Source=" + ServerNameCombo.Text + ";Initial Catalog=" + reader["name"].ToString() + ";Persist Security Info=True;User ID=" + UserNameEdit.Text + ";Password=" + PasswordEdit.Text))
                {
                    using (SqlCommand dbscmd = new SqlCommand("Select name from sysobjects where name = '" + TableName + "'", dbconn))
                    {
                        dbconn.Open();
                        if (dbscmd.ExecuteScalar() != null)
                        {
                            DBNames += (DBNames != "" ? "," : "") + reader["name"].ToString();
                        }
                    }
                }
            }
        }
    }
}

解决方案

This is expected behaviour; it closes the managed connection, which means it releases the underlying connection the connection pool. This keeps the connection artificially open so that the next managed connection for the same connection-string and identity can use the existing connection (setting the reset bit in the TDS pipeline) to avoid connection spin-up latency.

If you don't want this: disable the connection pool in the connection string (Pooling=false).

这篇关于"使用"关键字不会关闭打开的SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆