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

Chapter 19 - Getting Started With ADO.NET

By , About.com Guide

8 of 10

Page 2 of the program

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.

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.

Since the section of code that actually reads the database is critical, the entire section will be in a Try block. If an error occurs, the message will be displayed in the form using code in the Catch block. A more complete and professional system would probably have more error checking, but it would be variations of this same technique.

Here's the ADO.NET code down to the execution of the DataReader. The CommandText is set to a very simple SQL statement that reads everything in an example "Articles" table from the Access database.

~~~~~~~~~~~~~~~~~~~~~~~~~
ocmdAboutVBCommand = New OleDbCommand()
With ocmdAboutVBCommand
  .Connection = New OleDbConnection(strAboutVBConn)
  .Connection.Open()
  .CommandText = "Select * From Articles"
  odtrAboutVBDataReader = .ExecuteReader()
End With
~~~~~~~~~~~~~~~~~~~~~~~~~

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Getting Started with ADO.NET

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

All rights reserved.