sqldatareader and RecordsAffected

  • Thread starter Thread starter Dune
  • Start date Start date
D

Dune

Hi there,

I'm executing a stored procedure using a command object
and the executeReader method. The result is returned as a
sqldatareader.

I need to know the number of rows in the sqldatareader and
I've tried using the RecordsAffected property but that
seems to always return -1, no matter how many rows is
actually in the sqldatareader.

Any ideas as to why this is happenning?

Cheers :)
 
Because that property always returns that for a datareader.

A datareader is a forward only cursor - you can only know the # of rows once
you've read through all of them.

If you need the # up front, use a dataset.
 
Like Marina said, you can't know this in advance. If you are using the 1.1
framework, the DataReader has a .HasRows property that's a boolen, it
returns true or false but not the number of rows. One way of doing it,
although not necessarily the most efficient is to run a select count(blah)
query that matches the one you are running and use it as an Output param or
seperate it with a Go and use NextResult.

Records Affected has no relation to the number or records selected, it only
relates to the number updated/inserted/deleted.

It's not elegant, but the DataReader is a heck of a lot faster so it's often
worth the trade off.

HTH,

Bill
 
Dune said:
Hi there,

I'm executing a stored procedure using a command object
and the executeReader method. The result is returned as a
sqldatareader.

I need to know the number of rows in the sqldatareader and
I've tried using the RecordsAffected property but that
seems to always return -1, no matter how many rows is
actually in the sqldatareader.

Any ideas as to why this is happenning?

Cheers :)

if you are using update, insert or delete query then the
RecordsAffected property
returns the number of rows affected. While using select query the value
returned is -1 and it is 0 if there are no rows affected. you have to
use another technique to get the number of rows affected.
i hope this link can help you..
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#f9fa855642094292.
 
Back
Top