2015-10-12 45 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

它说更新,但没有改变我的数据库先生,即使在数据网格 –

相关问题