Tuesday, 4 November 2014

Merge multiple Access database into one database with c#



I suggest you take the following steps:
  1. Create a database with a single table to hold the final results. The table should have the exact same structure as the individual files, but with a primary key field - either the path to the database file, or some other unique value for each row (this can be generated with an AutoNumber field). You may want an additional column for the database path in any case, particularly if you need to prevent rereading the same database file multiple times.
  2. Open an ADO.NET connection to the final database, using the OleDb provider. This entails a) creating an OleDbConnection object, b) creating an OleDbCommand object that runs on the connection, c) setting the CommandText of the command object to an SQL statement, and d) executing the command.
  3. Your SQL statement could look something like this:
    INSERT INTO desttable (pkfield, field1, field2 ...)
    SELECT field1
    , field2
    FROM sourcetable
    sourcetable can also be a table in a different database like so:
    INSERT INTO desttable (pkfield, field1, field2 ...)
    SELECT field1
    , field2
    FROM sourcetable IN path\to\mdb
    so for each path you could build the SQL statemnt by substituting the appropriate path each time.
  4. If you want to iterate over all the mdb files in a particular folder, you can use the EnumerateFilesmethod of the Directory class, in the System.IO namespace. Alternatively, you can open up a dialog box with the OpenFileDialog in the Windows.Forms namespace.
  5. Once you've figured out what kind of UI you want to see, it should be trivial to bind to this data.

No comments:

Post a Comment

Working with 3- Tier Architecture in C#

  Introduction In this article we will learn Use to 3- Tier architecture in C#.NET application. 3-Tier architecture is very famous and ...