SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 datareader record count.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

subhaoviya
Posting Yak Master

India
135 Posts

Posted - 09/07/2012 :  07:02:27  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/07/2012 :  07:10:54  Show Profile  Reply with Quote
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

India
135 Posts

Posted - 09/07/2012 :  09:49:40  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 09/07/2012 :  09:58:36  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

397 Posts

Posted - 09/08/2012 :  07:05:48  Show Profile  Reply with Quote
What sunitabeck said

or using SqlDataAdaptor/DataSet/DataTable
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000