Solved

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

Posted on 2006-11-28
4
180 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Replace &lt; with < 14 59
User Authentication using Digital Certificate 2 47
MediaHelp 4 23
Can I command line build a C# application? 6 33
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 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