[英]SQL Server : drop table with SQL parameters
I'm trying to drop a table using SqlParameters
. 我正在尝试使用
SqlParameters
删除表。 I have this code . 我有这个代码。
dbCon.Open();
DataRowView d= (DataRowView) cmbTabele.Items[cmbTabele.SelectedIndex];
string name = (d["table_name"]as string);
SqlCommand com=new SqlCommand("drop table @nume ", dbCon);
com.Parameters.Clear();
SqlParameter param = new SqlParameter("@nume", name);
com.Parameters.Add(param);
com.ExecuteNonQuery(); // ERROR HERE
dbCon.Close();
I receive this error : 我收到此错误:
Incorrect syntax near '@nume'.
'@nume'附近的语法不正确。
But when I do 但是,当我这样做
SqlCommand com = new SqlCommand("drop table " + name, dbCon);
it works, and I really don't understand this error. 它的工作原理,我真的不明白这个错误。
You cannot use a parameter for a table name. 您不能将参数用于表名。 Although you'll normally get told off around here for building up a query using string concatenation, this is one occasion where you'll need to!
虽然你通常会在这里被告知使用字符串连接来构建查询,但这是你需要的一个场合!
SqlCommand com=new SqlCommand("drop table " + name, dbCon);
I do not recommand it, but if you really want to use SQLParameter, then it is possible this way. 我不推荐它,但如果你真的想使用SQLParameter,那么就有可能这样。
SqlCommand com=new SqlCommand("EXEC('drop table ''' + @nume + '''')", dbCon);
But really, there is no advantage in doing it this way. 但实际上,这样做没有任何优势。 This work on SQL Server 2005 and newest version of it.
这项工作在SQL Server 2005及其最新版本上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.