Getting MySQL record count with C#

she hates me picture she hates me · May 11, 2010 · Viewed 30.8k times · Source

I would like to know how can I get record count of a query with C#.

Here is the code that I use..

    MySqlDataReader recordset = null;
    query = new MySqlCommand("SELECT * FROM test ORDER BY type_ID ASC", this.conn);
    recordset = query.ExecuteReader();


    while (recordset.Read())
    {
        result.Add(recordset["type_ID"].ToString());

    }
    return result;

Answer

Trevorm picture Trevorm · Jan 25, 2012

I was using a SELECT COUNT(*) and expected an int to be returned. You may need this to get a usable value:

mysqlint = int.Parse(query.ExecuteScalar().ToString());