views:

790

answers:

2

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:

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

What am I doing wrong?

A: 

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

Mr. Flibble
Good thought, but actually that doesn't seem to make a difference
Samuel Jack
And can you add a param twice like that, with one add param?Might want to try using GO instead of ';'...
Mr. Flibble
+4  A: 

putting it simply the Alter Database command doesn't support parameters as you want it to. you'll have to concat strings here.

Mladen Prajdic
Mladen, That was the conclusion I came to. Thanks for confirming it.
Samuel Jack