INSERT INTO语法错误C#访问数据库

时间:2018-04-09 23:03:25

标签: c# database syntax access

当执行此查询并将参数添加为值时,我收到错误消息,说明我的语法错误。我尝试了多个教程,在这里查找问题是堆栈溢出,并且在比较时,它们看起来是一样的,但我的似乎不起作用。

OleDbConnection con = new OleDbConnection();
        con.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =C:\\Users\\fidyc\\OneDrive\\Desktop\\ProgrII.accdb";
        OleDbCommand cmd = new OleDbCommand("INSERT Product_Orders(order_ID,plankCount,thickness,width,height)VALUES(@order_ID, @plankCount, @thickness, @width, @height)");
        cmd.Parameters.Add("@order_ID", OleDbType.Integer).Value = orderID;
        cmd.Parameters.Add("@plankCount", OleDbType.Decimal).Value = plankCount;
        cmd.Parameters.Add("@thickness", OleDbType.Decimal).Value = thickness;
        cmd.Parameters.Add("@width", OleDbType.Decimal).Value = width;
        cmd.Parameters.Add("@height", OleDbType.Decimal).Value = height;
        cmd.Connection = con;
        con.Open();
        if (con.State == ConnectionState.Open)
        {

            /*try
            {*/
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Added");
                con.Close();
            /*}
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Source);
                con.Close();
            }*/
        }

编辑:将值传递给函数

public static void Push(int orderID, decimal plankCount, decimal thickness, decimal width, decimal height)
    {

2 个答案:

答案 0 :(得分:0)

问题原来是计数列名,因为sql有一个count命令,它需要是

 OleDbCommand cmd = new OleDbCommand("INSERT INTO Product_Orders(order_ID,[count],thickness,width,height)VALUES(@order_ID, @count, @thickness, @width, @height)");

而不是

 OleDbCommand cmd = new OleDbCommand("INSERT INTO Product_Orders(order_ID,count,thickness,width,height)VALUES(@order_ID, @count, @thickness, @width, @height)");

答案 1 :(得分:0)

using (OleDbCommand cmd = conn.CreateCommand())
 {
   cmd.CommandText = 
   "INSERT INTO bookRated "+
   "([firstName], [lastName]) "+
   "VALUES(@firstName, @lastName)";

    cmd.Parameters.AddRange(new OleDbParameter[]
       {
        new OleDbParameter("@firstName", firstName),
        new OleDbParameter("@lastName", lastName),
        });

           cmd.ExecuteNonQuery();
      }
相关问题