[英]How to fix ExecuteNonQuery error
An exception is thrown on ExecuteNonQuery
, how can I fix it?在
ExecuteNonQuery
上抛出异常,我该如何解决?
string sqlQuery = "INSERT INTO transaction(ID,item_name,quantity,price,T_date) values (?,?,?,?,?)";
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\KCdb.accdb"))
using (OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@item_name", item.ToString());
cmd.Parameters.AddWithValue("@quantity", quantity);
cmd.Parameters.AddWithValue("@price", price);
cmd.Parameters.AddWithValue("@T_date", dt.ToString());
cmd.ExecuteNonQuery();
}
For guiding you, we need the exception , because there will be different reasons of failure.为了指导你,我们需要例外,因为会有不同的失败原因。 But as I can see in your code there are some casts in assigning value to parameters, when your parameter is of DateTime type you should pass a DateTime as its value not a string.
但是正如我在您的代码中看到的那样,在为参数分配值时有一些强制转换,当您的参数是 DateTime 类型时,您应该将 DateTime 作为其值而不是字符串传递。 SO I think you should change
cmd.Parameters.AddWithValue("@T_date", dt.ToString());
所以我认为你应该改变
cmd.Parameters.AddWithValue("@T_date", dt.ToString());
to cmd.Parameters.AddWithValue("@T_date", dt);
到
cmd.Parameters.AddWithValue("@T_date", dt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.