Solved

Joining Datasets using columns

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

837 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