• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

How do I get an SqlDataReader to return the number of rows returned by a SELECT statement?

I use SqlDataReader and need to onkw how many rows were returned.  Is that possible?

thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
2 Solutions
 
JimBrandleyCommented:
If you are loading a DataTable with the reader,
int rowsReturned = myDataTable.Rows.Count;
0
 
prosh0tCommented:
not without rendering your sqldatareader object useless.  By definition, the data reader has forward only access which means that you would have to scan all the records until you hit eof, which then would tell you how many records were there.  Better ways to do it are to either
1. run a separate query with the 'count' keyword in it that returns the count of your records, or
2. instead of using an sqldatareader use a datatable object and load them all in there.  note that this means all of your data will be in memory and it willh ave much more overhead than a straight sqldatareader

good luck
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now