我试图将我所有的数据保存在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();
}
}
}
}
如果sDate是日期类型,则可能需要对其进行格式化- format ("YYYY-mm-DD",sDate)或format ("YYYY-mm-DD HH:MM:ss",sDate)