这个问题在这里已经有了答案:
“无法删除数据库,因为它当前正在使用中”。 怎么修? 7个答案我正在运行集成测试。 在开始测试之前,我删除了数据库并再次创建它。 第一次测试成功。 但是在第二次测试开始时我得到一个例外:
SetUp:System.Data.SqlClient.SqlException:无法删除数据库“Test”,因为它当前正在使用中。
Сode:
[TestFixture] class Class1 { public SqlConnection Repository; [SetUp] public void LocInit() { Repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;"); Repository.Open(); Repository.Execute("USE master;"); Repository.Execute("DROP DATABASE Test;"); Repository.Execute("USE master; CREATE DATABASE Test;"); Repository.Execute("USE Test;"); } [Test] public void Test1() { using (var repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;")) repository.Execute("USE Test; SELECT 10"); } [Test] public void Test2() { using (var repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;")) repository.Execute("USE Test; SELECT 10"); } [TearDown] public void LocalTearDown() { Repository.Dispose(); } }为什么我会得到这个例外?
This question already has an answer here:
“Cannot drop database because it is currently in use”. How to fix? 8 answersI am running integration tests. Before starting the test, I deleted the database and create it again. The first test is successful. But at the start of the second test I get an exception:
SetUp : System.Data.SqlClient.SqlException : Cannot drop database "Test" because it is currently in use.
Сode:
[TestFixture] class Class1 { public SqlConnection Repository; [SetUp] public void LocInit() { Repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;"); Repository.Open(); Repository.Execute("USE master;"); Repository.Execute("DROP DATABASE Test;"); Repository.Execute("USE master; CREATE DATABASE Test;"); Repository.Execute("USE Test;"); } [Test] public void Test1() { using (var repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;")) repository.Execute("USE Test; SELECT 10"); } [Test] public void Test2() { using (var repository = new SqlConnection(@"Data Source=.\SQLEXPRESS; Integrated Security=true;")) repository.Execute("USE Test; SELECT 10"); } [TearDown] public void LocalTearDown() { Repository.Dispose(); } }Why do I get this exception?
最满意答案
我自己没试过,但也许你可以试试这个:
在连接字符串中设置Pooling = false
要么
删除数据库之前清除池:SqlConnection.ClearAllPools()
“无法删除数据库,因为它当前正在使用中”。 怎么修?
Havent tried it myself but maybe you can try this:
Set Pooling=false in your connection string
or
Clear the pool before you delete the database: SqlConnection.ClearAllPools()
"Cannot drop database because it is currently in use". How to fix?
更多推荐
发布评论