?
Solved

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

Posted on 2006-11-28
4
Medium Priority
?
186 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 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