tags:

views:

80

answers:

4

I was asked to create a desktop tool to compare an older access DB with a newer "updated" access DB and create an excel file of all new or altered records.

the DB tables I need to compare are structured
Primary key | Description.

I created a simple Windows form that allows the user to select the old and new databases and I figured out how to open the DB's and run SQL queries against them however I am stuck on how to do the comparison without making thousands of SQL calls to each DB.

With SQL server and a Web server I would be done by now but creating this as a desktop application and MS Access has me a bit confused.

A: 

Are you stuck with the user's choice of technologies? If there's just one older db and one newer one to compare, you don't need a desktop application. You'll need it if there's multiple older and newer ones.

You could import the Access dbs to SQL Server, if that helps.

Also consider creating a third Access db with links to the tables in the first two. Then you have both data sets in one place and you can write queries to compare them.

The idea's the same whether you bring them into memory or SQL Server, just easier to keep track of the queries on a back end than in code.

Beth
This task happens multiple times a month and I have no way of changing the technologies. Plus a desktop app allows me to do some learning in this direction. :)
Chris Chadwick
A: 

If you are using .Net Framework 3.5 or higher, one way is to create two lists and put the records from the two db's to compare in those two lists. After that, you can use the Linq Except and Union extension methods to quickly find the differences in the two sets. See here for these extension methods.

desigeek
+1  A: 

This is exactly the same in Access as it would be in SQL Server. Just write a query that does the comparison for you:

select t1.key, t1.description, t2.description
 from t1
   full outer join t2 on t1.key=t2.key
 where t1.description <> t2.description
    or t1.description is null
    or t2.description is null;

That will produce a list of all records that have identical keys but differing values. Then just dump the recordset to a file or a gridview or something.

You could also do the UI for this in pure MS Access, and skip the C# part entirely. Then it would be very fast to do, and MS Access is pretty good for building native UIs.

apenwarr
MS Access does not support full outer join.
Remou
That is, Jet/ACE doesn't support the OUTER keyword on JOINs. There's only INNER (default) and LEFT/RIGHT. I guess you need a UNION of two LEFT JOINs.
David-W-Fenton
A: 

Here is how I ended up doing it.

I recieved the Old and New database file information from users in a simple form. Then I opend the Olddatabase and added each record to a Dictionary "dictionaryOld" useing code from Ahmad Mageed's answer. I then created a Second Dictionary "dictionaryNew" to hold the records that were new or changed. I iterated through the New Database and compared each record to dictionaryOld adding the records that did not exist or were changed.

I am sure there are much better ways of doing this but it looks like it works for what I need thank you all for pointing me in a direction!

Now off to figureout how to write this out to a file and catch errors etc...

Here is the code

    Removed source, new and improved version comming....
Chris Chadwick
Why not just select the new or changed records? For example: SELECT * FROM t1 LEFT JOIN [MS Access;PWD=databasePWD;DATABASE=C:\docs\db.mdb].t2 ON t1.ID=t2.ID WHERE t2.ID Is Null
Remou
I am rewriting currently to do that.
Chris Chadwick
I have way over complicated this... I cannot get the sql statement to execute. I currently end up with the following
Chris Chadwick
"SELECT ID,Col2 FROM TableOLD LEFT JOIN [MS Access;DATABASE=c:\\dbNEW.mdb].t2 ON TableOLD.ID=TableNEW.ID WHERE TableNEW.ID Is Null" I get a syntax error on this statement when I execute the reader
Chris Chadwick
here is the simplified SQLstring sqlCompare = "SELECT "+col1+","+col2+" FROM "+t1+" LEFT JOIN [MS Access;DATABASE="+db2+"].t2 ON "+t1+"."+col1+"="+t2+"."+col1+" WHERE "+t2+"."+col1+" Is Null";
Chris Chadwick
The problem is, I think that the column names could refer to more than one table, so `SELECT tableNew.ID` or `t2.Col1` etc.
Remou