Solved

How can I create a dataset table with data from 2 different data source?

Posted on 2006-11-28
4
178 Views
Last Modified: 2010-04-16
Hi, I'm using vs2005 .net 2.0.  I need to create a dataset table with some columns of data from one data source and some other columns of data from another data source.  These 2 data source have the same primary keys and that's how that can relate to each other in creating this new table.  

I need to create this table so I can then make a query into this dataset table.  Are there any one line sample code on how I can do this?  Thanks.
0
Comment
Question by:lapucca
4 Comments
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 18035175
If your data is in the same database than you can use a select on both tables to combine data in a DataTable.
This method will work if you have multiple databases on a DBMS that supports queryes on multiple databases.
If this is not the case than I see no other solution but to select in two different DataTables and write code to combine them.
0
 
LVL 4

Accepted Solution

by:
CBeach1980 earned 500 total points
ID: 18038416
If you can't do a join within the database as BogdyPtr suggests you could process them on the C# side to combine them into a single table.

ex/

DataTable dt1 = DataSource1.Tables[0];
DataTable dt2 = DataSource2.Tables[0];
DataTable dtCombined = dt1.Clone();

foreach (DataColumn col in dt2.Columns)
{
    // If there are duplicate column names you may have to add a prefix
    dtCombined.Columns.Add(col);
}

foreach (DataRow dr in dt1.Rows)
{
    string pkey = dr["<Name of key field>"].ToString();
    DataRow ndr = dtCombined.NewRow();
    ndr["<Name of key field>"] = pkey;
    // Read each field from dr into ndr
    ....
    DataRow drs2 = dt2.Select("<Key field> = " + pkey);
    foreach (DataRow dr2 in drs2)
    {
        // Read each field from dr2 into ndr
        ....
    }
    dtCombined.Rows.Add(ndr);
}

Let me know if you need any additional clarification.
0
 

Author Comment

by:lapucca
ID: 18074578
Hi CBeach,

My data source are not straight forward database source.  Your sample is exactly what I'm looking for.  I haven't had a change to do it (coding some other part).  I'll let you know as soon as I can do it in a day or 2 .  Thanks.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now