Incorrect syntax near @ [parameter name] when using parameterized Alter Database command
I am 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 an error:
Incorrect syntax near '@database'. Incorrect syntax near the 'c' keyword. If this statement is a generic table expression or xmlnamespaces, the previous expression must end with a semicolon. Incorrect syntax next to "IMMEDIATE".
What am I doing wrong?
0
Samuel jack
source
to share
2 answers
Assuming it, the Alter Database command does not support the options you want. you have to enclose the lines here.
+5
Mladen Prajdic
source
to share
Is Params case sensitive? You have capital D in the second @Database.
0
Mr. Flibble
source
to share