C#中查询值和目标字段的数量不一样错误?
Posted
技术标签:
【中文标题】C#中查询值和目标字段的数量不一样错误?【英文标题】:Number of query values and destination fields are not the same error in C#? 【发布时间】:2013-12-15 06:56:17 【问题描述】:我正在尝试将我的 textedit 值存储到 MS Access 数据库中,但出现类似“查询值和目标字段的数量不一样”的错误。但是我的查询值和目标字段是相同的。我尝试在两种方法中插入两种方法都得到相同的错误。
我先尝试了这个方法
int invoicenumber = Convert.ToInt32(TXE_Invoice_Number.Text);
string terms = CBL_Terms.Text;
DateTime date = CBL_Date.DateTime;
string ourquote = TXE_OurQuote.Text;
string salesperson = CBL_Sales_Person.Text;
string customername = CBL_Customer_Nmae.Text;
string oderno = CBL_Order_Number.Text;
string invoiceaddress = TXE_Invoice_Address.Text;
string deliveryaddress = TXE_Delivery_Address.Text;
decimal wholediscper = Convert.ToDecimal(TXE_FlatDiscountP.Text);
decimal wholediscamt = Convert.ToDecimal(TXE_FlatDiscountA.Text);
decimal shippingpercenatge = Convert.ToDecimal(TXE_ShippingPercentage.Text);
decimal shippingamount = Convert.ToDecimal(TXE_ShippingAmount.Text);
decimal unitprice = Convert.ToDecimal(TXE_SubTotal.Text);
decimal discount = Convert.ToDecimal(TXE_Discount.Text);
decimal tax = Convert.ToDecimal(TXE_Tax.Text);
decimal shiping = Convert.ToDecimal(TXE_Shipping.Text);
decimal grandtotal = Convert.ToDecimal(TXE_GrandTotal.Text);
OleDbCommand top = new OleDbCommand(
"INSERT INTO NewInvoice_1 (" +
"InvoiceNumber,Terms,[InvoiceDate],OurQuote," +
"SalesPerson,CustomerName,OrderNumber," +
"InvoiceAddress,DeliveryAddress," +
"WholeDiscountP,WholeDiscountA,ShippingP,ShippingA" +
"Price,Discount,Tax" +
"Shipping,GrandTotal" +
") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", conn);
top.Parameters.AddWithValue("?", invoicenumber);
top.Parameters.AddWithValue("?", terms);
top.Parameters.AddWithValue("?", date);
top.Parameters.AddWithValue("?", ourquote);
top.Parameters.AddWithValue("?", salesperson);
top.Parameters.AddWithValue("?", customername);
top.Parameters.AddWithValue("?", oderno);
top.Parameters.AddWithValue("?", invoiceaddress);
top.Parameters.AddWithValue("?", deliveryaddress);
top.Parameters.AddWithValue("?", wholediscper);
top.Parameters.AddWithValue("?", wholediscamt);
top.Parameters.AddWithValue("?", shippingpercenatge);
top.Parameters.AddWithValue("?", shippingamount);
top.Parameters.AddWithValue("?", unitprice);
top.Parameters.AddWithValue("?", discount);
top.Parameters.AddWithValue("?", tax);
top.Parameters.AddWithValue("?", shiping);
top.Parameters.AddWithValue("?", grandtotal);
top.ExecuteNonQuery();
第二种方法
int invoicenumber = Convert.ToInt32(TXE_Invoice_Number.Text);
string terms = CBL_Terms.Text;
DateTime date = CBL_Date.DateTime;
string ourquote = TXE_OurQuote.Text;
string salesperson = CBL_Sales_Person.Text;
string customername = CBL_Customer_Nmae.Text;
string oderno = CBL_Order_Number.Text;
string invoiceaddress = TXE_Invoice_Address.Text;
string deliveryaddress = TXE_Delivery_Address.Text;
decimal wholediscper = Convert.ToDecimal(TXE_FlatDiscountP.Text);
decimal wholediscamt = Convert.ToDecimal(TXE_FlatDiscountA.Text);
decimal shippingpercenatge = Convert.ToDecimal(TXE_ShippingPercentage.Text);
decimal shippingamount = Convert.ToDecimal(TXE_ShippingAmount.Text);
decimal unitprice = Convert.ToDecimal(TXE_SubTotal.Text);
decimal discount = Convert.ToDecimal(TXE_Discount.Text);
decimal tax = Convert.ToDecimal(TXE_Tax.Text);
decimal shiping = Convert.ToDecimal(TXE_Shipping.Text);
decimal grandtotal = Convert.ToDecimal(TXE_GrandTotal.Text);
OleDbCommand top = new OleDbCommand("INSERT INTO test_top(InvoiceNumber,Terms,[InvoiceDate],OurQuote,SalesPerson,CustomerName,OrderNumber,InvoiceAddress,DeliveryAddress,WholeDiscountP,WholeDiscountA,ShippingP,ShippingA,Price,Discount,Tax,Shipping,GrandTotal) VALUES (" + invoicenumber + ",'" + terms + "','" + date + "','" + ourquote + "','" + salesperson + "','" + customername + "','" + oderno + "','" + invoiceaddress + "','" + deliveryaddress + "',"+ wholediscper +","+ wholediscamt +","+ shippingpercenatge +","+ shippingamount +"," + unitprice + "," + tax + "," + grandtotal + ")", conn);
在这两种方法中得到相同的错误?我的代码有什么问题?帮帮我
【问题讨论】:
【参考方案1】:您在“ShippingA”之后缺少逗号
"WholeDiscountP,WholeDiscountA,ShippingP,ShippingA".
因此,尽管您的列名正确,但列数却减少了 1。
【讨论】:
非常感谢它现在工作正常。第二种方法的错误是什么? 第二个查询中缺少折扣变量 另外,在查询中建议使用第一种涉及参数的方法。以上是关于C#中查询值和目标字段的数量不一样错误?的主要内容,如果未能解决你的问题,请参考以下文章
查询值和目标字段的数量不同 - C# 脚本任务 SSIS - 使用动态列将 SQL Proc 的结果导出到 Excel