DbDataReader numRows?

K

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."
 
G

Guest

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.
 
C

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.
 
G

Guest

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?
 
C

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())
 
C

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top