Inserting Data from other Database File
-
I use code to insert data from a mdb file into another using the Merge() method of a DataSet...But it's not very effective in terms of performance. Is it possible to use a sql query to directly insert the data? I don't know how to create the code to link the databases.
public void MergeTables(string connS1, string connS2) { string connString1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connS1; string connString2 = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + connS2; OleDbConnection oleConn1 = new OleDbConnection(connString1); OleDbConnection oleConn2 = new OleDbConnection(connString2); string cmd1 = "SELECT *FROM db1_table1"; string cmd2 = "SELECT *FROM db2_table1"; OleDbDataAdapter da1 = new OleDbDataAdapter(cmd1, oleConn1); OleDbDataAdapter da2 = new OleDbDataAdapter(cmd2, oleConn2); DataSet ds1 = new DataSet(); da1.Fill(ds1, "db1_table1"); da2.Fill(ds1, "db2_table1"); ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]); foreach (DataRow row in ds1.Tables["db1_table1"].Rows) row.SetAdded(); OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1); da1.UpdateCommand = cmdBld.GetUpdateCommand(); @da1.Update(ds1, "db1_table1"); }
Thank you :-D