我正在尝试创建一个临时数据库以在 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/