I wrote a general method to search if a value exists in a specific table, the method is really simple:
public bool CheckRecordExist(int id, string tableName, string column)
{
string connection = string.Format("Server=localhost; database={0}; UID=root; password=root; SslMode=none; CharSet=utf8;", "myDb");
MySqlConnection conn = new MySqlConnection(connection);
string query = "SELECT * FROM " + tableName + " WHERE " + column " = @" + column;
MySqlCommand = new MySqlCommand(query, conn);
MySqlCommand.Parameters.AddWithValue("@" + column, id);
return Convert.ToBoolean(MySqlCommand.ExecuteScalar());
}
Suppose I want check if the record with id 2538782
exist on the table match, I do:
CheckRecordExist(2538782, "match", "id");
so the query
variable will be:
SELECT * FROM match WHERE id = @id
if I run this in PhpMyAdmin
, replacing the id
with the value, I'll get the correct response, but inside my app I'll get:
MySql.Data.MySqlClient.MySqlException: '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 'match WHERE id = 2538782' at line 1'
what is wrong?