Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Number of rows from datareader?

Reply
Thread Tools

Number of rows from datareader?

 
 
dew
Guest
Posts: n/a
 
      01-20-2006
How do I get the number of rows a datareader has? I can tell HasRows, but
not how many.

Thanks.


 
Reply With Quote
 
 
 
 
Bruce Barker
Guest
Posts: n/a
 
      01-20-2006
you need to read read to the end with a counter.

a datareader (at least for sqlserver) is a forward only cursor returning
data from the response stream buffer. thus you can not lnow how many rows
there are until you read to the end. on a big query you can read rows while
the server is still performing the query.

this is also why you can not read return paramater from a sp until you've
read all the result sets.

-- bruce (sqlwork.com)


"dew" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> How do I get the number of rows a datareader has? I can tell HasRows, but
> not how many.
>
> Thanks.
>



 
Reply With Quote
 
 
 
 
Ranjan Sakalley
Guest
Posts: n/a
 
      01-20-2006
Hello dew,

No way other than iterating through the reader. Sometimes the overhead of
doing this is so much that I have seen people running a count query too,
before/after Read() ing the data, if needed.

You might want to post this question in microsoft.public.dotnet.framework.adonet.

HTH,
r.

> How do I get the number of rows a datareader has? I can tell HasRows,
> but not how many.
>
> Thanks.
>



 
Reply With Quote
 
dew
Guest
Posts: n/a
 
      01-20-2006
Thanks, all, I guess I'll have to use a dataset.

"dew" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> How do I get the number of rows a datareader has? I can tell HasRows, but
> not how many.
>
> Thanks.
>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      01-20-2006
As one poster has already, commented, you can still use a DataReader. Just
populate it with 2 resultsets, the first one being the count of rows returns.
then use the DataReader's NextResultSet() method to switch to the one
containing all the rows.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"dew" wrote:

> How do I get the number of rows a datareader has? I can tell HasRows, but
> not how many.
>
> Thanks.
>
>
>

 
Reply With Quote
 
Bruce Barker
Guest
Posts: n/a
 
      01-21-2006
this is really a bad pattern. it requires running the query twice, and if a
row is inserted/deleted between queries, the wrong answer can appear. to
make the query stable you have to use exclusive lock, or select into a temp
table.

-- bruce (sqlwork.com)


"Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> As one poster has already, commented, you can still use a DataReader. Just
> populate it with 2 resultsets, the first one being the count of rows
> returns.
> then use the DataReader's NextResultSet() method to switch to the one
> containing all the rows.
> Peter
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "dew" wrote:
>
>> How do I get the number of rows a datareader has? I can tell HasRows,
>> but
>> not how many.
>>
>> Thanks.
>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      01-21-2006
Good point, Bruce, if that precision is critical to the business logic
scenario.
But then, doing so would make it a "good pattern"?
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Bruce Barker" wrote:

> this is really a bad pattern. it requires running the query twice, and if a
> row is inserted/deleted between queries, the wrong answer can appear. to
> make the query stable you have to use exclusive lock, or select into a temp
> table.
>
> -- bruce (sqlwork.com)
>
>
> "Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > As one poster has already, commented, you can still use a DataReader. Just
> > populate it with 2 resultsets, the first one being the count of rows
> > returns.
> > then use the DataReader's NextResultSet() method to switch to the one
> > containing all the rows.
> > Peter
> >
> > --
> > Co-founder, Eggheadcafe.com developer portal:
> > http://www.eggheadcafe.com
> > UnBlog:
> > http://petesbloggerama.blogspot.com
> >
> >
> >
> >
> > "dew" wrote:
> >
> >> How do I get the number of rows a datareader has? I can tell HasRows,
> >> but
> >> not how many.
> >>
> >> Thanks.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide the Gridview Rows and Header rows in Button click VijayRama ASP .Net 2 10-12-2009 01:58 PM
Rows the rows of a grid view? Jason James ASP .Net 0 08-10-2006 09:24 PM
dynamically add new rows and save data typed in these rows to a database Arjen Hoekstra ASP .Net 0 08-02-2005 12:15 PM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM
script for moving rows up and down and traverse thru rows of HTML table Subba Rao via DotNetMonster.com ASP .Net 0 03-19-2005 06:46 AM



Advertisments