Free computer Tutorials

home
Stay at Home and Learn

Write your own Database code in VB .NET

 
Computer Tutorials List

 

 

 

 

 

In this next section, we'll take a look at the objects that you can use to open and read data from a Database. We'll stick with our Access database, the AddressBook.mdb one, and recreate what the Wizard has done. That way, you'll see for yourself just what is going on behind the scenes.

So close any open projects, and create a new one. Give it whatever name you like, and let's begin.

If you haven't yet downloaded the Address Book database, you can get it here:

Download the Address Book Database

 

The Connection Object

The Connection Object is what you need if you want to connect to a database. There are a number of different connection objects, and the one you use depends largely on the type of database you're connecting to. Because we're connecting to an Access database, we'll need something called the OLE DB connection object.

OLE stands for Object Linking and Embedding, and its basically a lot of objects (COM objects) bundled together that allow you to connect to data sources in general, and not just databases. You can use it, for example, to connect to text files, SQL Server, email, and a whole lot more.

There are a number of different OLE DB objects (called data providers), but the one we'll use is called "Jet". Others are SQL Server and Oracle.

So place a button on your form. Change the Name property to btnLoad. Double click your button to open up the code window. Add the following line:

Dim con As New OleDb.OleDbConnection

If you have the free Visual Basic 2005 Express Edition, you may see a wiggly line appear under the line of code. This is because you first need to add a reference to the Data Objects. Here's how to do this:

  • Click Project from the menu bar
  • Then click Add Reference
  • From the dialogue box, select the .NET tab. Scroll down and select the System.Data item
  • Click OK.

At the very top of your code window, before Public Class Form 1, type the following:

Imports System.Data

This will then allow you to work with the various objects in the Database section. Your coding window will look like this:

Add a reference to System.Data if you have the Express Edition

Whichever version you have, though, the variable con will now hold the Connection Object. Notice that there is a full stop after the OleDB part. You'll then get a pop up box from where you can select OleDbConnection. This is the object that you use to connect to an Access database.

 

No more reading these lessons online - get the eBook here!

 

Setting a Connection String

There are Properties and Methods associated with the Connection Object, of course. We want to start with the ConnectionString property. This can take MANY parameters . Fortunately, we only need a few of these.

We need to pass two things to our new Connection Object: the technology we want to use to do the connecting to our database; and where the database is. (If your database was password and user name protected, you would add these two parameters as well. Ours isn't, so we only need the two.)

The technology is called the Provider; and you use "Data Source" to specify where your database is. This should be entered on the same line, and not two as it is below. So add this to your code:

con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\AddressBook.mdb"

Notice the two parts, separated by a semi-colon:

1st Part: PROVIDER=Microsoft.Jet.OLEDB.4.0
2nd Part: Data Source = C:\AddressBook.mdb

The first part specifies which provider technology we want to use to do the connecting (JET). The second part, typed after a semi-colon, points to where the database is. In the above code, the database is on the C drive, in the root folder. The name of the Access file we want to connect to is called AddressBook.mdb. (Note that "Data Source" is two words, and not one.)

But your coding window should now look like this:

What your code window should look like (21K - needs javascript enabled)

This assumes that you have copied the AddressBook database over to the root folder of your C Drive. If you've copied it to another folder, change the "Data Source" part to match. For example, if you copied it to a folder called "databases" you'd put this:

Data Source = C:\databases\AddressBook.mdb

In our code , though, ConnectionString is a property of the con variable. The con variable holds our Connection Object. We're passing the Connection String the name of a data provider, and a path to the database.

 

Opening the Connection

Now that we have a ConnectionString, we can go ahead and open the datatbase. This is quite easy - just use the Open method of the Connection Object:

con.Open( )

Once open, the connection has to be closed again. This time, just use the Close method:

con.Close( )

Add the following four lines to your code:

con.Open()

MsgBox("A Connection to the Database is now open")

con.Close()

MsgBox("The Connection to the Database is now Closed")

Your coding window will then look like this:

What your code window should look like (30K - needs javascript enabled)

Test out your new code by running your programme. Click your button and the two message boxes should display. If they don't, make sure your Data Source path is correct. If it isn't, you might see this error message:

Connection Error

The error message is a bit on the vague and mysterious side. But what it's saying is that it can't find the path to the database, so it can't Open the connection. The line con.Open in your code will then be highlighted in green. You need to specify the correct path to your database. When you do, you'll see the message boxes from our code, and not the big one above.

 

Now that we've opened a connection to the database, we need to read the information from it. This is where the DataSet and the DataAdapter come in.

 

Learn about the DataSet and DataAdapter -->

<--Back to the .NET Contents Page

View all our Home Study Computer Courses