I'm trying to get a user from mysql table with a username instead of id in ASP.Net Core, the default column in GET method is id. I tried to change it to username, but the column is unknown.
In my table, username, password and email are in TEXT type, id in INT type.
My changed code is:
// GET: api/Person/XXX
[HttpGet("{uname}", Name = "Get")]
public Person Get(string uname)
{
ConnectMysql();
Person p = new Person();
string queryString = "SELECT * FROM users WHERE uname = " + uname;
MySqlCommand cmd = new MySqlCommand(queryString, conn);
MySqlDataReader myReader = cmd.ExecuteReader();
if (myReader.Read())
{
p.id = (int)myReader["id"];
p.name = myReader["uname"].ToString();
p.password = myReader["pword"].ToString();
p.email = myReader["email"].ToString();
return p;
}
else
{
return null;
}
}
And then I try to get an item in table with calling http://localhost:54203/api/Person/q ('q' is an item exists in the table).
The error is MySql.Data.MySqlException:Unknown column 'q' in 'where clause'
How can I fix it?