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

使用添加为值的参数执行此查询时,出现错误,提示我的语法错误。我尝试了以下多个教程,在这里查找问题是堆栈溢出,并且进行比较时,它们似乎相同,但是我的似乎不起作用。


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)

    {


慕哥6287543
浏览 220回答 2
2回答

慕田峪9158850

问题原来是count列名,因为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)");

慕桂英4014372

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();      }
打开App,查看更多内容
随时随地看视频慕课网APP