当前位置: 代码迷 >> Sql Server >> SQL表名使用SQLparameter出错
  详细解决方案

SQL表名使用SQLparameter出错

热度:28   发布时间:2016-04-24 10:02:06.0
SQL表名使用SQLparameter报错
  string strdata = "delete from @tab where ExamNo=1200";
 int num = SqlHelper.ExecuteNonQuery(strdata,new SqlParameter("@tab","text-初测-VOO"));


 public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn =  new SqlConnection(connstr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {

                    cmd.Parameters.AddRange(parameters);
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
报错 必须声明变量“@tab”


刚学这个,希望能够详细指导下,谢谢!
------解决方案--------------------
  string strdata = "delete from [" + "text-初测-VOO" + "] where ExamNo=1200";
 int num = SqlHelper.ExecuteNonQuery(strdata); //再写个无参数的函数

------解决方案--------------------
Parameter 只能替代字段不能替代表
也就是你只能delete from tablename where col=@col,而不能是你的那种写法