Professional Documents
Culture Documents
www.codeguru.com/vb/gen/vb_database/adonet/article.php/c15031/
Back to Article
Home >> Visual Basic >> General >> Database >> ADO.NET
To work with ADO.NET, the first thing you'll need is a database. A database sample has been provided in the attachment; it
consists of a few simple fields in the tbl_master table:
EmployeeID
FirstName
LastName
Location
You will be creating a simple form for navigating through the records in the table.
Start by placing three labels, three textboxes, and four buttons on a form as shown in the following figure. Name the
textboxes txtFirstName, txtLastName, and txtLocation. The buttons should be self explanatory as well: btnFirst, btnPrevious,
btnNext, and btnLast.
Now you can begin the coding. Declare a dataset at the class level and import the System.Data.OleDb namespace.
1 of 5 9/28/2010 2:10 AM
A Basic ADO.NET Tutorial in Visual Basic.NET - CodeGuru http://www.codeguru.com/vb/gen/vb_database/adonet/print.php/c15031/...
In the Form's Load event, fill the dataset. To do this, create a DataAdapter and use its Fill() method to fill up the dataset.
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; _
Data Source=C:\Documents and Settings\mendhak\My Documents\ _
Visual Studio 2005\Projects\ADONetTutorial1\ADONetTutorial1\ _
sample.mdb;User Id=admin;Password=;"
da.Fill(ds)
(You will have to modify the connection string to point the location of the MDB file on your machine.)
The dataset has now been filled. If you have worked with classic ADO, think of a dataset as something like a recordset,
except that a dataset is disconnected from the dataset, so you don't need to worry about cursors, EOF, BOF, or closing
connections. Datasets are .NET collections as well; this makes them more flexible.
Anyway, you now fill the textboxes with the data in the dataset. Remember that a dataset is a collection. More specifically, it
is a collection of DataTables. A DataTable simply represents a table of data you have retrieved from the database. You'll
start with the first row. Immediately after the Fill() method, do this:
To do the navigation, you will need an integer to hold your current Row position in the dataset's table. Declare an integer
where you declared the dataset.
Now, double-click the << button (btnFirst) and in its Click event, set the textboxes to read from Row 0.
2 of 5 9/28/2010 2:10 AM
A Basic ADO.NET Tutorial in Visual Basic.NET - CodeGuru http://www.codeguru.com/vb/gen/vb_database/adonet/print.php/c15031/...
txtFirstName.Text = _
ds.Tables(0).Rows(intCurrentIndex).Item("FirstName").ToString()
txtLastName.Text = _
ds.Tables(0).Rows(intCurrentIndex).Item("LastName").ToString()
txtLocation.Text = _
ds.Tables(0).Rows(intCurrentIndex).Item("Location").ToString()
Else
MessageBox.Show("You're already at the first record.")
End If
txtFirstName.Text = _
ds.Tables(0).Rows(intCurrentIndex).Item("FirstName").ToString()
txtLastName.Text = _
ds.Tables(0).Rows(intCurrentIndex).Item("LastName").ToString()
txtLocation.Text = _
ds.Tables(0).Rows(intCurrentIndex).Item("Location").ToString()
Else
MessageBox.Show("You're already at the last record.")
End If
That's it! You've just created a basic navigation form. You should be able to move to other rows.
3 of 5 9/28/2010 2:10 AM
A Basic ADO.NET Tutorial in Visual Basic.NET - CodeGuru http://www.codeguru.com/vb/gen/vb_database/adonet/print.php/c15031/...
There are many improvements that can be done here. The code to fill up the fields can be placed in a single method to which
you pass a parameter. You'll see this in the next part of the tutorial, along with adding, updating, and deleting.
Downloads
Search:
About Internet.com
Copyright 2010 QuinStreet Inc. All Rights Reserved.
Solutions
Whitepapers and eBooks
IBM Cloud Computing Development Center Microsoft TechNet Spotlight
MSDN Spotlight for Developers Helpful Cloud Computing Resources
4 of 5 9/28/2010 2:10 AM
A Basic ADO.NET Tutorial in Visual Basic.NET - CodeGuru http://www.codeguru.com/vb/gen/vb_database/adonet/print.php/c15031/...
Webcasts
Virtual Events in October: IBM Cloud Computing for Developers All About Cloud Computing
IBM Webcast: Developing in the Cloud - Sept. 30 MORE WEBCASTS, PODCASTS, AND VIDEOS
5 of 5 9/28/2010 2:10 AM