INSERT INTO语法错误c#,Access

时间:2016-07-12 20:17:52

标签: c# sql access

我得到" INSERT INTO语法错误"在我的查询中,我不知道为什么。 (与Access的连接在另一个类中。)

    public void InsertOrder(Order Item) // add new order
    {
        string cmdStr = "INSERT INTO [Order] (CostumerID,ProID,ProName,ProPrice,Comments,Discount,Color,Size,Quantity,OrdertDate) VALUES (@costumerID,@proID,@proName,@proPrice,@comments,@discount,@proColor,@proSize,@quantity,@orderDate)";
        using (OleDbCommand command = new OleDbCommand(cmdStr))
        {

            command.Parameters.AddWithValue("@costumerID", Item.CostumerID);
            command.Parameters.AddWithValue("@proID", Item.ProId);
            command.Parameters.AddWithValue("@proName", Item.ProName);
            command.Parameters.AddWithValue("@proPrice", Item.ProPrice);
            command.Parameters.AddWithValue("@comments", Item.Comments);
            command.Parameters.AddWithValue("@discount", Item.Discount);
            command.Parameters.AddWithValue("@proColor", Item.ProColor);
            command.Parameters.AddWithValue("@proSize", Item.ProSize);
            command.Parameters.AddWithValue("@quantity", Item.Quantity);
            command.Parameters.AddWithValue("@orderDate", Item.OrderDate);

            base.ExecuteSimpleQuery(command);
        }

    }

请帮忙吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

您需要查看此页面:Reserved Words in MSAccess

您会注意到SIZE是一个保留关键字,因此,如果您真的无法更改该名称并仍希望在代码中使用它,则需要将其括在方括号中

   string cmdStr = @"INSERT INTO [Order] 
          (CostumerID,ProID,ProName,ProPrice,Comments,
           Discount,Color,[Size],Quantity,OrdertDate) 
           VALUES (@costumerID,@proID,@proName,@proPrice,@comments,
                   @discount,@proColor,@proSize,@quantity,@orderDate)";