DataReader and RecordCount

G

Guest

Does anyone have any insight on how I can get a count of the records returned
after executing an ExecuteReader command? Something similar to the
..RecordCount property in Classic ASP?
 
M

Mark Rae

Does anyone have any insight on how I can get a count of the records
returned
after executing an ExecuteReader command? Something similar to the
.RecordCount property in Classic ASP?

Not natively. The DataReader is a connected object, so its record count is
not known until all the records have been read.

If you don't need to know the record count *before* you start reading the
records, you can simply increment a counter each time you read a record.

If you just want to know whether the DataReader contains records or not, use
its HasRows property (assuming you're using v1.1 of the Framework)

However, if you really need to know the number of records before you have
read them all, essentially you have two choices:

1) Return two recordsets, the first containing the number of rows e.g.

SELECT COUNT(*) FROM <table>
SELECT * FROM <table>

2) Use a DataSet...
 
¿

¿ Mahesh Kumar

declare 1 integer and assign the Datareader.ExecuteReader();
This method will return some integer(how many records affected / inserted) ,
work on that.
int i=Datareader.ExecuteReader(); --try ?

Mahesh
www.snipurl.com/guac
 
G

Guest

Thanks for this but the ExecuteReader method does not return an integer?

It is my understanding that it returns a SQLDataReader object.
 
G

Guest

Thank you for this help.

I do need the count before I loop through the records.

HasRows will not help because I need a specific count.

Submitting 2 SQL calls (SELECT COUNT(*) and regular SELECT) is what I
thought I might have to do, but was hoping for something more efficient.

Using a DataSet rather than a DataReader might be the answer, but I still do
not see any Count property that would give me the count before I looped
through the records?
 
M

Mark Rae

Using a DataSet rather than a DataReader might be the answer, but I still
do
not see any Count property that would give me the count before I looped
through the records?

<DataSet object>.Tables[0].Rows[0].Count
 
M

Mark Rae

declare 1 integer and assign the Datareader.ExecuteReader();
This method will return some integer(how many records affected / inserted)
,
work on that.
int i=Datareader.ExecuteReader(); --try ?

1) ExecuteReader() is a method of the Command object, not the DataReader
object.

2) ExecuteReader() returns a DataReader object, not an integer.
 
M

Mark Rae

Thanks for this but the ExecuteReader method does not return an integer?

It is my understanding that it returns a SQLDataReader object.

That's correct - you should ignore that post - it's totally wrong.
 
P

Patrick.O.Ige

There a many ways to do that.
You can make use of "ExecuteScalar "
if you use
Select count(*) from TableName
you will get back a single item - - so you can use ExecuteScalar, instead of
executeReader:
dim counter as integer
counter=cmdMbr.ExecuteScalar
label1.text=counter

Using Dataset also is a good idea but there is alos some burden vs
getting the count(*) directly using command object
Patrick
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top