C#MySQL错误:不支持关键字

时间:2016-06-13 15:48:01

标签: 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");

1 个答案:

答案 0 :(得分:0)

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

尝试用“pwd”替换密码;

对我来说这项工作很好

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