Solved

Joining Datasets using columns

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now