ActiveX Data Objects in ASP
-
Upload
safish-xavier -
Category
Documents
-
view
233 -
download
1
Transcript of ActiveX Data Objects in ASP
-
7/29/2019 ActiveX Data Objects in ASP
1/36
ASP-ADO
-
7/29/2019 ActiveX Data Objects in ASP
2/36
ActiveX Data Objects (ADO)
Does for Microsoft data sources what ODBC did for
databases
similar in concept to the combination JDBC and JNDI (and then
some)
The process of using ADO involves the four step process:
Connect to the database
Define the data you want
Manipulate the data
Display the data
-
7/29/2019 ActiveX Data Objects in ASP
3/36
ADO Software Architecture
VC++ VB Script Java
ADO
OLE DB
RDBMS E-mail Directory Services
-
7/29/2019 ActiveX Data Objects in ASP
4/36
ADO, ODBC and OLE DB
ADO is the strategic way for using ODBC data sources on the
Windows platform
ODBC was the MS way of connecting to and using databases on the
Windows platform.
OLE DB extends ODBC by using the concept of service providers to
add the ability to connect to other non-database data sources (MS
Active Directory (LDAP), e-mail servers (Exchange))
ODBC Data Sources; provider = MSDASQL
MS Index Server; provider = MSIDXS
MS Active Directory Server - provider = ADSD300Object
MS Jet databases - provider = Microsoft.Jet.OLEDB.3.51
MS SQL Server - provider = SQLOLEDB
Oracle Databases - provider = MSDAORA
-
7/29/2019 ActiveX Data Objects in ASP
5/36
Providers and Drivers
ADO
JET SQL Oracle
JET SQL Oracle
ODBC
Access SQL Oracle Access SQL Oracle
OLE DB
Providers
ODBC
Drivers
-
7/29/2019 ActiveX Data Objects in ASP
6/36
Accessing a Database in ASP
The common way to access a database from inside
an ASP page is to:
Create an ADO connection to a database Open the database connection
Create an ADO recordset
Open the recordset
Extract the data you need from the recordset
Close the recordset
Close the connection
-
7/29/2019 ActiveX Data Objects in ASP
7/36
DSN-less Database Connection
-
7/29/2019 ActiveX Data Objects in ASP
8/36
Without DSN - OLE DB
-
7/29/2019 ActiveX Data Objects in ASP
9/36
File DSN
------------- file.dsn -------
[ODBC]
DRIVER=Microsoft Access Driver (*.mdb)
ReadOnly=0UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=8
MaxBufferSize=512
ImplicitCommitSync=Yes
FIL=MS Access
DriverId=25
-
7/29/2019 ActiveX Data Objects in ASP
10/36
Using File DSN
-
7/29/2019 ActiveX Data Objects in ASP
11/36
ODBC Database Connection
With an ODBC connection, you can
connect to any database, on any computer inyour network, as long as an ODBC
connection is available.
-
7/29/2019 ActiveX Data Objects in ASP
12/36
An ODBC Connection to an MS
Access Database
To create a connection to a MS Access Database:
Open the ODBC icon in your Control Panel.
Choose the System DSN tab. Click on Add in the System DSN tab.
Select the Microsoft Access Driver. ClickFinish.
In the next screen, clickSelect to locate the database.
Give the database a Data Source Name (DSN).
ClickOK.
-
7/29/2019 ActiveX Data Objects in ASP
13/36
ADO Connection Object
The ADO Connection object is used to
create an open connection to a data source.
Through this connection, you can access
and manipulate a database.
-
7/29/2019 ActiveX Data Objects in ASP
14/36
Connection Object
Collections
-
7/29/2019 ActiveX Data Objects in ASP
15/36
Connection Object - Methods
-
7/29/2019 ActiveX Data Objects in ASP
16/36
Connection Object - Properties
-
7/29/2019 ActiveX Data Objects in ASP
17/36
Creating a ConnectionObject
Connection objects are created using the
CreateObject() function.
The only parameter is a string indicating the object tocreate
object_library_name.object
Dim cn
Set cn = Server.CreateObject(ADODB.Connection)
-
7/29/2019 ActiveX Data Objects in ASP
18/36
ConnectionString
Once the ConnectionObject is created we need to give it a
ConnectionString to logon to the datasource
ConnectionString specifies the : provider
datasource name
userid (optional)
password (optional)
Dim cnn
Dim str str = Provider=MSDASQL; Data Source=mydatasource; User
Id=;Password=
cnn.ConnectionString = str
-
7/29/2019 ActiveX Data Objects in ASP
19/36
Open() and Close()
Once the connection has been established the Connection must be
opened before using it
Likewise it should be closed when done.
Dim cnn
Dim str
Set cnn = CreateObject(ADODB.Connection)
str = Provider=MSDASQL.1,Data Source=mydatasource;
str = str & User TD=myuserid;Password=mypasscnn.ConnectionString = str
cnn.Open
..
cnn.Close
-
7/29/2019 ActiveX Data Objects in ASP
20/36
Recordset
Record set objects are the recipients of the result of a SQL query
create the Connection object the create a recordset object and associate
them
Dim cnn
Dim rs
set cnn = CreateObject(ADODB.Connection)
set rs = CreateObject(ADODB.Recordset)
associate the record set with the connection
rs.ActiveConnection = cnn
-
7/29/2019 ActiveX Data Objects in ASP
21/36
To use the Recordset, Open it
from previous code...
Dim SQL
SQL = SELECT..FROM..
rs.Open SQL
you can abbreviate this by doing the association at the same time as the Open
rs.Open SQL, cnn
this will eliminate the need for the rs.ActiveConnection = cnn statement
-
7/29/2019 ActiveX Data Objects in ASP
22/36
Getting the data out
The record set is a collection of rows, each row containing the data in
the order that it was asked for in the SQL statement, each column in
the row can be accessed by its column name
for: SELECT CourseName from CourseTable
Dim string
While Not rs.EOF
string = string & rs(CourseName) &
rs.MoveNextWend
-
7/29/2019 ActiveX Data Objects in ASP
23/36
Getting data in
To get data into an ADO data source use the ADO command object.
Use this for the SQL
insert
update
and delete statements
Dim SQL
Dim Cmd
Set cmd = CreateObject(ADODB.Command) create a command objectSQL = INSERT into ..
cmd.CommandText = SQL set the CommandTexy property of the CommandObject
cmd.ActiveConnection = cnn
cmd.Execute
-
7/29/2019 ActiveX Data Objects in ASP
24/36
Display records from a database in an HTML Page
-
7/29/2019 ActiveX Data Objects in ASP
25/36
Put the data in a table with colors and titles
-
7/29/2019 ActiveX Data Objects in ASP
26/36
Another example, make data on page sortable by user
Company
Contact Name
-
7/29/2019 ActiveX Data Objects in ASP
27/36
Using the Recordset Object
This example returns the value of the first column in the first two records:
ALFKI
ANTON
This example returns the value of the first three columns in the first record:
ALFKI
Alfreds FutterkisteMaria Anders
-
7/29/2019 ActiveX Data Objects in ASP
28/36
ADO.NET
Under .NET, ADO hasnt changed much but the way you use it has
VB has changed extensively under .NET so we wont address its use with
VB (developer community isnt real happy about this)
the ADO API is pretty consistent across the MS supported .NETlanguages
Consists of the namespaces and classes that manage accessing backend
databases.
five namespaces
the most common are: Ststem.Data
System.Data.OleDB
System.Data.SqlClient
hundreds of classes
-
7/29/2019 ActiveX Data Objects in ASP
29/36
the .NET Data Provider
DataReader
SelectCommand UpdateCommand
InmsertCommand DeleteCommand
Transaction Parameters
Connection Command
DB
.NET Data Provider
-
7/29/2019 ActiveX Data Objects in ASP
30/36
.NET DataSet
ConstraintCollection
DataColumnCollection
DataRowCollection
DataTableDataTable
DataTableCollection
DataSet
DataRelationshipCollection
-
7/29/2019 ActiveX Data Objects in ASP
31/36
System.Data Namespace
Consists mainly of classes that make up
ADO.NET
Allows a developer to build components
that manage data from data sources
-
7/29/2019 ActiveX Data Objects in ASP
32/36
System.Data.OleDb Namespace
Ole DB .Net Data Provider
Designed to be a replacement for ODBC
Used for accessing different kinds of data
stores uniformly
non-relational databases
Dbase, Foxpro, Access
Spreadsheets
-
7/29/2019 ActiveX Data Objects in ASP
33/36
System.Data.SqlClient Namespace
.NET data provider for MS SQL Server
classes bypass OLE DB to interact directly
with SQL Server
increase performance for SQL Server access
-
7/29/2019 ActiveX Data Objects in ASP
34/36
MySql.Data.MySqlClient
namespace containing classes for accessing
MySQL databases
provided by MySQL organization
-
7/29/2019 ActiveX Data Objects in ASP
35/36
IBM.Data.DB2 namespace
namespace for using IBM DB2 databases
with .NET
Provided by IBM with the DB2 product
-
7/29/2019 ActiveX Data Objects in ASP
36/36
Oracle.DataAccess.Client
namespace used for accessing Oracle
databases from .NET
provided by Oracle with Oracle product