Is there any way to get the total number of rows returned from a SQL query (from the IDataReader) before iterating through the rows by using reader.Read();
?
No.
IDataReader
is a simple forward-only view of a resultset; it cannot get a count.
No, the datareader will not return a count first. However, if you do need to do it, use two queries that return multiple result sets.
for example in SQL Server:
sql = "SELECT COUNT(1) FROM A; SELECT * FROM A;"
Iterate the result sets. Use the IDataReader
on the second result set.
The database server should be able to do this quite fast if it is using appropriate indexes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With