Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

17 Experts available now in Live!

Get 1:1 Help Now