I have a WinForms project in C# that connects to a MySQL database. I’m trying to count the number of rows in the result, i can’t use RecordsAffected as this property only has the correct value after the read is complete and as you can see from the code below I need it before. Any idea’s on how to do this?
string sql = "SELECT * FROM Tigers WHERE Link='" + link + "'";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
if (rdr.RecordsAffected == 0)
{
//can't find in db
}
else
{
//found at least 1 result
}
You can use
HasRowsinstead :Edit : if you don’t need to read the data you retrieve from the database and just want the count you could use :