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?
putting it simply the Alter Database command doesn’t support parameters as you want it to. you’ll have to concat strings here.