C# SqlConnection/SqlCommand在关闭和处置数据库后保持数据库的使用状态
为了在Xunit中进行集成测试,我正在尝试创建一个临时数据库,但是当我尝试删除临时数据库时,我得到一个错误: 无法删除数据库“TempDatabase_u[numbers]”,因为它当前正在使用 简单地关闭和处理命令和连接似乎并没有切断它 这是我的测试的精简版本,失败了:C# SqlConnection/SqlCommand在关闭和处置数据库后保持数据库的使用状态,c#,.net,sql-server,sqlconnection,sqlcommand,C#,.net,Sql Server,Sqlconnection,Sqlcommand,为了在Xunit中进行集成测试,我正在尝试创建一个临时数据库,但是当我尝试删除临时数据库时,我得到一个错误: 无法删除数据库“TempDatabase_u[numbers]”,因为它当前正在使用 简单地关闭和处理命令和连接似乎并没有切断它 这是我的测试的精简版本,失败了: using System; using System.Data.SqlClient; using Xunit; namespace Test { public class Test_Raw_Spec {
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();
}
}
}
您是否尝试过在删除db之前等待一小段时间?很可能是连接没有及时关闭 另外,您不需要关闭连接,调用dispose将为您完成所有操作。另外,最好在using语句中这样做
using(var sqlConnection1 = new SqlConnection(string.Format(connectionStringTemplate, "master")))
{
//do your stuff here
}
这样,您就不必担心关闭任何东西,因为它会在使用块结束时自动关闭
我也不建议打开连接负载。一个连接就足够了,因为打开连接很昂贵。因此,创建一个(在您的情况下是两个),然后在每个命令中重复使用它。这是因为。当您关闭并处理该连接时,它将被释放回池中,以备再次使用,而不是被破坏。一次又一次地创建和销毁连接是一个非常昂贵的过程,因此使用连接池来提高应用程序的整体性能。当连接完成时(当池被回收或重新启动时,例如当应用程序启动或关闭时),连接将被破坏
此外,您可以更有效地使用命令和连接。如果命令执行完毕,则可以更改其文本。如果不想这样做,至少可以重用连接:
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();
}
}
}
在连接2和3之间添加120秒的延迟(
System.Threading.Thread.Sleep(2*60*1000);
)似乎没有帮助。我喜欢您的其他建议,并将在基线正常工作后实施这些建议。在删除数据库之前,请尝试调用sqlConnection2.ClearAllPools()。池是在SqlClient库中实现的。每个唯一连接字符串都有一个池,与开发人员使用“using”语句无关。是的,你是对的。在我键入注释并将其删除后,我意识到…ALTER DATABASE[test]设置SINGLE_USER WITH ROLLBACK after 5
将在5s后强制关闭数据库中的所有其他连接(例如池中的其他连接)。我发现您还可以将Pooling=false
添加到连接字符串中,最后还清除了连接池(请参阅)。由于集成测试应该是所有测试的一个较小的子集,所以这对我来说也是一个可行的替代方案。