Get the number of Rows returned by a OleDbDataReader ASP.NET (VB)

Wassim AZIRAR picture Wassim AZIRAR · Jun 18, 2010 · Viewed 21.4k times · Source

After connecting to a database using DataReader, how can I count the number of rows ?

Thanks.

Answer

Kevin LaBranche picture Kevin LaBranche · Jun 18, 2010

Data readers are forward only so they don't have the count when first filled. You can do several things to address this:

  1. Run a separate command to get the count OR using NextResult to help instead of a totally separate command).
  2. Loop through the results and count the records
  3. Use a DataSet

Here's an example of #1:

Without NextResult: http://www.devx.com/vb2themax/Tip/18807

With NextResult (Doesn't return record count but gives you idea of how to use NextResult): http://bytes.com/topic/asp-net/answers/295793-datareader-nextresults-question

Here's an example of #2: http://support.microsoft.com/kb/308050