C# 在C语言中分离sqlserver数据库#

C# 在C语言中分离sqlserver数据库#,c#,sql-server,database,detach,C#,Sql Server,Database,Detach,我得到这个错误: 无法分离数据库“PhoneBookDB”,因为它当前正在使用。 已将数据库上下文更改为“主” 我该怎么办?你能试试这个吗: SqlConnection con = new SqlConnection(); con.ConnectionString = @"Data Source =.; Initial Catalog = master; Integrated Security = True;"; con.Open(); string str = "USE master;" +

我得到这个错误:

无法分离数据库“PhoneBookDB”,因为它当前正在使用。 已将数据库上下文更改为“主”

我该怎么办?

你能试试这个吗:

SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source =.; Initial Catalog = master; Integrated Security = True;";
con.Open();

string str = "USE master;" +
"EXEC sp_detach_db @dbname = N'PhoneBookDB'";

SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Detached", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
您还可以尝试使用
sp_who
sp_whoisactive
查找当前连接并将其终止。

您可以尝试以下操作:

SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source =.; Initial Catalog = master; Integrated Security = True;";
con.Open();

string str = "USE master;" +
"EXEC sp_detach_db @dbname = N'PhoneBookDB'";

SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Detached", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();

您还可以尝试使用
sp_who
sp_whoisactive
查找当前连接并将其终止。

但没有其他人在使用数据库?您是否尝试在分离之前将数据库设置为脱机和/或单用户?没有其他人在使用数据库?您是否尝试过将数据库设置为脱机和/或单用户分离?