2016-12-04 87 views
0

嘿傢伙我差不多完成了我的數據庫項目的CRUD項目。我只是想完成並完成刪除功能。刪除查詢異常被稱爲C#

query = string.Format("DELETE FROM customers WHERE `cid`= {0};", mDeleteTextBox); 

我的變量mDeleteTextBox填充了我想要的值。 我的查詢出了什麼問題?

錯誤信息

An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll 

Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.TextBox, Text: 6' at line 1 
+0

的可能的複製[如何逃避作爲列名的保留字? MySQL/Create Table](http://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table) – Eris

+0

另外,你的'{ 0}'需要引號,或者更好的是使用參數化查詢:http://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp – Eris

+1

另外,檢查在mDeleteTextBox類型是字符串。也許你想寫mDeleteTextBox.Text? Nigrimmist – Nigrimmist

回答

4

你更多的信息說,這一切:你想通過mTextBox作爲查詢參數,但爲了訪問文本框本身的內容(這是該數據你想用來完成你的查詢),你應該訪問文本框的Text屬性。

所以,你的代碼:

query = string.Format("DELETE FROM customers WHERE `cid`= {0};", mDeleteTextBox); 

成爲

query = string.Format("DELETE FROM customers WHERE `cid`= {0};", mDeleteTextBox.Text); 
+0

更好的答案會告訴OP如何使用參數,並永遠不會構建像這樣的SQL語句。這是開放的SQL注入。 – GuidoG