繁体   English   中英

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

[英]INSERT INTO Syntax error C# access database

使用添加为值的参数执行此查询时,我收到一条错误消息,指出我的语法错误。 我尝试了多个教程,在这里查找问题是堆栈溢出,比较时,它们似乎相同,但我的似乎不起作用。

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)
    {

原来是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)");
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();
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM