Row count from OracleDataReader

Pradeep picture Pradeep · Oct 18, 2010 · Viewed 20.4k times · Source

Can any one tell me how to find row count from OracleDataReader in .net 2.0?

Answer

Alex picture Alex · Feb 25, 2011

An OracleDataReaderobject represents a forward-only, read-only, in-memory result set. Unlike the DataSet, the OracleDataReaderobject stays connected and fetches one row at a time.

So, it does not know how many rows there will be. If you use the data adapter, then you will be able to get a row count since it fetches the rows.

In your case you have to fetch all the rows (if you need to fetch the data only) to get the row count:

OracleDataReader reader = cmd.ExecuteReader();
int rowCount = 0;
while (reader.Read())
{
    // your logic here
    rowCount++;
}

But if you don't need that data, it would be better to reformulate your stored procedure/query to return row count explicitly.