2013-03-31 85 views
1

我有一個sql表中有許多字段。從c更新sql中只有一個單元#

我想只更新一個名爲「ready」的字段,其ID是從組合框中獲取的。我用下面的代碼:

string query = "Update order SET ready='true' Where id=" + int.Parse(idComboBox.SelectedItem.ToString()) + ""; 

它給出錯誤說:

列名空

我應該怎麼做,以保持的其他記錄的值相同,只改變字段值ready

+2

雖然使用動態生成的SQL「消毒」整數不是[危險](http://xkcd.com/327/),使用參數化SQL始終是一個更好的選擇,因爲它避免了反覆解析僅在ID中有所不同的查詢。 – dasblinkenlight

回答

3

ORDER是保留字。改爲使用[ORDER]

1

使用此:

string query = "Update [order] SET ready='true' Where id=" + int.Parse(idComboBox.SelectedItem.ToString()) + ""; 
2

ORDERreserved word SQL Server上。你應該用方括號[]使用它

保留關鍵字是所使用的SQL Server來解析和理解的Transact-SQL語句 和批處理的Transact-SQL語言 的語法的一部分。

string query = "Update [order] SET ready='true' Where id=" + int.Parse(idComboBox.SelectedItem.ToString()) + ""; 

但更重要的一部分,你的查詢是開放的SQL Injection攻擊。您應始終使用parameterized queries

string query = "Update [order] SET ready='true' Where id = @id"; 

SqlCommand myCommand = new SqlCommand(query, cnn); 
myCommand.Parameters.AddWithValue("@id", int.Parse(idComboBox.SelectedItem.ToString())); 
相關問題