正如上面评论之一所建议的,您应该首先更改代码以使用参数化查询。这将使您不再需要分隔值,并且还会使您的代码更安全。此外,您应该利用using
声明让.NET更好地管理资源。
进行这些更改后,您的代码将看起来更像这样:
string query =
@"INSERT INTO tbl_RentFactor([ID],DateNow,customerName, objectName,
objectNumber,unitCost,objectCost,paidMoney,restOfMonyy,customerID,DateBack)
VALUES (?,?,?,?,?,?,?,?,?,?,?)";
con.Open();
for (int i = 0; i < (dataGridFactorRent.Rows.Count) - 1; i++)
{
using (var command = new OleDbCommand(query, con));
{
command.Parameters.AddWithValue("?", ID);
command.Parameters.AddWithValue("?", lbldate.Text);
command.Parameters.AddWithValue("?", cmdCustomName.Text);
command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[1].Value);
command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[3].Value);
command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[4].Value);
command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[5].Value);
command.Parameters.AddWithValue("?", txtPaid.Text);
command.Parameters.AddWithValue("?", lblRemained.Text);
command.Parameters.AddWithValue("?", customerID);
command.Parameters.AddWithValue("?", lbldate.Text);
command.ExecuteNonQuery();
}
}
con.Close();
如果进行这些修改后仍然收到错误,请仔细检查 INSERT 语句中的字段名称。