Solved

compare & update a MS access database through CSharp code

Posted on 2007-04-02
10
231 Views
Last Modified: 2008-01-09
I have 2 databases,old and the new.Now ,I need to compare the old from the new db & update the old from the new instead of overwriting the old db. db is the term used for db. I work with Ms access db's & C#.net .

Can anyone tell me how to i compare & update instead of overwrite the databases..is there any C# script to be written..

Thanx in advance
0
Comment
Question by:Anisha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 41

Expert Comment

by:graye
ID: 18837211
Luckly, this scenario was anticipated by the folks who wrote ADO.Net...  

Here is how you'd do it...   You'd do a normal DataAdapter.Fill operation for each of the tables (the old and the new).   Then you'd do a DataSet.Merge operation.  This will create a DataTable what has rows that are marked for insertion, deletion,  or modification based  upon the old and new tables.   The last step is to save these changes to the underlying database with the DataAdapter.Update method.

http://msdn2.microsoft.com/en-us/library/system.data.dataset.merge.aspx
0
 

Author Comment

by:Anisha
ID: 18842598

Hi Graye,

I tried working with the merge functions....But this is not exactly what i want to do..

1)If new records are inserted into the New db,i would like the old db too to get the newly inserted record from the new db.

2)If i have to update records in the old db from the new db, that is with  the same primary key but with different data entry in the fields for that particular primary key record ....so i wud like the table to add a new record with an automated primary key ,to which the record could be added...instead of overwritting the existing record( same primary key record with different data) in the old db .

How do i go about with these problem?
0
 
LVL 41

Expert Comment

by:graye
ID: 18842942
Let me see if I understand...

1) an Insert in "New", should create an Insert in "Old"
2) an Update in "New", should create a new Insert in "Old"?

Is that it?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:Anisha
ID: 18848679
Yes graye!!

1) an Insert in "New", should create an Insert in "Old"
2) an Update in "New", should create a new Insert in "Old"?
Thats right,
1) an Insert in "New", should create an Insert in "Old"1) an Insert in "New", should create an Insert in "Old"
2) an Update in "New", should create a new Insert in "Old"?
2) an Update in "New", should create a new Insert in "Old"?

Can u help out with a solution,,,,...or should i manually code each step to manually compare each record and each field & then insert records if required...which is going to be a tedious task..if there is any better solution to save time...kindly guide.

Thanx

0
 

Author Comment

by:Anisha
ID: 18855697
Yes graye!!

1) an Insert in "New", should create an Insert in "Old"
2) an Update in "New", should create a new Insert in "Old"?
Thats right,

Can u help out with a solution,,,,...or should i manually code each step to manually compare each record and each field & then insert records if required...which is going to be a tedious task..if there is any better solution to save time...kindly guide.

Thanx
0
 
LVL 41

Expert Comment

by:graye
ID: 18856980
I don't see any "easy way" to do with... although the "hard way" isn't all that bad.

I'd suggest you use the DataTable.Select method to find all of the rows that have been changed/added (via the DataViewRowState property).  Then you'd have to write code to perfom the inserts of those rows into the other table.

http://msdn2.microsoft.com/en-us/library/b5c0xc84.aspx
0
 
LVL 41

Accepted Solution

by:
graye earned 125 total points
ID: 18857276
Here is a quick-n-dirty example to demontrate the concepts

            foreach (DataRow dr_new in dt_new.Select(null, null, (DataRowState.Added | DataRowState.Modified)))
            {
                DataRow dr_old;
                // copy the rows from the one table to another
                dr_old = dt_old.NewRow();
                dr_old.ItemArray = dr_new.ItemArray;
                dt_old.Rows.Add(dr_old);
            }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Change to file doesn't show up 16 93
C# Service FileSystemwatcher isse 9 60
asp.net, radiobuttonlist, c# 3 66
aspx ascx, c# 7 39
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question