我正在构建一个尝试在App_Start上安装/升级数据库的Web应用程序.部分安装过程是确保数据库安装了asp.net功能.为此,我使用System.Web.Management.SqlServices对象.
我的目的是在SQL事务中执行所有数据库工作,如果其中任何一个失败,则回滚事务并保持数据库不变.
SqlServices对象有一个方法"Install",它接受ConnectionString而不是事务.所以我使用SqlServices.GenerateApplicationServicesScripts,如下所示:
string script = SqlServices.GenerateApplicationServicesScripts(true, SqlFeatures.All, _connection.Database);
SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, script, ...);
Run Code Online (Sandbox Code Playgroud)
然后我使用企业库中的SqlHelper.
但是这会引发一个异常错误的异常,其中一些是错误的
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near the keyword 'USE'.
Incorrect syntax near the keyword 'CREATE'.
Incorrect syntax near 'GO'.
The variable name '@cmd' has already been declared. Variable names must be unique within a query batch or stored procedure.
Run Code Online (Sandbox Code Playgroud)
我假设在SQL事务中使用GO语句存在一些问题.
如何以这种方式执行时,如何使生成的脚本生效.