We help IT Professionals succeed at work.

How to merge 2 datatable and exclude data

Hi experts

i have 2 database
i make a query on the first and i have, let's say this datatable :

ID (int) | Label (string)

I make another query on the other database and i have this datatable :

ID (int) | Label (string | X (int) | Y (int)


and i want to create a unique dataset or datatable that contains all result from the first datatable without the result of the second datatable based on ID

is it possible?

thank you

regards
Comment
Watch Question

Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
' Assuming you have similar data structures, you can merge the DataTables using
' the DataSet's Merge method, like this:

' Create a DataSet so I can merge the DataTable objects
Dim oDs As New DataSet("MyFunDataSet")
oDs.Tables.Add(sourceTable)
oDs.Merge(destinationTable)

'The Merge method will add the rows to the destimation table if it does not
'find a matching primary key.
Dirk HaestProject manager
CERTIFIED EXPERT

Commented:
CEO / CTO
Top Expert 2005
Commented:
"all result from the first datatable without the result of the second datatable based on ID"

That would be first table by itself.

Did you meant to say, "all results from the second datatable which has same ID as results in first table" ?

Here is a msdn article which talks about join between datatables.
http://support.microsoft.com/kb/325688/en-us

You could also
 1. add both tables to a dataset.
 2. add a new relation between two tables on the ID fields with first table as parent and second as child
 3. iterate all records on first tables
      a. get children records through the relation you created in #2
      b. if the # of children records is > 0 then you find a matching row(s) in the second table

CERTIFIED EXPERT
Commented:
maybe the better solution is making a query with left / right join that populates a datatable with the results that you want ...
Dirk HaestProject manager
CERTIFIED EXPERT
Commented:
>> maybe the better solution is making a query with left / right join that populates a datatable with the
>> results that you want ...

As a remark on this. I see that you use 2 diffent databases... Are they on the same server ? Can you use linked servers ?

Explore More ContentExplore courses, solutions, and other research materials related to this topic.