473,657 Members | 2,559 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.SqlSe rver
Imports System
Imports System.Data
Imports system.Data.Sql Client

Public Class Form1

Private Sub btnProcess_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnProcess.Clic k

Dim connection As New
System.Data.Sql Client.SqlConne ction("Server=V ISTADAN;Databas e=BlueForceImpo rt;Trusted_Conn ection=True")
Dim connection2 As New
System.Data.Sql Client.SqlConne ction("Server=P ortal;Database= SLDemoApp60;Tru sted_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", "tblEmploye es")

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

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

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

' Fill the DataSet.
Dim NewEmployees As DataSet = New DataSet("Employ ees")
EmployeeAdapter .Fill(NewEmploy ees)
' Create a SqlDataAdapter for the old Employee table.
Dim OldEmployeeAdap ter As SqlDataAdapter = New SqlDataAdapter( )

' A table mapping names the DataTable
OldEmployeeAdap ter.TableMappin gs.Add("Table", "PJEMPLOY")

' Open the Connection
connection2.Ope n()
MsgBox("SQLConn ection2 is open.")

' Create a SQLCommand to retrieve Employee Data
Dim OldEmployeeComm and As SqlCommand = New SqlCommand("SEL ECT * FROM
PJEMPLOY;", connection2)
OldEmployeeComm and.CommandType = CommandType.Tex t

' Set the SqlDataAdapter' s SelectCommand.
OldEmployeeAdap ter.SelectComma nd = OldEmployeeComm and

' Fill the DataSet.
Dim OldEmployees As DataSet = New DataSet("OldEmp loyees")
OldEmployeeAdap ter.Fill(OldEmp loyees)

connection.Clos e()
MsgBox("SQLConn ection1 is closed.")

connection2.Clo se()
MsgBox("SQLConn ection2 is closed.")

End Sub
End Class

Oct 10 '08 #1
1 1250
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*********@di scussions.micro soft.comschreef in bericht
news:48******** *************** ***********@mic rosoft.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.SqlSe rver
Imports System
Imports System.Data
Imports system.Data.Sql Client

Public Class Form1

Private Sub btnProcess_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnProcess.Clic k

Dim connection As New
System.Data.Sql Client.SqlConne ction("Server=V ISTADAN;Databas e=BlueForceImpo rt;Trusted_Conn ection=True")
Dim connection2 As New
System.Data.Sql Client.SqlConne ction("Server=P ortal;Database= SLDemoApp60;Tru sted_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", "tblEmploye es")

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

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

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

' Fill the DataSet.
Dim NewEmployees As DataSet = New DataSet("Employ ees")
EmployeeAdapter .Fill(NewEmploy ees)
' Create a SqlDataAdapter for the old Employee table.
Dim OldEmployeeAdap ter As SqlDataAdapter = New SqlDataAdapter( )

' A table mapping names the DataTable
OldEmployeeAdap ter.TableMappin gs.Add("Table", "PJEMPLOY")

' Open the Connection
connection2.Ope n()
MsgBox("SQLConn ection2 is open.")

' Create a SQLCommand to retrieve Employee Data
Dim OldEmployeeComm and As SqlCommand = New SqlCommand("SEL ECT *
FROM
PJEMPLOY;", connection2)
OldEmployeeComm and.CommandType = CommandType.Tex t

' Set the SqlDataAdapter' s SelectCommand.
OldEmployeeAdap ter.SelectComma nd = OldEmployeeComm and

' Fill the DataSet.
Dim OldEmployees As DataSet = New DataSet("OldEmp loyees")
OldEmployeeAdap ter.Fill(OldEmp loyees)

connection.Clos e()
MsgBox("SQLConn ection1 is closed.")

connection2.Clo se()
MsgBox("SQLConn ection2 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
1081
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 Dataset1 and Sheet2 should have dataset2 and so on. PLEASE HELP ME WITH THIS. -- Harish
4
1730
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 objects to the frist few forms but then later I removed those and created these objects in my code. I now see 3 datasets in the Solution Explorer panel part but not all the datasets that I have in my codes. Are these 3 datasets leftover from the...
2
1294
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 Datasets for what I'm doing because I can't be continually posting back to the server. Is there a way to create datasets from stored procedures? Also, I have three tables. If I use datasets that access data from all
6
1818
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 this objects : - DATASET - sqlDataAdapter - BindingSource - sqlConnection I usually write database applications in Delphi, but now I want to learn Dot Net platform ant it's quite diffrent than programming
16
1927
by: Luqman | last post by:
Is it recommended to use datasets in ASP.Net 2.0 / VS.Net 2005 ? Best Regards, Luqman
4
9915
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... code gen, code gen, code gen. What's at the end looks slick, but then there's a ton of generated code that I'm going to have to maintain now. I.e. I like typing things myself (don't like wizards) so I can know exactly what I've done.
5
1363
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 Error: Object reference not set to an instance of an object.
0
1216
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 two weeks I discovered that such objects are no more directly usable in pages, namely in DataGrid which don't see them any more. Even if rebuilt in Component Designer, and so visible in code, DataSets are invisible in Page Designer, so now it's...
12
3581
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 gridview, for example, I loop through a datareader, populating an array list with instances of a custom class in the middle tier, and then send the array list up to the presentation layer and bind the gridview to it. If I use a typed dataset, I...
9
1937
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
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7324
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1611
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.