近@ [参数名称]使用参数化时,alter database命令的语法不正确 [英] Incorrect syntax near @[parameter name] when using parameterised Alter Database command
问题描述
我想用下面的code,掉了SQL Server数据库:
I'm trying to drop a SQL Server database using the following code:
SqlCommand command = new SqlCommand("USE MASTER; ALTER DATABASE @database SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE @Database", connection);
command.Parameters.AddWithValue("@database", TestingEnvironment.DatabaseName);
command.ExecuteNonQuery();
当我执行它,我得到的错误:
When I execute it, I get the error:
附近有语法错误@database。 关键字附近的不正确的语法 用。如果这种说法是一种常见的 表中的前pression或XMLNAMESPACES 子句,previous语句必须 以分号结束。不正确 附近的'即时'语法。
Incorrect syntax near '@database'. Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon. Incorrect syntax near 'IMMEDIATE'.
我是什么做错了吗?
推荐答案
把它简单地ALTER DATABASE命令不支持的参数,你想要它。你必须在这里Concat的字符串。
putting it simply the Alter Database command doesn't support parameters as you want it to. you'll have to concat strings here.
这篇关于近@ [参数名称]使用参数化时,alter database命令的语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!