2013-12-15 29 views
0

正在嘗試將我的textedit存儲到MS Access數據庫,但遇到像這樣的錯誤「查詢值和目標字段的數量不相同」。但我的查詢值和目標字段是相同的。我嘗試在兩個方法中插入兩個方法獲得相同的錯誤。在C#中查詢值和目標字段的數量不是相同的錯誤?

這個方法我試過第一

 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

您已在該行

"WholeDiscountP,WholeDiscountA,ShippingP,ShippingA". 

「ShippingA」後缺少逗號因此,當你的列名是否正確,列數是關閉的一個。

+0

現在很感謝它的正常工作。第二種方法的錯誤是什麼? – Srihari

+1

第二個查詢中缺少折扣變量 –

+0

此外,建議您使用涉及查詢中參數的第一個方法。 –

相關問題