查询值和目标字段的数量在C#中不是相同的错误? [英] Number of query values and destination fields are not the same error in C# ?

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

问题描述

我正在尝试将我的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 +
价格,折扣,税 +
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( ,折扣);
top.Parameters.AddWithValue( ,tax);
top.Parameters.AddWithValue( ,发货);
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 + ',' + myquote + ',' + salesperson + ',' + customername + < span class =code-string>',' + oderno + ',' + invoiceaddres s + ',' + deliveryaddress + ', + wholediscper + + wholediscamt + + shippingpercenatge + + shippingamount + + unitprice + + tax + + grandtotal + ,conn);





两种方法都出现同样的错误?我的代码有什么问题?帮帮我

解决方案

折扣发货值在第二种方法中缺少。

再次尝试添加它们。


首先是错误的,因为你需要在查询中和添加值时命名参数。 br />
第二个是正确的,除了你为insert语句定义了2个额外的列而没有传递它的值。

第一个缺失的值是Discount&第二个是运费。



所以,正确的代码会有点像



 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 + < span class =code-string>  + shippingpercenatge +   + shippingamount +   + unitprice +   + discount +   + tax +   + shipping +   + grandtotal +  ,conn); 


因为这里

 OleDbCommand top = new OleDbCommand(
INSERT INTO NewInvoice_1(+
InvoiceNumber,Terms,[InvoiceDate],OurQuote,+
SalesPerson,CustomerName,OrderNumber,+
InvoiceAddress, DeliveryAddress,+
WholeDiscountP,WholeDiscountA,ShippingP,ShippingA+
价格,折扣,税+
运费,GrandTotal+
)VALUES(?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?),conn);





你写了17个值并传递和传递了18个值。根据我的说法,ShippingA,Price应该写成ShippingA+Price。



如果您的查询得到解答,请点击接受解决方案。


Hi, am trying to store my textedit valued to MS Access Database but am getting error like this "Number of query values and destination fields are not the same". But my query values and destination fields are same. I try to inserting in two methods both method getting same error.

I tried this method first

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



Second method

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



In Both method getting same error ? Whats wrong with my code ? Help me

解决方案

The discount and shiping values are missing in second method.
Try again by adding them.


First is wrong as you need to name the parameters in query and when adding the values.
Second is correct except that you are defined 2 additional column for insert statement and not passing it's value.
First missing value is for Discount & Second is of Shipping.

So, correct code would be some thing like

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 + "," + discount + "," +tax + "," + shipping + "," + grandtotal + ")", conn);


its because here

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



you have written 17 values and passing and passing 18 values. According to me "ShippingA,Price" should be written instead of "ShippingA" + "Price".

If your query is answered please click accept solution.


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

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