Clone/compare a DataReader?

V

VB Programmer

I have a datareader that I am using in my VB.NET code. I loop through it
once "processing" each row.

On the 2nd "pass" I want to get the newest data in the database (stored into
a 2nd datareader) and compare each record (compare dr1 and dr2) to see what
has changed. If a row remains the same I don't "process" it again.

What is the best/easiest way to do this? Should I use a DataSet instead?

Is it possible to clone the 1st datareader before we read it so we have it
for reference for the next time?

Thanks!
 
W

William Ryan

The datareader must be kept open after the first pass if you want to use it
again as such. Depending on your situation, this probably isn't desirable.

If you only have a few fields, you can populate something like an array list
or collectino with the info from Reader1 so that you can close it and its
connection and then use the collection to compare against with reader two.
I don't think cloning it is going to do you any good b/c you only get use
out of a DataReader by having it actively connected to the DB and walking
forward through it.

You can do it with two datatables, or you could use a reader and a table, it
really depends on what your ultimate goal is, how much time is going to pass
between Reader1 and Reader2, how many columns/rows in the resultset etc.

For instance, if you are talking about an 10 minutes between reads, then
definitely close that first reader (I'd probably close it even if they were
to occur one after the other). If you need to check every value of every
column and you have a lot of columns and rows, then you may want to consider
DataTables just for ease of use. after all, SELECT * From MyTable and a
da.Fill() is a lot easier to code than iterating through a dataReader and
adding items to a custom collection of 50 properties for instance [assuming
there were 50 columns in your query].

Can you post a little more about the specifics and what the ultimate goal
is, it'd probably be easier to help you that way.

HTH,

Bill
 

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