Solved

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

Posted on 2006-11-28
4
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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