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. How to combine two datatables into single datatable

How to combine two datatables into single datatable

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 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
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, I am trying to combine two datatables into single datatable... First table contains 3 columns[e.g: 'col1','col2','col3'] Second table contains 2 columns[e.g: 'col4','col5'] Both tables have the same no.of records[rows], but different columns. How to combine them into single table? I need DataTable with columns ['col1','col2','col3','col4','col5']. I am inserting through looping the both the tables and adding values into third table. I tried the Merge (DataTable.Merge()), but it is adding the records to bottom of first one, but i need it should be added to the side[as columns] to first table' Is there any other way? please suggest me. Thanks in advance.

    P L 2 Replies Last reply
    0
    • K K V Sekhar

      Hi all, I am trying to combine two datatables into single datatable... First table contains 3 columns[e.g: 'col1','col2','col3'] Second table contains 2 columns[e.g: 'col4','col5'] Both tables have the same no.of records[rows], but different columns. How to combine them into single table? I need DataTable with columns ['col1','col2','col3','col4','col5']. I am inserting through looping the both the tables and adding values into third table. I tried the Merge (DataTable.Merge()), but it is adding the records to bottom of first one, but i need it should be added to the side[as columns] to first table' Is there any other way? please suggest me. Thanks in advance.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      sekhar.k wrote:

      I am inserting through looping the both the tables and adding values into third table.

      That should work. But a better technique is to use a join in your SQL query to start with.

      1 Reply Last reply
      0
      • K K V Sekhar

        Hi all, I am trying to combine two datatables into single datatable... First table contains 3 columns[e.g: 'col1','col2','col3'] Second table contains 2 columns[e.g: 'col4','col5'] Both tables have the same no.of records[rows], but different columns. How to combine them into single table? I need DataTable with columns ['col1','col2','col3','col4','col5']. I am inserting through looping the both the tables and adding values into third table. I tried the Merge (DataTable.Merge()), but it is adding the records to bottom of first one, but i need it should be added to the side[as columns] to first table' Is there any other way? please suggest me. Thanks in advance.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Is there a common key column between these two tables?

        K 1 Reply Last reply
        0
        • L Lost User

          Is there a common key column between these two tables?

          K Offline
          K Offline
          K V Sekhar
          wrote on last edited by
          #4

          Thanks for your response. There is no common key or relation ship between them. Both queries are from different tables.

          L 1 Reply Last reply
          0
          • K K V Sekhar

            Thanks for your response. There is no common key or relation ship between them. Both queries are from different tables.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Then joining them may not make any meaning since the order of rows returned by SQL Server is not guaranteed. If you still want to proceed, add an IDENTITY column to both tables and use those columns to join the tables.

            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