执行OleDbCommand时出错.“必须声明标量变量"@MaxID"." [英] Error in executing an OleDbCommand.. “Must declare the scalar variable ”@MaxID“.”

查看:79
本文介绍了执行OleDbCommand时出错.“必须声明标量变量"@MaxID"."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void AddValue(string strValue)
{
      //get the maximum id for Lists first
      int MaxID = DataOperations.ReturnMaxIDInATable("Lists", connString);
      int iSqlStatus = 0;
      string query = "INSERT INTO Lists(ID, ListName, ListValue) 
         VALUES(@MaxID, @ListName, @ListValue)";
      MaxID++;
      OleDbConnection dbConn = new OleDbConnection(connString);
      OleDbCommand dbComm = new OleDbCommand();
      dbComm.Parameters.Clear();
      try
      {
                dbComm.CommandText = query;
                dbComm.CommandType = CommandType.Text;
                OleDbParameter IDParam = new OleDbParameter();
                IDParam.ParameterName = "@MaxID";
                IDParam.OleDbType = OleDbType.BigInt;
                IDParam.Value = MaxID;
                dbComm.Parameters.Add(IDParam);
                dbComm.Parameters.AddWithValue("@ListName", ListName);
                dbComm.Parameters.AddWithValue("@ListValue", strValue);
                dbComm.Connection = dbConn;
                DataAccess.HandleConnection(dbConn);
                iSqlStatus = Convert.ToInt16(dbComm.ExecuteNonQuery());
                //Now check the status
                if (iSqlStatus != 0)
                {
                    //DO your failed messaging here
                    //return false;
                }
                else
                {
                    //Do your success work here
                    //dbComm.
                    //return true;
                }
      }
      catch (Exception ex)
      {
                MessageBox.Show(ex.Message, "Error inserting value in "
                                            + ListName + ","
                                            + strValue);
                //return false;
      }
      finally
      {
                DataAccess.HandleConnection(dbConn);
      }
}

推荐答案

您在这里得到了答案 [
You got answer there Error in executing an OleDbCommand.. "Must declare the scalar variable "@MaxID"."[^] also explanations there



而不是使用"IDParam.ParameterName ="@MaxID";您可以尝试将@MaxId传递为dbComm.Parameters.AddWithValue("@ MaxId",MaxID);.
Hi,
Instead of using "IDParam.ParameterName = "@MaxID";" can you try to pass the @MaxId as dbComm.Parameters.AddWithValue("@MaxId", MaxID);.


为什么不这样做:

Why don''t you just do it like this:

string query = string.Format("INSERT INTO Lists(ID, ListName, ListValue) 
         VALUES({0}, '{1}', '{2}')", MaxID, ListName, strValue);



那你就不需要参数了...

编辑===============

为什么要进行1票表决?



Then you don''t need the parameters...

EDIT ===============

Why was this 1-voted?


这篇关于执行OleDbCommand时出错.“必须声明标量变量"@MaxID"."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆