Get retrived records count from OleDbDataReader in C#?

user662285 picture user662285 · May 9, 2011 · Viewed 25.6k times · Source

I want to get the retrived records count from the OleDbDataReader in C# ?

strQuery = "SELECT * FROM Table_Name" ;                   
    dbCommand = new OleDbCommand(strQuery, dbConnection);
    dbReader = dbCommand.ExecuteReader();
    //Now how to get RowCount from the Table after this.

Any help is appreciated.

Thanks.

Answer

Pranay Rana picture Pranay Rana · May 9, 2011

For more detail : Get row count by 'ExecuteScalar'

Make use of ExecuteSclar() rather than going for read function.

SqlCommand cmd = new SqlCommand("SELECT count(*) FROM " + Table_Name, conn);
    try
    {
        conn.Open();
        int total = (Int32)cmd.ExecuteScalar();
    }