运行集成测试时抛出SqlException [重复](When run integration tests throw SqlException [duplicate])

编程入门 行业动态 更新时间:2024-10-05 15:23:50
运行集成测试时抛出SqlException [重复](When run integration tests throw SqlException [duplicate])

这个问题在这里已经有了答案:

“无法删除数据库,因为它当前正在使用中”。 怎么修? 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 answers

I 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?

更多推荐

本文发布于:2023-08-07 18:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   测试   SqlException   run   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!