INSERT INTO语句中的语法错误(将多个记录从Gridview插入到Access)

时间:2014-07-02 13:09:58

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

我正在尝试将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();
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果sDate是日期类型,您可能需要格式化它 - 格式(“YYYY-mm-DD”,sDate)或格式(“YYYY-mm-DD HH:MM:ss”,sDate)