How to know total records selected?

A

anand

Hello Group,
How can i obtain the total no of records selected with the help of
Datareader class. rowsaffected does not seem to work.

Thanks

Anand
 
P

Patrice

A DataReader reads data as your code proceeds in the reading loop. Because
of this, you can't know how much rows are returned until they are all
processed.

If you don't need the number of rows before reading them :
- count the rows in the reading loop

If you need this number before reading the rows
- issue a separate select count(*) statement to return the number of rows

(The ado.net group should be enough)

-
 
W

William Ryan eMVP

anand said:
Hello Group,
How can i obtain the total no of records selected with the help of
Datareader class. rowsaffected does not seem to work.

Rowsafftected doesn't show you this. A reader doesn't know this number
until the processing has been done, remember that it's a streambased
mechanism. As such you'll either have to use a SELECT COUNT(*) statement
before the real sql statement or you'll have to walk through the reader
while(rdr.Read()){ i++;} The first method puts twice the stress on the db
and is really not efficient. You can also consider using an Output
parameter.

Similarly, you can get this using a dataatable using the Rows.Count
property.
Thanks

Anand

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
A

anand

Thanks a lot...

Salute
Anand
William Ryan eMVP said:
Rowsafftected doesn't show you this. A reader doesn't know this number
until the processing has been done, remember that it's a streambased
mechanism. As such you'll either have to use a SELECT COUNT(*) statement
before the real sql statement or you'll have to walk through the reader
while(rdr.Read()){ i++;} The first method puts twice the stress on the db
and is really not efficient. You can also consider using an Output
parameter.

Similarly, you can get this using a dataatable using the Rows.Count
property.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
A

anand

Thanks alot

Salute
Anand :)
Patrice said:
A DataReader reads data as your code proceeds in the reading loop. Because
of this, you can't know how much rows are returned until they are all
processed.

If you don't need the number of rows before reading them :
- count the rows in the reading loop

If you need this number before reading the rows
- issue a separate select count(*) statement to return the number of rows

(The ado.net group should be enough)

-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top