Record Count from Data Reader

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How to find the Record count from the Data Reader (ADO.Net) is there any way
to do it without looping thru the records ? Please let me know.

Thanks,
RKNET
 
if your TSQL has changed, inserted or deleted then you can use
dr.RecordsAffected.
If just a select statement then loop
why not do a
cmd.ExecuteNonQuery; return number of rows affected.

or get a datatable and the rest is history for you.

SA
 
How to find the Record count from the Data Reader (ADO.Net) is there any
way
to do it without looping thru the records ? Please let me know.

DataReader is streaming the data in, so by design it can't know how many
records it's grabbing until you're done looping. Normally, if you need a
count before the loop (like say to update a progress bar), do a "SELECT
COUNT(*) FROM your_table WHERE your_where_clause" to get a count of records
that your DataReader will be returning. In *most* cases, this is sufficient
and not taxing on your server.

-BKN
 
Also you may

get a datareader with two result sets
the first one has your Row Count

select count(*) from youtable where etc..
select * from yourtable where etc...

reader.read() - your Row Count -- gets you the count
dr.NextResult -- gets you the select * from ...

loop

Reader.read()

end loop

SA
 
Back
Top