M0194 Web-based Programming Lanjut Session 5 

advertisement
M0194
Web-based Programming Lanjut
Session 5
 2004 Tau Yenny, SI - Binus
2
ActiveX Data Object (ADO)


OLE DB and ADO Architecture
The ADO Object Model







Connection Object
Command Object
Recordset Object
Record Object
Stream Object
Collections
Connecting to Data Stores
 2004 Tau Yenny, SI - Binus
3
ADO Basics

ActiveX Data Object (ADO)



The components that allow us to interact with data
stores
One way to access data
OLE DB


The underlying technology
Interfaces between our programs and the source
of the data
 2004 Tau Yenny, SI - Binus
4
OLE DB and ADO Architecture
Application
VC++
VB
Script
Java
ADO
OLEDB
RDBMS
E-mail
Directory
Services
Data Store
 2004 Tau Yenny, SI - Binus
5
OLE DB and ADO Architecture

ADO is a COM component and can be used in any
COM compliant language, such as Delphi, or
scripting language that supports the Active Scripting
interface.

OLE DB was not designed to be used in all
language, so ADO sits on top of OLEDB, and
provides an interface for those languages, such as
VB and scripting languages, which can’t access
OLE DB directly.
 2004 Tau Yenny, SI - Binus
6
OLE DB and ADO Architecture

Two main reasons why do we need OLEDB and
ADO


First, OLEDB and ADO are designed to give you access to
data stores. Note that ‘data store’ does not always be
‘databases’. Although databases are still the most widely
used form of data storage, they don’t necessarily contain all
of your data.
Second, the rise of Internet applications, and the stateless
nature of the Web. The older data access methods aren’t
designed to handle data when they are not permanently
connected to their store of data. OLEDB and ADO provide
disconnected recordsets.
 2004 Tau Yenny, SI - Binus
7
The ADO Object Model
Errors
Collection
Connection
Active
Connection
Command
Execute()
ActiveCommand
Active
Connection
ActiveConnection
Recordset
SourceRecordset
GetChildren()
Execute()
Record
Open()
Collection
Collection
Collection
Source
Fields
Save()
Open()
Open()
Parameters
Stream
 2004 Tau Yenny, SI - Binus
8
The ADO Object Model

The Connection Object




The Connection object is what enables us to connect to
data stores. Can be constructed or stored commands (for
example SQL command or a stored procedure)
We can specify which OLEDB Provider we wish to use, the
security details used to connect to the data store, and any
other details.
It is not necessary to explicitly create a Connection object
to connect to a data store. We can create Command,
Recordset and Record objects without a Connection object
ADO implicitly creates a Connection object if we do not
create one ourself.
 2004 Tau Yenny, SI - Binus
9
The ADO Object Model

The Command Object



The Command object is designed for running
commands against a data store.
Connection objects has restrictions on its facilities
for handling commands, whereas the Command
object was specifically created for dealing with all
aspects of commands.
Implicitly created when we run a command from
the Connection object.
 2004 Tau Yenny, SI - Binus
10
The ADO Object Model

The Recordset Object




Probably is the most commonly used object in
ADO, since it is this object that contains the sets
of data we extract from our data stores.
The Recordset is the object that holds set of
records.
It allows us to change the data (additions, updates
and deletions), move around the records, filter the
records, and so on.
Contains a Fields collection, where there is a Field
object for each field (column) in the recordset.
 2004 Tau Yenny, SI - Binus
11
The ADO Object Model

The Record Object


The Record object is used to model a semistructured recordset in ADO.
Semi-structured recordset is the sets of data
where the columns may be different (number of
column and data type) for each row.
 2004 Tau Yenny, SI - Binus
12
The ADO Object Model

The Stream Object




The Stream object is used to access the contents
of a node, such as an email message, or a Web
page.
Have access to the actual contents of a file, or
resource.
The Stream object is designed to handle binary
data.
Another use for streams is XML, where we can
access a set of data (structured or semistructured) as a stream of XML.
 2004 Tau Yenny, SI - Binus
13
The ADO Object Model

Collections

The Fields Collection
Holds Fields object associated with a recordset or a record.

The Parameters Collection
Used solely by the Command object, and identifies the
parameters in stored commands.

The Errors Collection
Contains details of the last ADO or OLE DB Provider error
that was generated by a command, and can be only
accessed through the Connection object.
 2004 Tau Yenny, SI - Binus
14
The ADO Object Model

The Properties Collection
Designed to work with many different data stores,
each of which might have different facilities
Connection
Properties
Record
Command
Recordset
Fields
 2004 Tau Yenny, SI - Binus
15
The ADO Object Model

We can use the same structure of code to loop through the
collections:
The syntax in VBScript is :
For Each Object In Collection
‘ Do Something with object
Next
For example, to loop through the Fields collection of a Recordset object :
For Each objField In rs.Fields
Response.Write objField.Name & “<BR>”
Next
If you prefer JScript, then you can use the Enumerator object :
for (objField = new Enumerator(rs.Fields); !objField.atEnd(); objField.moveNext())
Response.Write (objField.item().Name + ‘<BR>’);
 2004 Tau Yenny, SI - Binus
16
The ADO Object Model
Example of loop through Errors Collection :
For Each objError In rs.ActiveConnection.Errors
Response.Write objError.Name & “<BR>”
Next
Example of the OLE DB Provider for Jet allows us to access the Jet
specific security properties:
Set conDB = Server.CreateObject(“ADODB.Connection”)
conDB.Open “DSN=NWind”
conDB.Properties(“Jet OLEDB:Database Password”) = “LetMeIn”
 2004 Tau Yenny, SI - Binus
17
The ADO Object Model

ADO Constants

The first method of reference the constants is to include them in
your ASP file:
<!-- #INCLUDE FILE=“adovbs.inc” -->


You can either copy the include file into your local directory, or
reference it from the installation directory. One disadvantage of
this method is that it makes your ASP page larger, since a whole
host of constants are included, many of which you won’t typically
need.
A better solution is to create a reference to the type library, which
makes the constants available without having to include them in
the file :
<!– METADATA TYPE=“typelib” FILE=“C:\Program Files\Common Files\System
\ado\msado15.dll” -->
 2004 Tau Yenny, SI - Binus
18
Connecting to Data Stores

Several different ways connect to a data
source:



A Connection String. Put the connection details
in a string, or directly into the command that
opens the data store.
A Data Link File. A file (with a .udl extension) that
contains the connection details.
ODBC Data Source, or DSNs. Similar to Data
Link files, but are only applicable to ODBC Data
Sources. To be used in an ASP page, the Data
Source must be a System Data Source.
 2004 Tau Yenny, SI - Binus
19
Connecting to Data Stores

Connection Strings
The connection string varies upon the Provider. The OLEDB
Provider for ODBC is the default – therefore if you leave off
the Provider= section, you will automatically use ODBC.

Microsoft Access
If using an ODBC connection, without a DSN:
Driver = {Microsoft Access Driver (*.mdb)}; DBQ=C:\MyData\thedata.mdb
For native OLE DB Provider:
Provider=Microsoft.Jet.OLEDB.4.0; Data Source= C:\MyData\thedata.mdb
 2004 Tau Yenny, SI - Binus
20
Connecting to Data Stores

Microsoft SQL Server
Using the Provider for ODBC:
Driver = {SQL Server}; Server=server_name; Database=database_name;
UID=user_name; PWD=user_password
For example:
Driver = {SQL Server}; Server=MUGGLE; Database=pubs; UID=arine; PWD=letmein
For the native OLE DB Provider:
Provider =SQLOLEDB; Data Source=server_name; Initial Catalog=database_name;
User Id=user_name; Password=user_password
For example:
Provider =SQLOLEDB; Data Source=MUGGLE; Initial Catalog=pubs; User Id=arine;
Password=letmein
 2004 Tau Yenny, SI - Binus
21
Connecting to Data Stores

Microsoft Indexing Service
The Indexing Service is only accessible through a native OLE DB
Provider. The syntax is:
Provider =MSIDXS; Data Source=catalog_name
For example, using the Web catalog:
Provider =MSIDXS; Data Source=Web
 2004 Tau Yenny, SI - Binus
22
Connecting to Data Stores

ODBC Drivers
In the example that use the OLE DB for ODBC, the Driver is shown in curly
braces. For example:
Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\MyData\database_name.mdb
The name you should use be the
exact name taken from the list of
drivers when creating a new
data source :
 2004 Tau Yenny, SI - Binus
23
Connecting to Data Stores

Data Link Files
Create a Data Link file by
simply creating a blank text
file and renaming the
extension to .udl.
Once you have the physical
data link file, you can open
it, either by double-clicking,
or right-mouse clicking and
selecting Open. You are
presented with the following
dialog:
 2004 Tau Yenny, SI - Binus
24
Connecting to Data Stores
The details of this screen
change depending upon the
Provider selected. Note that
if you select the Allow
saving password option, the
password you type will be
saved in plain text in the
UDL file.
To change the Provider you
can select the Provider tab:
 2004 Tau Yenny, SI - Binus
25
Connecting to Data Stores
Alternatively, you can edit the file in a text editor :
You can select that the UDL file is really just storing a
connection string. To use the data link you simply specify
the data link file when opening the connection:
conDW.Open “File Name=C:BI\Batches.udl”
 2004 Tau Yenny, SI - Binus
26
Connecting to Data Stores

ODBC Data Sources
ODBC Data Sources (commonly
called Data Source Names, or
DSNs) are set up through the Data
Sources option on the
Administrative Tools menu. Previous
versions of Windows had this as an
applet in the Control panel.
To access a DSN from an ASP page
you must make sure that the DSN is
set up as a System DSN.
This simply involves selecting the
System DSN tab on the Data
Source Administrator, and then
selecting the Add.. button:
Once a DSN is set up, you use the DSN= attribute of the connection string.
For example:
conDW.Open “DSN=pubs“
 2004 Tau Yenny, SI - Binus
27
Using Include Files

Using include files to contain connection strings gives a
central place to store the connection details for a number of
ASP pages.
Connection.asp
<%
strConn = “Provider=SQLOLEDB; Data Source=MUGGLE; “ & _
“Initial Catalog=pubs; User Id=Arine; Password=letmein”
Response.Write “”
%>

In your ASP pages, add this on the top of the page:
<!-- #INCLUDE FILE=“Connection.asp” -->
This saves you having to type the connection details for each
ASP page, and lets you change the connection used for the
entire site from one central location.
 2004 Tau Yenny, SI - Binus
28
Using Connection State

Storing the connection string in an Application variable is quite
a common trick too, and equally as effective as using an
include file. For example, you could add the following to your
global.asa file:
Sub Application_OnStart()
strConn = “Provider=SQLOLEDB; Data Source=MUGGLE; “ & _
“Initial Catalog=pubs; User Id=Arine; Password=letmein”
Set Application (“ConnectionString”) =strConn
End Sub
In your ASP pages you can then use the following :
Set conDW = Server.CreateObject(“ADODB.Connection”)
conDW.Open Application(“ConnectionString”)
 2004 Tau Yenny, SI - Binus
29
Connection Syntax

If using an explicit Connection object, use the Open method:
connection.Open [ConnectionString], [UserID], [Password], [Options]
The arguments are as follows:
Argument
Description
ConnectionString
The string containing the connection details. This can be the name of an
ODBC DSN, the name of a Data Link file, or just the actual connection
details.
UserID
The name of the user to use during the connection. This overrides any
user name supplied in the connection string.
Password
The password for the user. This overrides and password details supplied
in the connection string.
Options
This can be adAsyncConnect to specify that the connection be
established asynchronously. Omitting this parameter ensures a
synchronous connection.
Connecting asynchronously is no use in an ASP environment, since scripting languages
can’t receive events from ADO.
 2004 Tau Yenny, SI - Binus
30
Connection Examples

To open a connection use the Open method of the Connection object. For
example, assume that strConn contains a valid connection string:
Set conPubs = Server.CreateObject(“ADODB.Connection”)
conPubs.Open strConn
‘ Some processing
conPubs.Close

Alternatively, use the ConnectionString property:
Set conPubs = Server.CreateObject(“ADODB.Connection”)
conPubs.ConnectionString = strConn
conPubs.Open
‘ Some processing
conPubs.Close

There is no difference between the two, and if you use the former method, then
the ConnectionString property is filled in for you.
 2004 Tau Yenny, SI - Binus
Download