473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with datasets

I am playing around trying to learn how to manipulate data in VB 2005. I
have code set to open up two db connections and pull in select data. Now
what I need to do is loop through table 1, field 1 and compare it to table 2,
field 2 and do some logical processing if the value exists.. I think I need
to use a While / for loop but not sure where to add it in. Basically I want
to look at table 1, field 1 compare it to table 2, field 2 and , if it does
not exist, write it to a file. If it does exist then continue on to the next
record.

Here is my code so far:
Imports Microsoft.SqlServer
Imports System
Imports System.Data
Imports system.Data.SqlClient

Public Class Form1

Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnProcess.Click

Dim connection As New
System.Data.SqlClient.SqlConnection("Server=VISTAD AN;Database=BlueForceImport;Trusted_Connection=Tru e")
Dim connection2 As New
System.Data.SqlClient.SqlConnection("Server=Portal ;Database=SLDemoApp60;Trusted_Connection=True")

' Create a SqlDataAdapter for the Employee table.
Dim EmployeeAdapter As SqlDataAdapter = New SqlDataAdapter()

' A table mapping names the DataTable
EmployeeAdapter.TableMappings.Add("Table", "tblEmployees")

' Open the Connection
connection.Open()
MsgBox("SQLConnection1 is open.")

' Create a SQLCommand to retrieve Employee Data
Dim EmployeeCommand As SqlCommand = New SqlCommand("SELECT * FROM
tblEmployees;", connection)
EmployeeCommand.CommandType = CommandType.Text

' Set the SqlDataAdapter's SelectCommand.
EmployeeAdapter.SelectCommand = EmployeeCommand

' Fill the DataSet.
Dim NewEmployees As DataSet = New DataSet("Employees")
EmployeeAdapter.Fill(NewEmployees)
' Create a SqlDataAdapter for the old Employee table.
Dim OldEmployeeAdapter As SqlDataAdapter = New SqlDataAdapter()

' A table mapping names the DataTable
OldEmployeeAdapter.TableMappings.Add("Table", "PJEMPLOY")

' Open the Connection
connection2.Open()
MsgBox("SQLConnection2 is open.")

' Create a SQLCommand to retrieve Employee Data
Dim OldEmployeeCommand As SqlCommand = New SqlCommand("SELECT * FROM
PJEMPLOY;", connection2)
OldEmployeeCommand.CommandType = CommandType.Text

' Set the SqlDataAdapter's SelectCommand.
OldEmployeeAdapter.SelectCommand = OldEmployeeCommand

' Fill the DataSet.
Dim OldEmployees As DataSet = New DataSet("OldEmployees")
OldEmployeeAdapter.Fill(OldEmployees)

connection.Close()
MsgBox("SQLConnection1 is closed.")

connection2.Close()
MsgBox("SQLConnection2 is closed.")

End Sub
End Class

Oct 10 '08 #1
1 1236
Dan,

For this kind of compares the do while is not the best option anymore in VB.

The For and ForEach is so strong that it does everything by instance,

\\
ForEach dr1 as DataRow in Table1.Rows
ForEach dr2 as DataRow in Table2.Rows
'Do what you want to do between dr2 and dr1
Next
Next
///

Don't be afraid that this is slow, you cannot get it faster, behind the
scene it will forever something like this.

Cor

"Dan Shepherd" <Da*********@discussions.microsoft.comschreef in bericht
news:48**********************************@microsof t.com...
>I am playing around trying to learn how to manipulate data in VB 2005. I
have code set to open up two db connections and pull in select data. Now
what I need to do is loop through table 1, field 1 and compare it to table
2,
field 2 and do some logical processing if the value exists.. I think I
need
to use a While / for loop but not sure where to add it in. Basically I
want
to look at table 1, field 1 compare it to table 2, field 2 and , if it
does
not exist, write it to a file. If it does exist then continue on to the
next
record.

Here is my code so far:
Imports Microsoft.SqlServer
Imports System
Imports System.Data
Imports system.Data.SqlClient

Public Class Form1

Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnProcess.Click

Dim connection As New
System.Data.SqlClient.SqlConnection("Server=VISTAD AN;Database=BlueForceImport;Trusted_Connection=Tru e")
Dim connection2 As New
System.Data.SqlClient.SqlConnection("Server=Portal ;Database=SLDemoApp60;Trusted_Connection=True")

' Create a SqlDataAdapter for the Employee table.
Dim EmployeeAdapter As SqlDataAdapter = New SqlDataAdapter()

' A table mapping names the DataTable
EmployeeAdapter.TableMappings.Add("Table", "tblEmployees")

' Open the Connection
connection.Open()
MsgBox("SQLConnection1 is open.")

' Create a SQLCommand to retrieve Employee Data
Dim EmployeeCommand As SqlCommand = New SqlCommand("SELECT * FROM
tblEmployees;", connection)
EmployeeCommand.CommandType = CommandType.Text

' Set the SqlDataAdapter's SelectCommand.
EmployeeAdapter.SelectCommand = EmployeeCommand

' Fill the DataSet.
Dim NewEmployees As DataSet = New DataSet("Employees")
EmployeeAdapter.Fill(NewEmployees)
' Create a SqlDataAdapter for the old Employee table.
Dim OldEmployeeAdapter As SqlDataAdapter = New SqlDataAdapter()

' A table mapping names the DataTable
OldEmployeeAdapter.TableMappings.Add("Table", "PJEMPLOY")

' Open the Connection
connection2.Open()
MsgBox("SQLConnection2 is open.")

' Create a SQLCommand to retrieve Employee Data
Dim OldEmployeeCommand As SqlCommand = New SqlCommand("SELECT *
FROM
PJEMPLOY;", connection2)
OldEmployeeCommand.CommandType = CommandType.Text

' Set the SqlDataAdapter's SelectCommand.
OldEmployeeAdapter.SelectCommand = OldEmployeeCommand

' Fill the DataSet.
Dim OldEmployees As DataSet = New DataSet("OldEmployees")
OldEmployeeAdapter.Fill(OldEmployees)

connection.Close()
MsgBox("SQLConnection1 is closed.")

connection2.Close()
MsgBox("SQLConnection2 is closed.")

End Sub
End Class

Oct 13 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1075
by: harish | last post by:
Friends. I have three Datasets to be populated to one Excel Workbook. but i need to populate each datasets in to one worksheet. So if i export the datasets to one excel file, sheet1 should have...
4
1725
by: Alpha | last post by:
I have a small Window application and through out the different forms I create a different dataset. At the begining I used the Tools to drag and drop the SqlDataAdapter, connection and dataset...
2
1286
by: Sandy | last post by:
Hello - I am used to retrieving data with stored procedures. DataSets have me baffled inasmuch as it exposes Sql statements directly in the code, however, I think I need to be working with...
6
1807
by: lennon1 | last post by:
Hi, I have already started learning .NET and I have a question. If I want to do anything - Display Data, Navigate, Update - with database (SQL Server) in Visual Studio 2005, do I have to use all...
16
1913
by: Luqman | last post by:
Is it recommended to use datasets in ASP.Net 2.0 / VS.Net 2005 ? Best Regards, Luqman
4
9904
by: Ronald S. Cook | last post by:
I've always used untyped datasets. In a Microsoft course, it walks through creating typed datasets and harps on the benefits. It has you drag all these things around ..wizard, wizard, wizard......
5
1354
by: Warex | last post by:
I am using the example from the microscuzz site on making a key but It keeps giving me an error: With DataSets.Tables("Numbers") .PrimaryKey = New DataColumn() {.Columns("Number")} End With ...
0
1203
by: S.Tedeschi | last post by:
Hi all; as posted some days ago, I'm converting an on-line app; I used to heavily rely on strongly-typed DataSets directly dropped onto pages, and so viewed by code(-behind) as well. In the next...
12
3550
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a...
9
1930
by: gardnern | last post by:
We have X number of data sets, of Y length each. For example... Small, Medium, Large and Red, Green, Blue, Yellow We need to generate a list of all possibilities Small Red
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.