如何强制SQLconnection释放数据库?

发布于 2024-07-15 20:22:32 字数 116 浏览 4 评论 0原文

我编写了一个小应用程序,可以恢复数据库(C# 和 SQL2005),但是在访问数据库后,我无法删除它 - 它说它正在使用中。 我想这与 SQLconnection-pooling 有关,但是我可以强制它释放数据库吗?

I have written a small application, that can restore a database (C# and SQL2005), but after I have accessed the database, I can't drop it - it says that it is in use..
I guess it has to do with the SQLconnection-pooling, but can I force it to relase the database??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一城柳絮吹成雪 2024-07-22 20:22:32

在连接字符串中指定“Pooling=False”。

Specify 'Pooling=False' in the connection string.

说不完的你爱 2024-07-22 20:22:32

处置您的 SqlConnection 对象。

Dispose your SqlConnection object.

腹黑女流氓 2024-07-22 20:22:32

如果您使用

Using SQLConnection
    '' do your stuff here
End Using

我认为这会在退出后强制释放资源

If you utlise

Using SQLConnection
    '' do your stuff here
End Using

I think this then forces the freeing of resources after it exits

入怼 2024-07-22 20:22:32

之一

调用“USE SomeOtherDB”(例如Master)来关闭自己的连接,或者调用ALTER DATABASE SET SINGLE_USER

ALTER DATABASE SET SINGLE_USER WITH ROLLBACK_IMMEDIATE

来关闭其他连接。 第一个等待连接结束,第二个是立即连接。

Call "USE SomeOtherDB" (e.g Master) to close your own connection, or one of

ALTER DATABASE SET SINGLE_USER

or

ALTER DATABASE SET SINGLE_USER WITH ROLLBACK_IMMEDIATE

to close other connections. The first waits for connections to conclude, the second is immediate.

偷得浮生 2024-07-22 20:22:32

“调用“USE SomeOtherDB”(例如Master)来关闭您自己的连接,或其中之一”

//on master ... CREATE
using (var cnn = new SqlConnection(MASTER))
{
    cnn.Open();
    var createDbCmd = new SqlCommand(string.Format("create database [{0}]", db), cnn).ExecuteNonQuery();
    cnn.Close();
}
using (var cnn = new SqlConnection(tempDB))
{
    cnn.Open();
    var createTbl = new SqlCommand(string.Format("create table t (t int )"), cnn).ExecuteNonQuery();
    var dropTbl = new SqlCommand(string.Format("drop table t"), cnn).ExecuteNonQuery();
    //Do additional stuf
    var userMaster = new SqlCommand(string.Format("use master"), cnn).ExecuteNonQuery();
    cnn.Close();

}
//on master ... CREATE
using (var cnn = new SqlConnection(MASTER))
{
    cnn.Open();
    var dropDbCmd = new SqlCommand(string.Format("drop database [{0}]", db), cnn).ExecuteNonQuery();
    cnn.Close();

}

"Call "USE SomeOtherDB" (e.g Master) to close your own connection, or one of"

//on master ... CREATE
using (var cnn = new SqlConnection(MASTER))
{
    cnn.Open();
    var createDbCmd = new SqlCommand(string.Format("create database [{0}]", db), cnn).ExecuteNonQuery();
    cnn.Close();
}
using (var cnn = new SqlConnection(tempDB))
{
    cnn.Open();
    var createTbl = new SqlCommand(string.Format("create table t (t int )"), cnn).ExecuteNonQuery();
    var dropTbl = new SqlCommand(string.Format("drop table t"), cnn).ExecuteNonQuery();
    //Do additional stuf
    var userMaster = new SqlCommand(string.Format("use master"), cnn).ExecuteNonQuery();
    cnn.Close();

}
//on master ... CREATE
using (var cnn = new SqlConnection(MASTER))
{
    cnn.Open();
    var dropDbCmd = new SqlCommand(string.Format("drop database [{0}]", db), cnn).ExecuteNonQuery();
    cnn.Close();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文