Documente Academic
Documente Profesional
Documente Cultură
Introduction
OLEDB
ActiveX Data Objects (ADO) comes with ASP and allows your
pages to easily connect to databases. ADO works with any OLEDB
source, which includes ODBC-compliant sources. So it will work
with most databases currently being used. The ADO contains all
the objects that will be normally used within the Active Server
Pages. The ADO model contains six objects. Here’s a brief
overview of the main objects contained in the ADO:
Parameters
Collection
Command
Object
Connection Recordset
Object Object
Errors Fields
Collection Collection
I. Connecting to a DataBase:
First you need to create a new database. Before you can do
anything (e.g. like talking) to the database, you need to get a
connection to it.
The Connection Object
The Connection objects used to hold information about the data
store you want to access. It is created the same way your other
components have been, using
Server.CreateObject
Dim objConn
Set objConn = Server.CreateObject(“ADODB. Connection”)
There may be times when you want connections to several data
stores open at once. You might be using multiple Access
databases, or mixing Access, Oracle, SQL, or whatever. In such
cases, you would use multiple instances of the Connection
object.
Using a System DSN
A System DSN is a file that contains information about the
database such as where it is and what kind of database it is DSN
stands for Data Source Name. Creating a System DSN is easy:
i. Close Access.
ii. In Windows 2000, click Start button; go to Settings, Control
Panel, Administrative Tools, Data Sources (ODBC). Or, go to
Start, Administrative Tools, Data Sources.
iii. Go the System DSN tab. You should see something like
Figure below. There is a listing of the system data source
currently configured. Along the right are three buttons that
allow you to add a new System DSN, remove the selected
one, or change the settings of the currently selected one.
Figure: Viewing the Data Source Administrator System DSN
tab.
vi. Now the setup box pops up. Enter WidgetWorld.dsn for the
Data Source Name. This is the name you will use to
reference the DSN in your ASP pages. You may enter a
description as well. It should be something to remind you
what this database and DSN are for.
vii. Click Select. Now choose the .mdb file for the database. We
named it WidgetWorld.mdb.
viii. Click OK to select the database. Your screen should look
similar to Figure. Click OK again to finish creating the DSN.
You should see the DSN you just created listed under System
Data Sources.
ix. Click OK to exit.
Now you have a System DSN. You can tell the Connection object
about it like this: ObjConn.ConnectionString =
“DSN=WidgetWorld.dsn”
Using a DSN-less Connection
There is an alternative to using a System DSN. Instead of putting
the connection information into the DSN, you can put it into the
connection string. You saw an example of this kind of connection
yesterday. Here is the connection string we used:
ObjConn.ConnectionString=”DRIVER={MicrosoftAccessDriver
(*.mdb)};” & “DBQ=C:\My Documents\WidgetWorld.mdb”
The DRIVER=line tells the Connection object what kind of
database it is connecting to. The DBQ=line indicates where on the
server the database resides. This must be a complete physical
path. If you do not know the complete physical path, use
Server.MapPath. You can specify other values in the connection
used. If you prefer, you may change the connection string for a
DSN-less connection.
Closing the Connection
As with any object, you need to free the memory associated with
it when you are finished. However, before you do that, you need
to close the connection. You close a connection and then free the
object like this:
ObjConn.Close
Set objConn=Nothing
Always close and free record sets associated with a connection
before closing and freeing the connection. If you are finished with
one connection but want to open up another, you can reuse the
Connection object you just finished with. Simply close it, set the
connection information appropriately, and reopens it.
Now you know how to open a recordset. The next topic is how to
move through it and retrieve values from it. To do this, you will
need a few more methods and properties of the Recordset object.
The Recordset object may contain several records, but you can
only access one at a time. The Recordset object maintains a
pointer to the current record, and you can move that pointer
forward. If you have not set the recordset to forward-only
movement, you can also move pointer backward.
MoveNext advances to the next record in the recordset.
MovePrevious goes back to the previous record in the recordset.
This is not valid if the recordset is set to forward-only movement.
MoveFirst is a method that moves to the first record in the
recordset. MoveFirst may be used even if movement is set to
forward-only movement, but it may result in re-executing the
original Open method.
MoveLast goes directly to the last record in the recordset.
Move number goes to number records from the current one.
Because trying to move to either after the last record or before
the first one causes errors, you need some way of finding out
whether you are at the beginning or end of the recordset. A pair
of properties tells you this.
BOF is a Boolean value that is True while you are still at the
beginning of the recordset.
EOF is a Boolean value that is set to True when you reach the end
of the recordset.
You access data from the current record like this:
RecordsetObjectName(“FieldName”)
This returns the value of the field specified by FieldName in the
current row of the recordset.