Insert, Delete or Update cannot be performed?

让人想犯罪 __ 提交于 2019-12-23 23:53:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!