0

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?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Mladen, That was the conclusion I came to. Thanks for confirming it.
0

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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.