我在c#中有以下查询,并且不知道为什么它会向我显示此错误:
“INSERT INTO语句中的语法错误”。
我使用Access 2013。
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection;
command2.CommandText = "INSERT INTO money (price,cardnum,checknum,dateTime,employeeid) values('" + TempPrice + "','" + TempCriditNum + "','" + TempCheckNum + "','" + dateTimePickerX1.GetSelectedDateInPersianDateTime().ToShortDateString() + "','" + id + "')";
command2.ExecuteNonQuery();
connection.Close();
答案 0 :(得分:3)
要检查的一些事项
所以查询最终看起来像:
command2.CommandText = "INSERT INTO [money] (price,cardnum,checknum,[dateTime],employeeid) values(" + TempPrice + "," + TempCriditNum + "," + TempCheckNum + ",#" + dateTimePickerX1.GetSelectedDateInPersianDateTime().ToShortDateString() + "#," + id + ")";
答案 1 :(得分:3)
您的SQL语句可能容易出现SQL注入。考虑使用参数化查询,方法是通过OleDbCommand.Parameters
属性添加值,而不是连接它。
一个例子是:
command2.CommandText = "INSERT INTO [money] (price, cardnum, checknum, [dateTime], employeeid) values(@tempPrice, @tempCreditNum, @tempCheckNum, @dateTime, @id)";
command2.Parameters.AddRange(new OleDbParameter[] {
new OleDbParameter("@tempPrice", TempPrice),
new OleDbParameter("@tempCreditNum", TempCriditNum),
new OleDbParameter("@tempCheckNum", TempCheckNum),
new OleDbParameter("@dateTime", dateTimePickerX1.GetSelectedDateInPersianDateTime().ToShortDateString()),
new OleDbParameter("@id", id)
});
command2.ExecuteNonQuery();
这也应解决您的语法错误。