How to know total records selected?

  • Thread starter Thread starter anand
  • Start date Start date
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
 
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)

-
 
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/
 
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/
 
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)

-
 
Back
Top