使用参数化Alter Database命令时@ [参数名称]附近的语法不正确

时间:2020-11-30 23:08:57

I'm trying to drop a SQL Server database using the following code:

我正在尝试使用以下代码删除SQL Server数据库:

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:

当我执行它时,我收到错误:

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'.

'@database'附近的语法不正确。关键字'with'附近的语法不正确。如果此语句是公用表表达式或xmlnamespaces子句,则必须以分号结束前一个语句。 'IMMEDIATE'附近的语法不正确。

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


5  

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命令不支持你想要的参数。你必须在这里连接字符串。

#2


0  

Are the Params case sensitive? You have a capital D in the 2nd @Database.

Params是否敏感?你在第二个@Database中有一个大写字母D.

#1


5  

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命令不支持你想要的参数。你必须在这里连接字符串。

#2


0  

Are the Params case sensitive? You have a capital D in the 2nd @Database.

Params是否敏感?你在第二个@Database中有一个大写字母D.