Download Introduction to Active Server Pages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Working with Database in ASP
 ADO Object Model
 Database Connection
 Record Set
Query Examples
Update & Insertion Examples
An Application Example
ADO Object Model




ADO is a Microsoft technology
ADO stands for ActiveX Data Objects
ADO is a Microsoft Active-X component
ADO is automatically installed with Microsoft
IIS
 ADO is a programming interface to access data
in a database
 ADO can be accessed from within your Active
Server Pages
Relationships Among the Objects
Accessing a Database from an ASP Page
The normal way to access a database from
inside an ASP page is to:
1. Create an ADO connection to a
database
2. Open the database connection
3. Create an ADO recordset
4. Open the recordset
5. Extract the data you need from the
recordset
6. Close the recordset
7. Close the connection
Database Connection
The easiest way to connect to a database is to use a
DSN-less connection. A DSN-less connection can be
used against any Microsoft SQL database on your
web site.
If you have a database called "northwind " located in
your SQL Server, you can connect to the database
with the following ASP code:
Database Connection, cont.
1.
Create a file called “ADOConnection.asp” and type in
the code below
<%
dim connobj
dim connstr
connstr = "Provider=SQLOLEDB;Data
Source=MyServer;Database=Northwind;User
Id=NorthUser;Password=abc;"
set connobj = Server.CreateObject("ADODB.Connection")
connobj.Open (connstr)
connobj.Close
set connobj = nothing
%>
2. 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.
Create an ADO Table Recordset
1.Create the file “ADORecordset.asp” and type in the code
below
<%
dim connobj
dim connstr
dim recobj
connstr = "Provider=SQLOLEDB;Data
Source=MYSERVER;Database=Northwind;User
Id=NorthUser;Password=abc;"
set connobj = Server.CreateObject("ADODB.Connection")
connobj.Open (connstr)'this part create and assign the
recordset.
set recobj = Server.CreateObject("ADODB.Recordset")
'open the table customer using connobj as the default
connection.
recobj.Open "Customers", connobj
Response.Write("Table Customers opened.")
connobj.Close
recobj.close
Extract Data from the Recordset
Create a file called “ADORecordset2.asp” and type in the code
below:
<%
dim connobj
dim connstr
dim recobj
connstr = "Provider=SQLOLEDB;Data
Source=MYSERVER;Database=Northwind;User
Id=NorthUser;Password=abc;"
set connobj = Server.CreateObject("ADODB.Connection")
connobj.Open (connstr)
'this part create and assign the recordset.
set recobj = Server.CreateObject("ADODB.Recordset")
'open the table customer using connobj as the default
connection.
recobj.Open "Customers", connobj
Response.Write("Table Customers opened.<br />")
'retrieve data
Extract Data from the Recordset, cont.
for each x in recobj.fields
response.write(x.name)
response.write(" = ")
response.write(x.value & "<br />")
next
connobj.Close
recobj.Close
set recobj = nothing
set connobj = nothing
%>
Closing Recordset and closing Connection
1.After using the recordset and connection object, it
is important to properly close and destroy the
objects.
2.Below is the 4 lines necessary to perform such
operations.
Recobj.close
Connobj.close
Set recobj = nothing
Set connobj = nothing
Database Connection Examples
ODBC source connection example:
Set myDBConnection = Server.CreateObject( "ADODB.Connection" )
ConnStr = “DSN=airline db; uid=cis8490; pwd=test”
myDBConnection.open connStr
OLE connection example
Set myDBConnection = Server.CreateObject( "ADODB.Connection " )
myDBConnection.Open “PROVAIDER=SQLOLEDB; DATA SOURCE
= yourserver; UID=sa; PWD=secret; DATABASE=Pubs ”
ODBC Driver For Trusted Connection security connection
example
Set myDBConnection = Server.CreateObject( "ADODB.Connection " )
myDBConnection.Open "Driver={SQL Server};" & _
"Server=MyServerName;" & _
"Database=myDatabaseName;" & _
"Uid=;" & "Pwd=;"
Recordset Objects
A Recordset object: the entire set of records
Opened from a base table
Created by the results of an executed query
Record object: an record in the recordset
Current record
Only a single record can be referenced at any
time
Field object: an attribute (field) in a record
Can be referenced by the attribute name or
by its position (index)
Related documents