2014-04-09 77 views
-2

AM試圖更新該查詢中的日期我在UPDATE語句收到此錯誤語法錯誤,這是我的代碼查詢表達式中的語法錯誤(缺少運算符)'05 -04-2014 AM 12:00:00'C#?

Updated Code

OleDbCommand top = new OleDbCommand(
     "UPDATE NewInvoice_1 SET (" + 
     "Terms=?, [InvoiceDate]=?, OurQuote=?," + 
     "SalesPerson=?, CustomerName=?, OrderNumber=?," + 
     "InvoiceAddress=?, DeliveryAddress=?," + 
     "WholeDiscountP=?, WholeDiscountA=?, ShippingP=?, ShippingA=?," + 
     "Price=?, Discount=?, Tax=?," + 
     "Shipping=?, GrandTotal=?, TaxforDisc=?, DiscountType=?," + 
     "ShippingBy=?, ShipReferenceNo=?, IsInsured=?, Notes=?," + 
     "[DueDate]=?, AmountinWords=?) WHERE InvoiceId=?", conn); 


     top.Parameters.AddWithValue("?", CBL_Terms.EditValue.ToString()); 
     top.Parameters.AddWithValue("?", CBL_Date.DateTime); 
     top.Parameters.AddWithValue("?", TXE_OurQuote.Text); 
     top.Parameters.AddWithValue("?", CBL_Sales_Person.EditValue.ToString()); 
     top.Parameters.AddWithValue("?", CBL_Customer_Name.EditValue.ToString()); 
     top.Parameters.AddWithValue("?", TXE_Order_Number.Text); 
     top.Parameters.AddWithValue("?", TXE_Invoice_Address.Text); 
     top.Parameters.AddWithValue("?", TXE_Delivery_Address.Text); 

     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_FlatDiscountP.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_FlatDiscountA.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_ShippingPercentage.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_ShippingAmount.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_SubTotal.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_Discount.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_Tax.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_Shipping.Text)); 
     top.Parameters.AddWithValue("?", Convert.ToDecimal(TXE_GrandTotal.Text)); 

     top.Parameters.AddWithValue("?", barCheckItem1.Checked); 
     top.Parameters.AddWithValue("?", selectedItem); 

     top.Parameters.AddWithValue("?", TXE_Shipping_By.Text); 
     top.Parameters.AddWithValue("?", TXE_Reference_No.Text); 
     top.Parameters.AddWithValue("?", CBX_Is_Insured.Checked); 
     top.Parameters.AddWithValue("?", TXE_Notes.Text); 

     top.Parameters.AddWithValue("?", CBL_DueDate.DateTime); 
     top.Parameters.AddWithValue("?", TXE_AmountinWords.Text); 
     top.Parameters.AddWithValue("?", TXE_Unvisible.Text); 
top.ExecuteNonQuery(); 

現在更新我的代碼參數,但是在得到語法錯誤更新代碼

+0

爲什麼不使用參數化查詢? –

+4

認真,你在做這個嗎? –

+0

文本和日期時間值必須是用引號括起來的字符串文字。但嚴重的是,不要這樣做。使用參數化查詢。 [這使您可以開放SQL注入](http://xkcd.com/327/) –

回答

0

使用參數。 放?爲每個參數值。添加參數值時使用相同的順序。

string sql = "UPDATE NewInvoice_1 SET Terms =?, [InvoiceDate]=?, OurQuote=? ... WHERE InvoiceId=?"; 

      using (var command = conn.CreateCommand()) 
      { 
       command.CommandText = sql; 
       command.Parameters.Add(CBL_Terms.EditValue.ToString()); 
       command.Parameters.Add(CBL_Date.DateTime); 
       command.Parameters.Add(TXE_OurQuote.Text); 

       //add all other fields keeping order 

       command.ExecuteNonQuery(); 
      } 

備註 - 您不應該在表單中執行或保留查詢。 此外,如果您使用適配器,則可以使用以下語法來添加參數:

command.Parameters.Add(adapter.Parameter(value)); 
+0

誰說這是關於MySQL的? –

+0

沒有人說這是不是:) –

+0

嗨@AntonGorlin我更新了我的問題,請驗證 – Sri

相關問題