Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 datareader record count.

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2012-09-07 : 07:02:27
Hi,

Is it possible to use the datareader resultset more than a time?
if record exists then only i need to loop through records.
currently i use reader=cmd.Executereader 2 time one time for getting count then for processing.

thanks
Subha

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-07 : 07:10:54
You can use the HasRows property to check if there are any rows in the reader. There is an example on this MSDN page: http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.80).aspx
Go to Top of Page

subhaoviya
Posting Yak Master

135 Posts

Posted - 2012-09-07 : 09:49:40
i know about it, but i need exact count because based on that i am performing some calculation.

thanks
subha
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-07 : 09:58:36
As far as I know DataReader does not provide the count information (other than of course, by reading it as you are doing). DataReader is a forward only reader, so you cannot go back and pick up data you have already read either.

Your options would be:
a) To read the data into a list/array in your .net code and process the list to get the count and to do any other processing.
b) Add a parameter to your query to return the count from T-SQL.
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-09-08 : 07:05:48
What sunitabeck said

or using SqlDataAdaptor/DataSet/DataTable
Go to Top of Page
   

- Advertisement -