Rowcount of a UniDataReader Result

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for universal data access
Post Reply
magixxfactory
Posts: 11
Joined: Fri 27 Aug 2010 18:24

Rowcount of a UniDataReader Result

Post by magixxfactory » Fri 21 Jul 2017 10:54

Hello,
How can I got the count of rows returned by a UniDataReader ?
Thanks and regards
Uwe

Pinturiccio
Devart Team
Posts: 2192
Joined: Wed 02 Nov 2011 09:44

Re: Rowcount of a UniDataReader Result

Post by Pinturiccio » Tue 25 Jul 2017 14:34

A DataReader, be it UniDataReader or SqlDataReader, is not designed to get row counts. Data is read row by row, and the number of rows in the DataReader is unknown.

There are different ways to get the number of rows. Here are the two most popular and recommended ones:
1. Execute the Read method in the while cycle and increment a counter:

Code: Select all

while (reader.Read())
{
    counter++;
}
2. Load the reader to a DataTable, and then get the number of rows from the DataTable:

Code: Select all

using (DataTable dt = new DataTable())
{
    dt.Load(reader);
    Console.WriteLine(dt.Rows.Count);
}
You can also execute the SELECT COUNT(*) query, which returns the number of rows, before reading the DataReader.

Post Reply