Record count when using a DataReader

G

Guest

I'm using a DataReader, populating an object per row with the values and
slapping those objects into an ArrayList.

the DataReader is returning several million records and i'd like to
preinitialize the size of the ArrayList with the exact count of objects that
i'm putting in (to minimize wasted memory because of the ArrayList's growth
algorithm).

Is there a way to get the record count when using a DataReader?
 
G

Guest

john,

You cannot get a record count directly from a data reader.

However, you could issue 2 select statements. The first would use Select
Count to get a count of the records that meet your criteria. The second
select would actually select the data. Use the result from Select Count to
initialize the arraylist.

I'll just mention that retrieving and working with several million records
is rarely practical.

Kerry Moorman
 
M

Marina

No, there is not. You need to either do a count query first, or read all the
rows out and count. Or you could just use a DataTable object for your
retrieval, which does not involve a connected server side cursor.

In either case, I strongly recommend you rethink your requirements to
retrieve millions of records. This will be a performance and memory
nightmare.
 
W

William \(Bill\) Vaughn

I would re-examine why you're fetching so many rows.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
W

William \(Bill\) Vaughn

Actually, the Fill method uses the DataReader "connected" interface and it's
not a server-side cursor, it's just a connected data stream. A server-side
cursor is far more expensive and another structure in of itself.
I also want to hear why he/she is moving the database to the client...
perhaps it's the first good reason for doing so that we've seen.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
M

Marina

I was trying to say that after issuing the query, all the data is in the
datatable, and you can disconnect. You can also see how many rows came
back, etc.
Whereas with the reader, after issuing the query, you still have to manually
read all the data in.
 
W

William \(Bill\) Vaughn

Of course. The point I was trying to make is that in a practical sense the
DataAdapter Fill is virtually identical to the code one has to write to
open, execute and return the rows and close the connection for a query using
a DataReader--especially if you have to (as you said) move it to a DataTable
afterwards to get the functionality you wanted in the first place...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

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