Solved

Joining Datasets using columns

Posted on 2003-10-31
4
422 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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