Number of rows from datareader?

Discussion in 'ASP .Net' started by dew, Jan 20, 2006.

  1. dew

    dew Guest

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

    Thanks.
     
    dew, Jan 20, 2006
    #1
    1. Advertising

  2. dew

    Bruce Barker Guest

    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" <> wrote in message
    news:...
    > How do I get the number of rows a datareader has? I can tell HasRows, but
    > not how many.
    >
    > Thanks.
    >
     
    Bruce Barker, Jan 20, 2006
    #2
    1. Advertising

  3. 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.
    >
     
    Ranjan Sakalley, Jan 20, 2006
    #3
  4. dew

    dew Guest

    Thanks, all, I guess I'll have to use a dataset.

    "dew" <> wrote in message
    news:...
    > How do I get the number of rows a datareader has? I can tell HasRows, but
    > not how many.
    >
    > Thanks.
    >
     
    dew, Jan 20, 2006
    #4
  5. 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.
    >
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 20, 2006
    #5
  6. dew

    Bruce Barker Guest

    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]" <> wrote in message
    news:...
    > 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.
    >>
    >>
    >>
     
    Bruce Barker, Jan 21, 2006
    #6
  7. 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]" <> wrote in message
    > news:...
    > > 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.
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jan 21, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacko
    Replies:
    8
    Views:
    587
  2. JC

    Missing rows in DataReader

    JC, Jun 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    408
  3. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,238
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  4. helpful sql
    Replies:
    0
    Views:
    815
    helpful sql
    May 19, 2005
  5. Mattyw
    Replies:
    2
    Views:
    2,909
    Mattyw
    Sep 12, 2005
Loading...

Share This Page