c# - SqlConnection/SqlCommand 在 Close 和 Dispose 后保持数据库处于使用状态

标签 c# .net sql-server sqlconnection sqlcommand

我正在尝试创建一个临时数据库以在 Xunit 中进行集成测试,但是当我尝试删除临时数据库时,我收到错误:

Cannot drop database "TempDatabase_[numbers]" because it is currently in use.

简单地关闭和处理命令和连接似乎并没有切断它。

这是我的测试失败的精简版本:

using System;
using System.Data.SqlClient;
using Xunit;

namespace Test
{
    public class Test_Raw_Spec
    {
        [Fact]
        public void PerformWorkInTemporaryDatabase()
        {
            string connectionStringTemplate = "Data Source=SQLEXPRESS;Initial Catalog={0};Integrated Security=SSPI;Connection Timeout=10";
            int dbNum = (new Random()).Next() % 1000000;
            int tblNum = (new Random()).Next() % 1000000;

            string nameTempDb = $"TempDatabase_{dbNum}";
            string nameTempTable = $"TempTable_{tblNum}";

            var sqlConnection1 = new SqlConnection(string.Format(connectionStringTemplate, "master"));
            var sqlCommand1 = new SqlCommand($"CREATE DATABASE {nameTempDb}", sqlConnection1);
            sqlConnection1.Open();
            sqlCommand1.ExecuteNonQuery();
            sqlCommand1.Dispose();
            sqlConnection1.Close();
            sqlConnection1.Dispose();

            var sqlConnection2 = new SqlConnection(string.Format(connectionStringTemplate, nameTempDb));
            var sqlCommand2 = new SqlCommand($"CREATE TABLE {nameTempTable}(id int)", sqlConnection2);
            sqlConnection2.Open();
            sqlCommand2.ExecuteNonQuery();
            sqlCommand2.Dispose();
            sqlConnection2.Close();
            sqlConnection2.Dispose();

            var sqlConnection3 = new SqlConnection(string.Format(connectionStringTemplate, "master"));
            var sqlCommand3 = new SqlCommand($"DROP DATABASE {nameTempDb}", sqlConnection3);
            sqlConnection3.Open();
            sqlCommand3.ExecuteNonQuery();
            sqlCommand3.Dispose();
            sqlConnection3.Close();
            sqlConnection3.Dispose();
        }
    }
}

最佳答案

这是因为 Connection Pooling .当您关闭并处理连接时,它会释放回池中,准备再次使用而不是被销毁。一遍又一遍地创建和销毁连接是一个非常昂贵的过程,因此使用连接池来尝试提高应用程序的整体性能。连接在完成时被销毁(当池被回收或重新启动时,例如当您的应用程序启动或关闭时。)

此外,您可以更有效地使用命令和连接。如果命令执行完毕,您可以更改命令的文本。如果您不想这样做,您至少可以重用连接:

private void Execute()
{
    using (var connection = new SqlConnection("."))
    {
        connection.Open();

        using (var command = connection.CreateCommand())
        {
            command.CommandText = "CREATE DATABASE [test]";
            command.ExecuteNonQuery();

            connection.ChangeDatabase("test");
            command.CommandText = "CREATE TABLE [dbo].[MyTable] (id int)";
            command.ExecuteNonQuery();
            
            // you must change your db context to drop the database
            connection.ChangeDatabase("master");
            command.CommandText = "DROP DATABASE [test]";
            command.ExecuteNonQuery();
        }
    }
}

关于c# - SqlConnection/SqlCommand 在 Close 和 Dispose 后保持数据库处于使用状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65384069/

相关文章:

c# - 修复 RegEx Split() 函数 - 空字符串作为第一个条目

.net - FileIO.FileSystem.CopyFile() 与 System.IO.File.Copy()

sql-server - 在 VB.NET 中获取 SQL Server 表中的行数

sql - 如果 T-SQL 中记录为 NULL,如何替换字符串

sql - 使用 WHERE 删除 - 日期、时间和字符串比较 - 非常慢

c# - 使用批处理文件从命令行调用 C# exe

c# - ActionFilter - 发送 HTTP header 后无法重定向

java - Protobuf-net 与 Java UUID 和 C# Guid 互操作

c# - SharpSvn GetInfo LastChangeRevision 错误

.net - 整个.NET Framework使用哪些设计模式?