我正在尝试使用 SqlParameters
删除一个表。我有这个代码。
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();
我收到这个错误:
Incorrect syntax near '@nume'.
但是当我这样做的时候
SqlCommand com = new SqlCommand("drop table " + name, dbCon);
它有效,我真的不明白这个错误。
最佳答案
您不能为表名使用参数。尽管您通常会在这里被告知不要使用字符串连接来构建查询,但这是您需要这样做的一次!
SqlCommand com=new SqlCommand("drop table " + name, dbCon);
关于SQL 服务器 : drop table with SQL parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29018450/