SEB’s VB.

NET Guide

Wo r k i n g w i t h D a t a b a s e s
Overview of Task Sequence:
1. 2. 3. 4. Set up your project / solution. Set up your database. Save your database into your project’s start up file (i.e.: into its BIN folder) Manipulate your database through your code – the following instructions are designed to help you through this stage.

Initial Set-up Add the following line above the class declaration: Imports System.Data.Oledb

Declare Module Level Variables:
Manipulation of a database through a VB.NET solution requires the following: •OleDbDataAdapter The OleDbDataAdapter is used to set up the type and path of the connection. •Dataset The Dataset is used to temporarily store the data from the database. •OleDbCommandBuilder The OleDbCommandBuilder enables the database to be updated & manipulated Variables must be declared for each of these at modular level. EG: Dim objDA As OleDbDataAdapter Dim objDS As New Data.DataSet() Dim objCB As OleDbCommandBuilder

Loading Data from a Database:
You need to know what you intend to do with the data. The following provides an example for placing the data from a database into a series of labels (the labels are referenced through their group box indexes). 1. Establish the connection
Page 1 of 3

SEB’s VB.NET Guide

2. Initialise database variables 3. Fill the dataset with the contents of the database table 4. Populate the labels

1. Establish the connection
objDA = New OleDbDataAdapter(“Select * from Data”, _ “Provider=Microsoft.Jet.OLEDB.4.0;Password=;” _ & “User ID=Admin;Data Source =” & _ Application.StartupPath & “\Dbase.mdb”) •ObjDA is the OleDbDataAdapter variable previously declared •This is all one statement which has overflowed across several lines – when this occurs in a program, underscores are required to indicate that where one line continues on to another. •There are two strings passed in through the parenthasis: “Select * from Data” This is an SQL statement which extracts the required data from the database.  * means anything / everything (ie: all fields)  Data is the name of the table in the database being accessed  The SQL statement can extract more specific data or extract the data in a specified order (sorted) “Provider=Microsoft.Jet.OLEDB.4.0;Password=; User ID=Admin;Data Source =” & Application.StartupPath & “\Dbase.mdb”) Note: The & at the beginning of line 3 was used only rejoin the string where is had been split.  This statement specifies the type of database being accessed  Application.StartupPath is a relative path (to the projects BIN file)  Dbase is the name of the database being accessed

2. Initialise database variables
objDS = New Data.DataSet() objCB = New OleDbCommandBuilder(objDA) •objDS and ObjCB are variables previously declared to store required data components

3. Fill the dataset with the contents of the database table
objDA.Fill(objDS, “Data”) •Using the Fill property of objDA the dataset is filled with the contents of the table •Data refers to the database table.

Page 2 of 3

SEB’s VB.NET Guide

4. Populate the labels
With objDS.Tables(0) For lintRecs = 0 To (.Rows.Count – 1) gbxNames.Controls(lintRecs).Text = _ .Rows(lintRecs).Item(“Name”).ToString gbxScores.Controls(lintRecs).Text = _ .Rows(lintRecs).Item(“Score”).ToString Next End With Note: This is the same as the following statement without the WITH: For lintRecs = 0 To (objDS.Tables(0).Rows.Count – 1) gbxNames.Controls(lintRecs).Text = _ objDS.Tables(0).Rows(lintRecs).Item(“Name”).ToString gbxScores.Controls(lintRecs).Text = _ objDS.Tables(0)Rows(lintRecs).Item(“Scores”).ToString Next •A For loop is being used here to loop through the records in the table •objDS.Tables(0).Rows.Count is the number of records in the data table •lintRecs is a local integer variable used to count through the loop •gbxNames and gbxScores are both group boxes containing a series of labels The first time through the loop, the first label in the group box would be referenced: gbxNames.Controls(0).Text = …. If the name of the first label was lblTopName, the above statement would be the same as: lblTopName.Text = … •The underscore indicates that the line continues on to the following line •objDS.Tables(0).Rows(lintRecs).Item(“Name”).ToString references the database. Below is an example of a database table which would produce the results in the illustration above Names Tom Billy Jo Scores 988 972 963

Page 3 of 3