Can I execute sql drop database cmd w/a SqlParameter

  • Thread starter Thread starter John Aldrin
  • Start date Start date
J

John Aldrin

Hi,

I'm wondering if it is possible to execute a sql command to drop a
database with a SqlParameter containing the name of the db to drop.

I've tried the following and I get an error

SqlCommand dropDbSqlCmd = _masterDbConn.CreateCommand ();
dropDbSqlCmd.CommandType = CommandType.Text;
dropDbSqlCmd.CommandText = "DROP DATABASE @dbToDrop";
SqlParameter dbNameParam = dropDbSqlCmd.Parameters.Add ( @dbToDrop,
SqlDbType.NVarChar, 128 );
dbNameParam.Value = dbName;


Error is

System.Data.SqlClient.SqlException : Line 1: Incorrect syntax near
'@dbToDrop'.

Thanx

jra
 
Parameters are used to insert literal values. In effect, the resulting sql
statement is: DROP DATABASE 'MyDatabase'. Which isn't the same as: DROP
DATABASE MyDatabase

So the short story is, you can't use parameters here.
 
Back
Top