0

My Query in PHPmyadmin has result but in C#, a.read() returns no data.

string query = "SELECT answer FROM tbl WHERE level = " + level + " AND subject = '" + subject[i] + "';";

MySqlCommand command = new MySqlCommand(query, connection);
connection.Open();
MySqlDataReader a = command.ExecuteReader();
while (a.Read())
{
    //Do Some Things
}
connection.Close();

Edit: I've already tried parameterized queries, didn't work, and also tried with the constant values from the table, it doesn't work in the project, but when I run the query in the database it works

seyed
  • 71
  • 1
  • 7
  • Please debug your code, and share some more info, make your question a [mre] – Luuk Mar 07 '21 at 14:37
  • 3
    And read http://bobby-tables.com before you write any more SQL (in your life, ever) please – Caius Jard Mar 07 '21 at 14:40
  • Replace the two variables with the constant values that you have used in PHPMyAdmin and check if now you have the records. If yes then your variables are not what you supposed them to be. After fixing the problem change ASAP this code to a [parameterized query](https://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp) – Steve Mar 07 '21 at 14:44
  • Can you show the sql query that brought the data, pls? – Serge Mar 07 '21 at 14:47
  • Does this answer your question? [Parameterized Query for MySQL with C#](https://stackoverflow.com/questions/652978/parameterized-query-for-mysql-with-c-sharp) – Charlieface Mar 07 '21 at 16:23
  • I've already tried parameterized queries, didn't work, and also tried with the constant values from the table, it doesn't work in the project, but when I run the query in the database it works – seyed Mar 07 '21 at 17:42
  • Don't tell me parameterized queries don't work, they do. Show the code that does it, and preferably also some sample data for your table. Please also indicate how you know that `a.Read` returns no data, does it skip the loop, or are you checking the variables in `//Do Some Things` – Charlieface Mar 08 '21 at 02:38
  • use Sql server with ef core and resolve (clean) problem – seyed Mar 11 '21 at 10:04

0 Answers0