问题
My query seems to be correct but why is this happening?
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sparrow vivek\Documents\Billing.accdb";
con.Open();
DateTime a = Convert.ToDateTime(label2.Text);
String query = "INSERT INTO balancesheet (BillNumber,CusName,Date,Amount) values (?,?,?,?)";
OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue("?",label3.Text);
cmd.Parameters.AddWithValue("?", label4.Text);
cmd.Parameters.AddWithValue("?", a.ToString("yyyy-MM-dd"));
cmd.Parameters.AddWithValue("?", label6.Text);
cmd.ExecuteNonQuery();
con.Close();
回答1:
Your query contains a reserved keyword: Date. To use it you need to use square brackets around that name
String query = "INSERT INTO balancesheet (BillNumber,CusName,[Date],Amount) values (?,?,?,?)";
It is highly recommended to avoid these names. If it is still possible change that name ASAP.
Now let's examine that list of AddWithValue. In this method the datatype of the parameter is automatically determined by the value that you pass. You have every value passed to the parameter collection of type string. But it is probable that your database fields doesn't want a string as value. For example Date,Amount seems requires a datetime and a number
cmd.Parameters.AddWithValue("?",label3.Text);
cmd.Parameters.AddWithValue("?", label4.Text);
cmd.Parameters.AddWithValue("?", a);
cmd.Parameters.AddWithValue("?", Convert.ToDecimal(label6.Text));
来源:https://stackoverflow.com/questions/27489119/insert-delete-or-update-cannot-be-performed