我工作在C#中的一个解决方案,我可以丢弃在SQL Server 2008 R2 Express版本的数据库。我搜索了WWW,发现如下解决方案: Script to kill all connections to a database (More than RESTRICTED_USER ROLLBACK)DROP DATABASE的SQL Server 2008 R2速成版 - 异常
我也试图从SQL Server创建脚本:
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'apm4reform'
GO
USE [master]
GO
ALTER DATABASE [apm4reform] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
/****** Object: Database [apm4reform] Script Date: 10/10/2013 00:58:06 ******/
DROP DATABASE [apm4reform]
GO
这里我的代码使用方法:
private static bool DropDatabase(SqlConnection tmpConn, string connectionString, string databaseName)
{
string sqlDropDBQuery;
bool result = false;
try
{
tmpConn.ConnectionString = connectionString;
tmpConn.Open();
SqlCommand thisCommand = new SqlCommand();
thisCommand.Connection = tmpConn;
sqlDropDBQuery = string.Format("EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'{0}'\n GO\n USE [master]\n GO\n ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO\n USE [master]\n GO\n DROP DATABASE [{0}]\n GO\n", databaseName);
thisCommand.CommandText = sqlDropDBQuery;
thisCommand.ExecuteNonQuery();
result = true;
}
catch (Exception ex)
{
result = false;
}
finally
{
tmpConn.Close();
}
return result;
}
不幸的是,我总是得到一个异常:
附近有语法错误 'GO'。 'GO'附近语法不正确。 'GO'附近的语法不正确 。
我也试过没有逃逸的\ n!
有什么建议吗?
感谢, 卓
删除最后一个去 –
我必须删除所有GO并使用';'代替!谢谢 – tro
曾听说过'@“逐字串”'? –