2016-06-13 119 views
1

我的數據庫寫入和數據庫讀取有問題。C#MySQL錯誤:不支持關鍵字

這裏是我的代碼:

MySqlConnection conn = new MySqlConnection(ConnectionString); 
string ID = ""; 
MySqlCommand cmdRegister = new MySqlCommand("Insert into Players (username,password) values('"+"John"+"', '"+"johnisbest"+"')", conn); 
cmdRegister.ExecuteNonQuery(); 
MySqlCommand cmdRead = new MySqlCommand("SELECT ID FROM Players WHERE username = '"+this.username+"';", conn); 
MySqlDataReader reader = cmdRead.ExecuteReader(); 
conn.Open(); 

while (reader.Read()) 
{ 
    ID = (string)reader["ID"]; 
} 
conn.Dispose(); 
return ID; 

它給我總是錯誤:關鍵字不支持

感謝每一個幫助;)

編輯:

public MySQL(string IPaddress, string port, string username, string password, string database) 
{ 
    ConnectionString = "datasource=" + IPaddress + ";port=" + port + ";username=" + username + ";password=" + password + ";database=" + database + ";charse=utf_8"; 
} 
sql = new MySQL("IP address to database", "3306", "username to database", "Here is my password to database", "name of my database"); 
+3

密碼是一個[保留字](https://dev.mysql.com/doc/refman/5.5/en/keywords.html)。用後面的蜱滴定它。你不應該以純文本形式存儲密碼 – Plutonix

+0

使用pwd而不是像這裏定義的方式使用密碼https://www.connectionstrings.com/mysql/ – Adrian

+1

我也建議你學會使用'參數以及string.Format ()函數與連接字符串構建連接字符串。用你的Select語句做同樣的事情不要使用連接字符串...! – MethodMan

回答

0
ConnectionString = "datasource=" + IPaddress + ";port=" + port + ";username=" + username + ";pwd=" + password + ";database=" + database + ";charse=utf_8"; 

試圖更換passw ord with「pwd」;

這對我來說做工精細

Server=localhost;Database=testing;UID=root;password=testing;connect timeout=700;charset=utf8;pooling=true;Port=3306 
+0

謝謝,但它工作。 –

+0

@Dortík你正在使用哪個版本? – Adrian

+0

我使用4.5版本 –

相關問題