2008-11-10 43 views
0

我嘗試使用下面的代碼來刪除SQL Server數據庫:不正確的語法@ [參數名稱]當使用參數化alter database命令

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(); 

當我執行它,我得到的錯誤:

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

我在做什麼錯?

回答

5

把它簡稱爲你想讓它ALTER DATABASE命令不支持參數。你必須在這裏連接字符串。

0

是PARAMS區分大小寫?你在第二個@Database中有一個大寫字母D.

+0

沒想好,但實際上似乎並不有所作爲 – 2008-11-10 17:07:47

+0

你能不能添加PARAM兩次那樣,用一箇中添加PARAM? 可能想嘗試使用GO而不是';'... – 2008-11-10 17:14:30

相關問題