1. Home
  2. Computing & Technology
  3. Visual Basic
Using ADO .NET - Access and OleDB
Part 2
3 - Coding A DataReader Application
 More of this Feature
• 1 - Starting at the beginning ...
• 2 - The OleDbDataReader Object
• 4 - Try-Catch Error Processing
• 5 - Finishing the Application
• 6 - The Application Example Code
 
 Join the Discussion
Is this the kind of article that helps you?
Let us know!
 
 Related Resources
• VB .NET Books!
• New to VB .NET?
  Learn It Here!
 
 Elsewhere on the Web
• About.Com DATABASES
 

In the example application we will cover here, the entire database is stored in a series of arrays. This allows random access to the data after the connection is closed on the client machine. The entire solution can be downloaded at the end of the article, but first, I'll go through each line of code and explain how it works. The flowchart boxes at the left diagram the ADO .NET related steps visually.

Import Namespace

The application starts with an Imports statement for the OleDb namespace.

'Imports OleDb namespace
Imports System.Data.OleDb

Then, before any subroutine code, a series of DIM statements are added to make the scope of the variables global over the subroutines. This has nothing to do with ADO .NET but it makes my program work.

Dim I As Integer = 0
Dim dteAboutVBDate(10000) As Date
Dim strAboutVBDesc(10000) As String
Dim blnAboutVB6(10000) As Boolean
Dim blnAboutVBNet(10000) As Boolean
Dim urlAboutVBURL(10000) As String
Connection String

The program reads the database and initializes the ListBox in the Form Load subroutine. The first statement in that subroutine is the declaration of the Connection String. This is a set of parameters that allows ADO .NET to match the unique characteristics of your database. For the Access database in this example, the Connection String is pretty simple. (Note that {path} must be replaced by the actual path on the computer.)

Dim strAboutVBConn As String = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" _
        & "User ID=Admin;" _
        & "Data Source={path}\"AboutVB.mdb"
OleDb Objects

The next statements declare objects necessary for using OleDb: the Command object and the DataReader object.

Dim ocmdAboutVBCommand As OleDbCommand
Dim odtrAboutVBDataReader As OleDbDataReader

The Imports statement coded earlier makes it possible to avoid qualifying these names in the DIM statement. Without the Imports statement,

OleDbCommand

would be coded as

System.Data.OleDb.OleDbCommand.

OleDb Objects

The next tasks are to instantiate the Command and Connection objects, Open the Connection and execute the DataReader. But before we do that, another refinement will be added.

Next page > Try-Catch Error Processing > Page 1, 2, 3, 4, 5, 6
Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.