Home > DeveloperSection > Forums > How to get number of rows in a table in mysql
F Scott Fitzgerald
F Scott Fitzgerald

Total Post:29

Points:203
Posted on    October-05-2013 7:39 AM

 C# C# 
Ratings:


 1 Reply(s)
 870  View(s)
Rate this:

After sleepless night and run over the stackoverflow I could not find the method (function) that can resolve my probleme.

What I wrote is very harder for the server. Is there any more clever way to resolve this problem?

Here my code:

public static int GetIndexLong(string TblName)
{
    int rowsNbr = 0;
    using(MySqlConnection conn = new MySqlConnection(PublicVariables.cs))
    {
        using(MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + TblName, conn))
        {
            conn.Open();
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    ++rowsNbr;
                }
            }
            return rowsNbr;
        }
    }
}

It does the job but I know it's not the good way.

Working with C# Express 2008 on .NET 4.0.



Ida B Wells
Ida B Wells

Total Post:30

Points:210
Posted on    October-05-2013 8:58 AM

Are you looking for count:

SELECT COUNT(*) FROM TableName

Don't want to miss updates? Please click the below button!

Follow MindStick