Friday, January 16, 2009

Application Connection to the Database

The classes you can use to connect your application to a Microsoft Access database are stored in the System.Data.OleDb namespace. To use these classes, besides including this namespace in the file where needed, you should also import the System.Data.OleDb.dll library in your project.



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oleConn As System.Data.OleDb.OleDbConnection
oleConn = New System.Data.OleDb.OleDbConnection
End Sub

If you are planning to access the connection from more than one event or method, you should declare it at the class level. Here is an example:

Dim oleConn As System.Data.OleDb.OleDbConnection

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oleConn = New System.Data.OleDb.OleDbConnection
End Sub

As opposed to using the default constructor, to establish a connection, you can use the other constructor of the class. Its syntax is:

Public Sub New(ByVal connectionString As String)

The argument to this constructor is a string that specifies how the connection would be established.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home