Home All Groups Group Topic Archive Search About

Number of rows from datareader?

Author
20 Jan 2006 6:20 PM
dew
How do I get the number of rows a datareader has?  I can tell HasRows, but
not how many.

Thanks.

Author
20 Jan 2006 6:25 PM
Bruce Barker
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)


Show quote
"dew" <d**@yahoo.com> wrote in message
news:uNMZN4eHGHA.2680@TK2MSFTNGP09.phx.gbl...
> How do I get the number of rows a datareader has?  I can tell HasRows, but
> not how many.
>
> Thanks.
>
Author
20 Jan 2006 6:28 PM
Ranjan Sakalley
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.

Show quote
> How do I get the number of rows a datareader has?  I can tell HasRows,
> but not how many.
>
> Thanks.
>
Author
20 Jan 2006 7:29 PM
dew
Thanks, all, I guess I'll have to use a dataset.

Show quote
"dew" <d**@yahoo.com> wrote in message
news:uNMZN4eHGHA.2680@TK2MSFTNGP09.phx.gbl...
> How do I get the number of rows a datareader has?  I can tell HasRows, but
> not how many.
>
> Thanks.
>
Author
20 Jan 2006 8:57 PM
Peter Bromberg [C# MVP]
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




Show quote
"dew" wrote:

> How do I get the number of rows a datareader has?  I can tell HasRows, but
> not how many.
>
> Thanks.
>
>
>
Author
21 Jan 2006 12:43 AM
Bruce Barker
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)


Show quote
"Peter Bromberg [C# MVP]" <pbromberg@yahoo.nospammin.com> wrote in message
news:0AD66EA1-E4DC-422C-B146-238267C36488@microsoft.com...
> 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.
>>
>>
>>
Author
21 Jan 2006 1:09 AM
Peter Bromberg [C# MVP]
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




Show quote
"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]" <pbromberg@yahoo.nospammin.com> wrote in message
> news:0AD66EA1-E4DC-422C-B146-238267C36488@microsoft.com...
> > 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.
> >>
> >>
> >>
>
>
>

AddThis Social Bookmark Button