Home All Groups Group Topic Archive Search About
Author
4 Nov 2005 9:16 PM
David Thielen
Hi;

Is there a way to get the number of rows a DbDataReader has?

--
thanks - dave

Author
5 Nov 2005 2:48 AM
Kevin Yu [MSFT]
Hi dave

As far as I know, we cannot get the row count for a DataReader. Because
when you open the data reader, a cursor is open the on the server, it
doesn't provide the row count information. You can try to use COUNT in the
SQL statement to get the row count first.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Author
5 Nov 2005 3:28 AM
David Thielen
Hi;

That's what I figured but it never hurts to ask. I can't do a count because
I don't control the select.

Oh well, just makes my coding a little harder.

--
thanks - dave


Show quote
"Kevin Yu [MSFT]" wrote:

> Hi dave
>
> As far as I know, we cannot get the row count for a DataReader. Because
> when you open the data reader, a cursor is open the on the server, it
> doesn't provide the row count information. You can try to use COUNT in the
> SQL statement to get the row count first.
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
>
Author
5 Nov 2005 7:31 AM
CT
You can use the HasRows proeprty to check if there are any more rows. It
might not be what you want, but you can do this without movinf the cursor,
which is what the Read method does.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

Show quote
"David Thielen" <thielen@nospam.nospam> wrote in message
news:5639901C-363D-436D-807E-A4A57256CFD7@microsoft.com...
> Hi;
>
> That's what I figured but it never hurts to ask. I can't do a count
> because
> I don't control the select.
>
> Oh well, just makes my coding a little harder.
>
> --
> thanks - dave
>
>
> "Kevin Yu [MSFT]" wrote:
>
>> Hi dave
>>
>> As far as I know, we cannot get the row count for a DataReader. Because
>> when you open the data reader, a cursor is open the on the server, it
>> doesn't provide the row count information. You can try to use COUNT in
>> the
>> SQL statement to get the row count first.
>>
>> Kevin Yu
>> =======
>> "This posting is provided "AS IS" with no warranties, and confers no
>> rights."
>>
>>
Author
5 Nov 2005 3:27 PM
David Thielen
The way I read the docs for HasRows is it tells you if you have any from the
select, but not if you have any more? Are you sure this works for all
DbConnection drivers?

--
thanks - dave


Show quote
"CT" wrote:

> You can use the HasRows proeprty to check if there are any more rows. It
> might not be what you want, but you can do this without movinf the cursor,
> which is what the Read method does.
>
> --
> Carsten Thomsen
> Communities - http://community.integratedsolutions.dk
>
> "David Thielen" <thielen@nospam.nospam> wrote in message
> news:5639901C-363D-436D-807E-A4A57256CFD7@microsoft.com...
> > Hi;
> >
> > That's what I figured but it never hurts to ask. I can't do a count
> > because
> > I don't control the select.
> >
> > Oh well, just makes my coding a little harder.
> >
> > --
> > thanks - dave
> >
> >
> > "Kevin Yu [MSFT]" wrote:
> >
> >> Hi dave
> >>
> >> As far as I know, we cannot get the row count for a DataReader. Because
> >> when you open the data reader, a cursor is open the on the server, it
> >> doesn't provide the row count information. You can try to use COUNT in
> >> the
> >> SQL statement to get the row count first.
> >>
> >> Kevin Yu
> >> =======
> >> "This posting is provided "AS IS" with no warranties, and confers no
> >> rights."
> >>
> >>
>
>
>
Author
6 Nov 2005 11:48 AM
CT
Correct, it will only tell you if there is one ir more rows in the
DataReader. It is generally used before trying to use the Read method to
check if there are any rows. I suppose that you could do a ExecuteScalar to
get the count along these lines:

        Dim rowCount As Integer
        Dim rowCountSQL As String = "SELECT COUNT(*) FROM tablename"

        Dim yourConnection As New SqlConnection("...")
        Dim yourCommand As New SqlCommand(rowCountSQL)

        rowCount = CInt(yourCommand.ExecuteScalar())


--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

Show quote
"David Thielen" <thielen@nospam.nospam> wrote in message
news:2DC0EE25-DC8E-4FC5-B331-98EF6334BAE4@microsoft.com...
> The way I read the docs for HasRows is it tells you if you have any from
> the
> select, but not if you have any more? Are you sure this works for all
> DbConnection drivers?
>
> --
> thanks - dave
>
>
> "CT" wrote:
>
>> You can use the HasRows proeprty to check if there are any more rows. It
>> might not be what you want, but you can do this without movinf the
>> cursor,
>> which is what the Read method does.
>>
>> --
>> Carsten Thomsen
>> Communities - http://community.integratedsolutions.dk
>>
>> "David Thielen" <thielen@nospam.nospam> wrote in message
>> news:5639901C-363D-436D-807E-A4A57256CFD7@microsoft.com...
>> > Hi;
>> >
>> > That's what I figured but it never hurts to ask. I can't do a count
>> > because
>> > I don't control the select.
>> >
>> > Oh well, just makes my coding a little harder.
>> >
>> > --
>> > thanks - dave
>> >
>> >
>> > "Kevin Yu [MSFT]" wrote:
>> >
>> >> Hi dave
>> >>
>> >> As far as I know, we cannot get the row count for a DataReader.
>> >> Because
>> >> when you open the data reader, a cursor is open the on the server, it
>> >> doesn't provide the row count information. You can try to use COUNT in
>> >> the
>> >> SQL statement to get the row count first.
>> >>
>> >> Kevin Yu
>> >> =======
>> >> "This posting is provided "AS IS" with no warranties, and confers no
>> >> rights."
>> >>
>> >>
>>
>>
>>
Author
6 Nov 2005 12:02 PM
CT
I should add that if there frequent changes to the number of rows in the
table(s) you select from, the retrieved row count might not match the
actualumber of rows in the DataReader.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk

Show quote
"CT" <carstent@spammersgoawayintegrasol.dk> wrote in message
news:%23oS71fs4FHA.156@TK2MSFTNGP15.phx.gbl...
> Correct, it will only tell you if there is one ir more rows in the
> DataReader. It is generally used before trying to use the Read method to
> check if there are any rows. I suppose that you could do a ExecuteScalar
> to get the count along these lines:
>
>        Dim rowCount As Integer
>        Dim rowCountSQL As String = "SELECT COUNT(*) FROM tablename"
>
>        Dim yourConnection As New SqlConnection("...")
>        Dim yourCommand As New SqlCommand(rowCountSQL)
>
>        rowCount = CInt(yourCommand.ExecuteScalar())
>
>
> --
> Carsten Thomsen
> Communities - http://community.integratedsolutions.dk
>
> "David Thielen" <thielen@nospam.nospam> wrote in message
> news:2DC0EE25-DC8E-4FC5-B331-98EF6334BAE4@microsoft.com...
>> The way I read the docs for HasRows is it tells you if you have any from
>> the
>> select, but not if you have any more? Are you sure this works for all
>> DbConnection drivers?
>>
>> --
>> thanks - dave
>>
>>
>> "CT" wrote:
>>
>>> You can use the HasRows proeprty to check if there are any more rows. It
>>> might not be what you want, but you can do this without movinf the
>>> cursor,
>>> which is what the Read method does.
>>>
>>> --
>>> Carsten Thomsen
>>> Communities - http://community.integratedsolutions.dk
>>>
>>> "David Thielen" <thielen@nospam.nospam> wrote in message
>>> news:5639901C-363D-436D-807E-A4A57256CFD7@microsoft.com...
>>> > Hi;
>>> >
>>> > That's what I figured but it never hurts to ask. I can't do a count
>>> > because
>>> > I don't control the select.
>>> >
>>> > Oh well, just makes my coding a little harder.
>>> >
>>> > --
>>> > thanks - dave
>>> >
>>> >
>>> > "Kevin Yu [MSFT]" wrote:
>>> >
>>> >> Hi dave
>>> >>
>>> >> As far as I know, we cannot get the row count for a DataReader.
>>> >> Because
>>> >> when you open the data reader, a cursor is open the on the server, it
>>> >> doesn't provide the row count information. You can try to use COUNT
>>> >> in
>>> >> the
>>> >> SQL statement to get the row count first.
>>> >>
>>> >> Kevin Yu
>>> >> =======
>>> >> "This posting is provided "AS IS" with no warranties, and confers no
>>> >> rights."
>>> >>
>>> >>
>>>
>>>
>>>
>
>

AddThis Social Bookmark Button