插入,删除或更新无法执行? [英] Insert, Delete or Update cannot be performed?

查看:233
本文介绍了插入,删除或更新无法执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询似乎是正确的,但为什么会这样



  OleDbConnection的CON =新的OleDbConnection(); 

con.ConnectionString = @供应商= Microsoft.ACE.OLEDB.12.0;数据源= C:\Users\Sparrow vivek\Documents\Billing.accdb
con.Open();
的DateTime一个= Convert.ToDateTime(label2.Text);
查询字符串=INSERT INTO资产负债表(BillNumber,CusName,日期,金额)值(,,,????);
OleDbCommand的CMD =新的OleDbCommand(查询,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();


解决方案

您的查询中包含的保留关键字:日期。要使用它,你需要使用周围的名称



 查询字符串=INSERT INTO资产负债表(BillNumber,CusName,[日期方括号],金额)值(,,,)????; 



强烈建议,以避免这些名字。如果仍然是尽快名可能的改变。



现在让我们来看看AddWithValue的该列表。在此方法的参数的数据类型是自动您传递的值决定。你必须传递给字符串类型的参数集合中的每个值。但很可能你的数据库字段不想要一个字符串值。例如日期,金额似乎需要一个datetime和一个数字

  cmd.Parameters.AddWithValue(?,label3.Text) ; 
cmd.Parameters.AddWithValue(,label4.Text?);
cmd.Parameters.AddWithValue(,一个?);
cmd.Parameters.AddWithValue(,Convert.ToDecimal(label6.Text)?);


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();

解决方案

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));

这篇关于插入,删除或更新无法执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆