3

I have two almost same databases (both are *.mdb), but one of them has few new tables. Now I can only detect tables, that should be imported, using code below:

    public static List<string> GetDBTables(string path)
    {
        List<string> allTables = new List<string>();
        String connect = ("Provider=Microsoft.JET.OLEDB.4.0;data source=" 
                                     + path + ";Persist Security Info=False;");

        OleDbConnection con = new OleDbConnection(connect);
        con.Open();

        DataTable tables = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, 
                                     new object[] { null, null, null, "TABLE" });

        int counter = 1;
        foreach (DataRow row in tables.Rows)
        {
            allTables.Add(row[2].ToString());
            counter++;
        }
        con.Close();

        return allTables;
    }

    var withNewTables = GetDBTables(".\\one.mdb");
    var withoutNewTables = GetDBTables(".\\another.mdb");
    var NotFoundTables = withNewTables.Except(withoutNewTables).ToList();

How can I import these tables in the old database using C #?

2 Answers 2

6

Access SQL offers two features which are useful here.

  1. SELECT <field list> INTO NewTable
  2. FROM table_name IN 'path to other db file'

So I can execute this statement from an OleDb connection to my destination db file, and it will create tblFoo_copy from the data contained in tblFoo in the other db file, NewData.mdb.

SELECT f.* INTO tblFoo_copy
FROM tblFoo AS f IN 'C:\Users\hans\Documents\NewData.mdb';

Build and execute similar statements for each of those tables you want to import.

Sign up to request clarification or add additional context in comments.

Comments

6

Well, in addition to HansUp answer, I post the implementation of this on C#:

public static void insertTables(string path_from, string path_to, 
                                                            List<string> _tables)
{
    string conString = ("Provider=Microsoft.JET.OLEDB.4.0;data source=" 
                                    + path_to + ";Persist Security Info=False;");
    OleDbConnection dbconn = new OleDbConnection(conString);
    dbconn.Open();
    OleDbCommand dbcommand = new OleDbCommand();

    _tables.ForEach(delegate(String name)
    {
        string selQuery = "SELECT f.* INTO " + name + " FROM " + name 
                                               + " AS f IN '" + path_from + "';";

        dbcommand.CommandText = selQuery;
        dbcommand.CommandType = CommandType.Text;
        dbcommand.Connection = dbconn;
        int result = dbcommand.ExecuteNonQuery();
    });

    dbconn.Close();
}

insertTables(".\\one.mdb", ".\\another.mdb", NotFoundTables);

1 Comment

Can I use this way if 'another.mdb' has a password? An if it has, how?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.