2015-10-12 58 views
-5

我怎樣才能擺脫這個「查詢表達式中的字符串中的語法錯誤」2333「erorr?更新語句的正確查詢是什麼?

 con.Open(); 
     OleDbCommand dt = new OleDbCommand("UPDATE AccRec SET Quantity=" + txtQuantity2.Text + ", Unit=" + txtUnit2.Text + " ,Company=" + txtCompany2.Text + ", Description=" + txtDesc2.Text + ", Amount='" + txtAmt2.Text + " Where No=" + textBox1.Text +"",con);Where No=5'. 


     dt.ExecuteNonQuery(); 
     MessageBox.Show("updated"); 
     OleDbDataAdapter da = new OleDbDataAdapter("SELECT * From AccRec ", con); 
     DataTable ds = new DataTable(); 
     da.Fill(ds); 
     dataGridView2.DataSource = ds; 
     con.Close(); 
+0

看到'如果沒有= 5''?你不能這樣做。 –

+4

[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該**不**連接你的SQL語句 - 使用**參數化查詢**,而不是爲了避免SQL注入 –

+0

[從SELECT使用SQL Server更新]的可能重複(http://stackoverflow.com/questions/2334712/update-from-select-using-sql-server) –

回答

0

就試試這個代碼

con.Open(); 
OleDbCommand dt = new OleDbCommand("UPDATE AccRec SET [email protected], [email protected] ,[email protected], [email protected], [email protected] Where [email protected]",con); 

dt.Parameters.Add("@P1", SqlDbType.VarChar); 
dt.Parameters["@P1"].Value = txtQuantity2.Text ; 
dt.Parameters.Add("@P2", SqlDbType.VarChar); 
dt.Parameters["@P2"].Value = txtUnit2.Text; 
dt.Parameters.Add("@P3", SqlDbType.VarChar); 
dt.Parameters["@P3"].Value = txtCompany2.Text; 
dt.Parameters.Add("@P4", SqlDbType.VarChar); 
dt.Parameters["@P4"].Value = txtDesc2.Text ; 
dt.Parameters.Add("@P5", SqlDbType.VarChar); 
dt.Parameters["@P5"].Value = txtAmt2.Text ; 
dt.Parameters.Add("@P6", SqlDbType.VarChar); 
dt.Parameters["@P6"].Value = textBox1.Text; 

dt.ExecuteNonQuery(); 

MessageBox.Show("updated"); 
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * From AccRec ", con); 
DataTable ds = new DataTable(); 
da.Fill(ds); 
dataGridView2.DataSource = ds; 
con.Close(); 
+0

@ James-DeanLorenzoAlimboyogue我剛添加了一個示例代碼,我沒有確切的數據庫。所以你可以引用我的代碼來解決你的問題。 –

+0

它說更新,但沒有改變我的數據庫先生,即使在數據網格 –

相關問題