|
System.Data.OleDb
The System.Data.OleDb namespace provides classes that are required to connect
to OLE DB data sources. Let's take a look at the classes provided by System.Data.OleDb.
OleDbConnection class
The System.Data.OleDb.OleDbConnection class represents a connection to OleDb data
source. Applications that need to connect to a OleDb data source should use this class.
OleDbCommand class
The System.Data.OleDb.OleDbCommand class represents a SQL statement or stored
procedure that is executed in a database by an OLE DB provider. The OleDbCommand class
can be used to create the Select, Insert, Update and Delete commands that
need to be sent to the data source.
OleDbDataReader
The System.Data.OleDb.OleDbDataReader class creates a data reader. It is used to read
a row of data from the database. The data is read as forward-only, read-only stream
which means that data is read sequentially, one row after another. The DataReader
is independent of the OleDb data source from which the data is retrieved.
OleDbDataAdapter
The System.Data.OleDb.OleDbDataAdapter acts as a middleman between the application
and OleDb data source. We use the Select, Insert, Delete and Update command properties
of this class for loading and updating the data.
DataSet
The System.Data namespace contains a DataSet class which is a disconnected, in-memory
representation of data. It can be considered as a local copy of all the relevant portions
of the database. The DataSet is persisted in memory and the data in it can be manipulated
and updated independent of the database.
Code Samples
The following code samples will put the System.Data.OleDb namespace and the classes
within it to work. The following code samples assume that you have an Access database
named Books (Books.mdb) with three columns (BookName, Publisher, ISBN) on the C: drive
of your machine.
Select Command
Open a new Web Forms page, add a Button control to it an paste the following code.
Imports System.Data.OleDb
'namespace to be imported
Public Class WebForm5 Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
#End Region
Dim myConn As OleDbConnection
Dim myComm As OleDbCommand
Dim dr As OleDbDataReader
Private Sub Select_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Select.Click
Try
myConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;_
Data Source=C:\sandeep\books.mdb;")
myConn.Open()
myComm = New OleDbCommand("Select* from Table1", myConn)
dr = myComm.ExecuteReader
Do While dr.Read
'reading from the datareader
Response.Write(dr(0) & " ")
Response.Write(dr(1) & " ")
Response.Write(dr(2) & "<br>")
'displaying data from the table
'html break is used to display data in a tabular format
Loop
Catch
End Try
End Sub
End Class
|
Next>>Insert, Update, Delete
|