【发布时间】:2021-12-20 05:43:02
【问题描述】:
我正在使用此代码通过 C# 删除数据库
Int32 result = 0;
try
{
String Connectionstring = CCMMUtility.CreateConnectionString(false, txt_DbDataSource.Text, "master", "sa", "happytimes", 1000);
SqlConnection con = new SqlConnection();
con.ConnectionString = Connectionstring;
String sqlCommandText = "DROP DATABASE [" + DbName + "]";
if (con.State == ConnectionState.Closed)
{
con.Open();
SqlConnection.ClearPool(con);
con.ChangeDatabase("master");
SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
sqlCommand.ExecuteNonQuery();
}
else
{
con.ChangeDatabase("master");
SqlCommand sqlCommand = new SqlCommand(sqlCommandText, con);
sqlCommand.ExecuteNonQuery();
}
con.Close();
con.Dispose();
result = 1;
}
catch (Exception ex)
{
result = 0;
}
return result;
但我得到一个错误
当前正在使用的数据库
谁能帮忙?
【问题讨论】:
-
哪一行给出异常?
-
sqlCommand.ExecuteNonQuery();给出异常
-
@FrankvanPuffelen 您的解决方案有效。谢谢
标签: c# .net sql-server-2008