Beruflich Dokumente
Kultur Dokumente
OLE DB Overview
Introduction
Until recently, to use a Microsoft Access database in an external application, programmers based their code on ADO or previous libraries related to OLE DB. With the release of the .NETFramework, you can now use such a database in any language that is part of this huge library. To use and maintain a Microsoft Access database, you can use the .NET Framework data provider for OLE DB. If you are planning to use a Microsoft Access database in a .NET Framework application, probably the first thing you do is to create the database. You can do this directly in Microsoft Access. To make it possible to connect an application to a database, the .NET Framework provides a series of classes through ADO.NET and each class is made for a specific purpose. The classes of ADO.NET are stored in a namespace named System.Data. Therefore, to make sure your application has access to these classes, you can first include this namespace.
Database Creation
Public Sub New(ByVal connectionString As String) The argument to this constructor is a string that specifies how the connection would be established.
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("Provider=Microsoft.JET.OLEDB.4.0;") End Sub If the connection was already established and you want to find out what provider it is using, you can get the value of the OleDbConnection.Provider property. 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("Provider=Microsoft.JET.OLEDB.4.0;") End Sub Private Sub btnEnquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnquire.Click Dim strProvider As String = oleConn.Provider MsgBox(strProvider) End Sub
Closing a Connection
After establishing a connection and while using it, it consumes resources. When you have finished using a connection and if you don't need anymore or you would not use it for a while, you should close it to release its resources and make them available to other applications. To close a connection, the OleDbConnection class provides the Close() method. Its syntax is: Public Overridable Sub Close() Implements IDbConnection.Close Here is an example of calling this method: 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 oleConn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;" & _ "Data Source=C:\Programs\Exercise.mdb;" oleConn.Open() oleConn.Close() End Sub
Commanding a Database
Introduction
After establishing a connection to a database, you can perform actions or operations on it. To make this possible and probably easier, the System.Data.OleDb namespace provides theOleDbCommand class. As its name suggests, this class is used to create commands or actions to be performed on the database. To create a command, the OleDbCommand class is equipped with four constructors. The first constructor is the default. It allows you to declare an OleDbCommand variable without specifying its assignment.
you must provide an appropriate OleDbConnection value using the techniques we reviewed earlier.