【问题标题】:Syntax error in INSERT INTO statement (Inserting Multiple Records From Gridview to Access)INSERT INTO 语句中的语法错误(将多条记录从 Gridview 插入到 Access)
【发布时间】:2014-07-02 13:09:57
【问题描述】:

我正在尝试将 DataGridView 中的所有数据保存到 Access 数据库中,但出现此错误:Syntax error in INSERT INTO statement.我的代码如下:

try
{
  string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\MotoFix.mdb;";

  OleDbCommand Com = new OleDbCommand();

  for (int i = 0; i < gridViewProduct_Transac.Rows.Count - 1; i++)
  {
    using (OleDbConnection Con = new OleDbConnection(ConnectionString))
    {
      string str;
      Con.Open();
      str = @"INSERT INTO Transaction(tranNo, custID, pID, userID, orderNo, sDate) VALUES ('" + gridViewProduct_Transac.Rows[i].Cells["Transaction Number"].Value + "', '" + gridViewProduct_Transac.Rows[i].Cells["Customer ID"].Value + "'," + gridViewProduct_Transac.Rows[i].Cells["Referral ID"].Value + ",'" + gridViewProduct_Transac.Rows[i].Cells["Cashier ID"].Value + "','" + gridViewProduct_Transac.Rows[i].Cells["Order Number"].Value + "','" + gridViewProduct_Transac.Rows[i].Cells["Date"].Value + "');";
      using (Com = new OleDbCommand(str, Con))
      {
        Com.ExecuteNonQuery();
        Con.Close();
      }
    }
  }
}

【问题讨论】:

  • TRANSACTION 是 Access SQL 中的 reserved word。试试INSERT INTO [Transaction] ...
  • 我已经更改了它,但我遇到了这个错误没有为一个或多个必需参数提供值。

标签: database ms-access datagridview save multiple-records


【解决方案1】:

如果 sDate 是日期类型,您可能需要对其进行格式化 - Format("YYYY-mm-DD",sDate) 或 Format("YYYY-mm-DD HH:MM:ss",sDate)

【讨论】:

  • 类似:str = "INSERT INTO Transaction(tranNo, custID, pID, userID, orderNo, sDate) VALUES ('" + gridViewProduct_Transac.Rows(I).Cells("Transaction Number")。 Value + "', '" + gridViewProduct_Transac.Rows(I).Cells("Customer ID").Value + "'," + gridViewProduct_Transac.Rows(I).Cells("Referral ID").Value + ",' " + gridViewProduct_Transac.Rows(I).Cells("收银员 ID").Value + "','" + gridViewProduct_Transac.Rows(I).Cells("订单号").Value + "','" + 字符串。 Format(gridViewProduct_Transac.Rows(I).Cells("Date").Value, "YYYY-mm-DD") + "');";
  • 我遇到了没有为一个或多个必需参数提供值。 :(
  • 它告诉您您的 6 个参数之一缺少值。检查您的报价和来者,并确保一切都是正确的。考虑把它拆开,这样更容易阅读。为每个值项分配短变量即创建一个变量 TransNo 并将其设置为 gridViewProduct_Transac.Rows(I).Cells("Transaction Number").Value 然后使用 TransNo 变量构建 sql
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多