/  3
 
 SEB’s VB.NET Guide
Working with Databases
Overview of Task Sequence:
1.Set up your project / solution.2.Set up your database.3.Save your database into your project’s start up file (i.e.: into its BIN folder)4.Manipulate your database through your code – 
the following instructions aredesigned to help you through this stage
.Initial Set-upAdd 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 & manipulatedVariables 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 anexample for placing the data from a database into a series of labels (the labels arereferenced through their group box indexes).1.Establish the connection
 Page 1 of 3
 
 SEB’s VB.NET Guide
2.Initialise database variables3.Fill the dataset with the contents of the database table4.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 ina program, underscores are required to indicate that where one line continues on toanother.
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 datain a specified order (sorted)
“Provider=Microsoft.Jet.OLEDB.4.0;Password=; User ID=Admin;DataSource =” & 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 BINfile)
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

Share & Embed

More from this user

Add a Comment

Characters: ...