Error in executing an OleDbCommand.. “Must declare the scalar variable ”@MaxID“.”

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 09:59:20

问题


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);
      }

}

回答1:


I believe you need to use question marks for the parameters when executing SQL through the OleDbCommand (while SqlCommand uses @). Example:

INSERT INTO Lists (ID, ListName, ListValue) VALUES (?, ?, ?)

You only need to Add the Parameters in the order that they appear in the SQL.




回答2:


If you include the clause "DECLARE" in the start of query, will work:

string query = "DECLARE @MaxID as bigint, "+
                      " @ListName as Varchar(100), "+
                      " @ListValue As Varchar(100) " +
        " INSERT INTO Lists(ID, ListName, ListValue) " +
            " VALUES(@MaxID, @ListName, @ListValue)"

Furthermore, the right solution is change your driver to SQLClient and OracleClient. OleDb is not recomended for be used with SQL 2005 and above.




回答3:


The following fragment should read:

IDParam.ParameterName = "MaxID";
IDParam.OleDbType = OleDbType.BigInt;
IDParam.Value = MaxID;
dbComm.Parameters.Add(IDParam);

dbComm.Parameters.AddWithValue("ListName", ListName);
dbComm.Parameters.AddWithValue("ListValue", strValue);



回答4:


This seemed to do the trick..

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

Although I have reservations about it like what if I need to add a date value?



来源:https://stackoverflow.com/questions/5870146/error-in-executing-an-oledbcommand-must-declare-the-scalar-variable-maxid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!