发生错误查询值的数量和目标字段不相同。 [英] Error occur number of query values and destination fields are not the same.

查看:108
本文介绍了发生错误查询值的数量和目标字段不相同。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

con = new OleDbConnection(Provider = Microsoft.Jet.OLEDB.4.0; Data Source = E:\\Sohkidatabase \\ xyz.accdb);

con.Open ();



com.Connection = con;

com.CommandType = CommandType.Text;



com.CommandText =插入Tbl_challanNo值(@ good_desc,@ quantity,@ taaff_class,@ Identification,@估算,@ date_of_issue,@ nature_of_procesing,@ factory,@ Expdur_proc,@ Place,@ date) ;

com.Parameters.Clear();

com.Parameters.AddWithValue(@ good_des,txt_goods.Text);

com.Parameters.AddWithValue(@ quantity,txt_quant.Text);

com.Parameters.AddWithValue(@ taaff_class,txt_tariff.Text);

com。 Parameters.AddWithValue(@ Identification,txt_ident.Text);

com.Parameters.AddWithValue(@ estimated,txt_estval.Text);

com.Parameters。 AddWithValue( @ date_of_issue,dateTimePicker_issue.Value.ToShortDateString() );

com.Parameters.AddWithValue(@ nature_of_procesing,txt_natproc.Text);

com.Parameters.AddWithValue(@ factory,txt_fact.Text);

com.Parameters.AddWithValue(@ Expdur_proc,txt_durprocess.Text);

com.Parameters.AddWithValue(@ Place,txt_place.Text);

com.Parameters.AddWithValue(@ date,dateTimePicker1.Value.ToShortDateString());



if(con.State == ConnectionState .Closed)

con.Open();

com.ExecuteNonQuery();

con.Close();

System.Windows.Forms.MessageBox.Show(Recrod Succefully Created);





在我的表中(ms访问) )

id用作主键

1)id

2)goods_desc

'

'

'12)日期



我的尝试:



i使用msaccess db我发现此错误查询值和目标字段的数量s不一样。

con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = E:\\Sohkidatabase\\xyz.accdb");
con.Open();

com.Connection = con;
com.CommandType = CommandType.Text;

com.CommandText = "insert into Tbl_challanNo values(@good_desc,@quantity,@taaff_class,@Identification,@estimated,@date_of_issue,@nature_of_procesing,@factory,@Expdur_proc,@Place,@date)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@good_des", txt_goods.Text);
com.Parameters.AddWithValue("@quantity", txt_quant.Text);
com.Parameters.AddWithValue("@taaff_class", txt_tariff.Text);
com.Parameters.AddWithValue("@Identification", txt_ident.Text);
com.Parameters.AddWithValue("@estimated", txt_estval.Text);
com.Parameters.AddWithValue("@date_of_issue",dateTimePicker_issue.Value.ToShortDateString());
com.Parameters.AddWithValue("@nature_of_procesing", txt_natproc.Text);
com.Parameters.AddWithValue("@factory", txt_fact.Text);
com.Parameters.AddWithValue("@Expdur_proc", txt_durprocess.Text);
com.Parameters.AddWithValue("@Place", txt_place.Text);
com.Parameters.AddWithValue("@date", dateTimePicker1.Value.ToShortDateString());

if (con.State == ConnectionState.Closed)
con.Open();
com.ExecuteNonQuery();
con.Close();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");


in my table(ms access)
id used as primary key
1)id
2)goods_desc
'
'
'12)date

What I have tried:

i used msaccess db and i find this error Number of query values and destination fields are not the same.

推荐答案

您插入的表中的字段数与参数数量不匹配,因此无法将参数与田野。为了解决这些问题总是说明你要插入哪些字段;



插入tablename(field1,field2)值(@ value1,@ value2)
The number of fields in the table you are inserting doesn't match the number of parameters so it can't match the parameters to the fields. To help with these problems always say what fields you are inserting into;

insert into tablename (field1, field2) values (@value1, @value2)


当您执行匿名INSERT操作时,您将假定数据库表中的字段计数和顺序。

When you do an anonymous INSERT operation, you are assuming the field count and order within the database table.
INSERT INTO MyTable VALUES (...)

这很糟糕,因为这意味着您的数据库中的数据更改可能无法在您的代码中获取,并且您可以获得数据库因此无可救药地腐败了。如果你有一个IDENTITY字段,那么你会收到一个错误,因为DB会尝试插入并抱怨。

总是按照你想要插入的顺序列出你的列:

That's bad, because it means that changes to your DB may not be picked up in your code, and you can get a DB that is hopelessly corrupted as a result. And if you have an IDENTITY field, then you get an error because the DB will try to insert to it and complain.
Always list your columns in the order you are trying to insert:

INSERT INTO MyTable (Col1, Col2, Col3) VALUES (@forCol1, @forCol2, @forCol3)

并且问题就消失了。

在您的特定情况下,您要插入11个值,但是DB不认为表中有那么多列...所以检查你是否使用了正确的表名!



另外,不要转换将DateTime值传递给字符串以将它们传递给数据库 - 将它们作为DateTime传递,或者当系统尝试将日期字符串转换回DateTime以插入值时,您会冒更多复杂的风险。它不知道你的日期是什么格式,所以它可能会弄错(例如)交换日期和月份。直接发送DateTime值并且该问题也不会发生。

and that problem goes away.
In your specific case, you are inserting 11 values, but the DB doesn't think there are that many columns in the table...so check you are using the right table name!

In addition, don't convert DateTime values to strings to pass them to the DB - pass them as DateTime, or you risk further complications when the system tries to convert the date string back to a DateTime to insert the value. It doesn't know what format your date is in, so it can get it wrong and (for example) swap the day and month over. Send the DateTime value directly and that problem doesn't happen either.


这篇关于发生错误查询值的数量和目标字段不相同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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