Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Joining Datasets using columns

Posted on 2003-10-31
4
Medium Priority
?
440 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 600 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 600 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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