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

后端 未结 4 1164
醉梦人生
醉梦人生 2021-01-14 07:48
private void AddValue(string strValue)

{

      //get the maximum id for Lists first

      int MaxID = DataOperations.ReturnMaxIDInATable(\"Lists\", connString);
          


        
相关标签:
4条回答
  • 2021-01-14 07:58

    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.

    0 讨论(0)
  • 2021-01-14 08:08

    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);
    
    0 讨论(0)
  • 2021-01-14 08:21

    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.

    0 讨论(0)
  • 2021-01-14 08:21

    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?

    0 讨论(0)
提交回复
热议问题