Solved

Joining Datasets using columns

Posted on 2003-10-31
4
428 Views
Last Modified: 2008-02-01
Hi,

I am looking for a way of joining or merging two datasets together in VB.net.  
Essentially I have two separate datasets, which have been populated
with data from a CSV file. The example below gives an Idea of
structure of both datasets.

ds1
----

Code, Name, Spend
A45 5, tyyy, 0.56
B65 5, YUTYU, 0.78
H74 0, GFGHHG, 0.59

ds2
---
Code, Data1, Data2, Data3
A45 5, Data, Data, Data
B65 5, Data, Data, Data
H74 0, Data, Data, Data

What I am trying to do is join the two data sets together using in the Code
field (This is same as a vlookup in excel or a join in SQL).  This
would then produce another dataset with the merged datasets which I can then
export to another CSV file.

ds3
---
Code, Name, Spend, Data1, Data2, Data3
A45 5, tyyy, 0.56,  Data, Data, Data
B65 5, YUTYU, 0.78, Data, Data, Data
H74 0, GFGHHG, 0.59, Data, Data, Data

I have a SQL version but this is done in a store procedure. I really need
to do this using flat files (csv) outside of SQL in a standalone application.
Also with the number of records being over 100,000 I can not use excel.

Does anyone have any ideas?  Also if anyone can think of an alternative way of
doing this the please let me know!!

Thanks in advance!!
0
Comment
Question by:nova30
[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
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 150 total points
ID: 9658267
The DataSet class has a Merge method to merge two data sets.
0
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 150 total points
ID: 9671657
This sample will show you how to merge two datasets into one dataset. When it starts up it will pull data
from two different xml files and populate the two grids then all you have to do is hit button 3 to join the
bottom grid to the top grid. Hit button 1 or 2 to refresh the respective grids under them. This sample shows
using the ignore action so that it only looks for a match and updates that match using the primary ID and
any extra records get ignored. What makes this sample work is making sure you got your primary keys
setup correctly.


Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region
    Private LoadDS1 As New DataSet("Imported DataSet")
    Private LoadDS2 As New DataSet("Imported DataSet")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim XMLReader As New Xml.XmlTextReader(Application.StartupPath & "\test2.txt")
            LoadDS1.ReadXml(XMLReader)
            XMLReader.Close()
            Dim DT As New DataTable()
            DT = LoadDS1.Tables(0)
            DataGrid1.DataSource = DT
            CurDir(Application.StartupPath)

        Catch err As Exception
            MsgBox(err.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim XMLReader As New Xml.XmlTextReader(Application.StartupPath & "\test1.cla.txt")
            LoadDS2.ReadXml(XMLReader)
            XMLReader.Close()

            Dim DT As New DataTable()
            DT = LoadDS2.Tables(0)
            DataGrid2.DataSource = DT
            CurDir(Application.StartupPath)
        Catch err As Exception
            MsgBox(err.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Key1() As DataColumn = {LoadDS1.Tables("licenseview").Columns("moduleid")}
        LoadDS1.Tables("licenseview").PrimaryKey = Key1
        LoadDS1.EnforceConstraints = True

        Dim Key2() As DataColumn = {LoadDS2.Tables("licenseview").Columns("moduleid")}
        LoadDS2.Tables("licenseview").PrimaryKey = Key2
        LoadDS2.EnforceConstraints = True

        LoadDS1.Merge(LoadDS2, False, MissingSchemaAction.Ignore)
    End Sub
End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10232310
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: TheLearnedOne {http:#9658267} & iboutchkine {http:#9671657}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

729 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