Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with merging 2 DataTables

Problem with merging 2 DataTables

Scheduled Pinned Locked Moved C#
helpannouncement
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kallileo
    wrote on last edited by
    #1

    I'm trying merge DataTable "db2_table1" into to DataTable "db1_table1". But it doen't seem to work. Any help will be appreciated.... public class MergeTableClass { private OleDbDataAdapter da1, da2; private DataSet ds1; 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"; da1 = new OleDbDataAdapter(cmd1, oleConn1); da2 = new OleDbDataAdapter(cmd2, oleConn2); ds1 = new DataSet(); da1.Fill(ds1,"db1_table1"); da2.Fill(ds1,"db2_table1"); ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]); da1.Update(ds1,"db2_table1"); ds1.AcceptChanges(); } }

    L 1 Reply Last reply
    0
    • K kallileo

      I'm trying merge DataTable "db2_table1" into to DataTable "db1_table1". But it doen't seem to work. Any help will be appreciated.... public class MergeTableClass { private OleDbDataAdapter da1, da2; private DataSet ds1; 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"; da1 = new OleDbDataAdapter(cmd1, oleConn1); da2 = new OleDbDataAdapter(cmd2, oleConn2); ds1 = new DataSet(); da1.Fill(ds1,"db1_table1"); da2.Fill(ds1,"db2_table1"); ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]); da1.Update(ds1,"db2_table1"); ds1.AcceptChanges(); } }

      L Offline
      L Offline
      lsconyer
      wrote on last edited by
      #2

      I think you're calling Update() on "db2_table1." Should you be calling it on "db1_table1"?

      Lester http://www.lestersconyers.com

      K 1 Reply Last reply
      0
      • L lsconyer

        I think you're calling Update() on "db2_table1." Should you be calling it on "db1_table1"?

        Lester http://www.lestersconyers.com

        K Offline
        K Offline
        kallileo
        wrote on last edited by
        #3

        I have tried both...still doens't work. :^)

        L 1 Reply Last reply
        0
        • K kallileo

          I have tried both...still doens't work. :^)

          L Offline
          L Offline
          lsconyer
          wrote on last edited by
          #4

          Oh ok. I think you just need to create a new command object with an update command. Then set the UpdateCommand of the adapter equal to that command object.

          Lester http://www.lestersconyers.com

          K 1 Reply Last reply
          0
          • L lsconyer

            Oh ok. I think you just need to create a new command object with an update command. Then set the UpdateCommand of the adapter equal to that command object.

            Lester http://www.lestersconyers.com

            K Offline
            K Offline
            kallileo
            wrote on last edited by
            #5

            I tried to create a OleDbCommandBuilder but it still didn't work... OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1);

            L 1 Reply Last reply
            0
            • K kallileo

              I tried to create a OleDbCommandBuilder but it still didn't work... OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1);

              L Offline
              L Offline
              lsconyer
              wrote on last edited by
              #6

              What did you do next? da1.UpdateCommand = cmdBld.GetUpdateCommand(); ?

              Lester http://www.lestersconyers.com

              K 1 Reply Last reply
              0
              • L lsconyer

                What did you do next? da1.UpdateCommand = cmdBld.GetUpdateCommand(); ?

                Lester http://www.lestersconyers.com

                K Offline
                K Offline
                kallileo
                wrote on last edited by
                #7

                Nothing yet... 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"; da1 = new OleDbDataAdapter(cmd1, oleConn1); da2 = new OleDbDataAdapter(cmd2, oleConn2); ds1 = new DataSet(); da1.Fill(ds1, "db1_table1"); da2.Fill(ds1, "db2_table1"); ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]); OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1); da1.UpdateCommand = cmdBld.GetUpdateCommand(); @da1.Update(ds1, "db1_table1"); ds1.AcceptChanges(); }

                K 1 Reply Last reply
                0
                • K kallileo

                  Nothing yet... 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"; da1 = new OleDbDataAdapter(cmd1, oleConn1); da2 = new OleDbDataAdapter(cmd2, oleConn2); ds1 = new DataSet(); da1.Fill(ds1, "db1_table1"); da2.Fill(ds1, "db2_table1"); ds1.Tables["db1_table1"].Merge(ds1.Tables["db2_table1"]); OleDbCommandBuilder cmdBld = new OleDbCommandBuilder(da1); da1.UpdateCommand = cmdBld.GetUpdateCommand(); @da1.Update(ds1, "db1_table1"); ds1.AcceptChanges(); }

                  K Offline
                  K Offline
                  kallileo
                  wrote on last edited by
                  #8

                  Found it.. The problem was that after the Merge none of the rows in data table had the state RowState Changed or Added. The update methon had nothing to apply. This peace of code made the whole class work as it should: foreach (DataRow row in ds1.Tables["db2_table1"].Rows) row.SetAdded(); Thanks

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups