我的數據庫寫入和數據庫讀取有問題。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");
密碼是一個[保留字](https://dev.mysql.com/doc/refman/5.5/en/keywords.html)。用後面的蜱滴定它。你不應該以純文本形式存儲密碼 – Plutonix
使用pwd而不是像這裏定義的方式使用密碼https://www.connectionstrings.com/mysql/ – Adrian
我也建議你學會使用'參數以及string.Format ()函數與連接字符串構建連接字符串。用你的Select語句做同樣的事情不要使用連接字符串...! – MethodMan