|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DbDataReader numRows?Hi;
Is there a way to get the number of rows a DbDataReader has? -- thanks - dave 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." 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. -- Show quotethanks - 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." > > 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. 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." >> >> 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? -- Show quotethanks - 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." > >> > >> > > > 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()) 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." >> >> >> >> >> >> >> 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. 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." >>> >> >>> >> >>> >>> >>> > > |
|||||||||||||||||||||||