You are on page 1of 29

Moving Forward with ASP:

Working with Data

29

Session Objectives

Data Driven Web-site


The ADO Framework
ADO Object Model
Connect to the Database
Manipulate that Data with ADO

29

Data Driven Web-site

The Active Platform reflects Microsoft's


ideas about how a desktop computer and
a server computer should communicate.
It consists of two parts: The Active
Desktop and the Active Server.

The Active Desktop refers to the client side, or


the user's side, where HTML files are displayed
on a web browser.
The Active Server refers to the server-side
component.

This consists of pages that can be


interpreted by the server, hence the term
Active Server Pages.
29

Data Driven Web-site

With ASP, programmers working on


Windows NT can tailor the way
pages are displayed based on
outside information.
A

different image could be displayed


each day of the week, or information
could be displayed based on a user's
age. This process, which is called
condition branching, allows ASP to
make decisions about what to display
based on certain criteria.
29

The ADO FrameWork

ADO, or ActiveX Data Objects, is a


collection of objects that allow
applications to communicate with
data sources in a consistent
manner.
Each object in the ActiveX Data
Objects model represents a
particular piece that is used when a
program works with data.
29

The ADO FrameWork

The bulk of the work you do with


ADO is going to be configuring the
properties of each object.
Then you call the methods of the
objects in order to tell the data
source to retrieve the data you
want, add new data, and delete or
change existing data.
29

Understanding ODBC

ODBC is an interpreter that makes all


databases seem the same to one another
and to programs that have to interact
with them, regardless of the inner
working of each database
The benfit of using ODBC is that you need
to learn only one methodology to work
with many different database.
Practically all the database manufactures
provide software that you install that
allows their database to work with ODBC.
This software is called an ODBC Driver.
29

Understanding ODBC
I know how to
interact with
ODBC

I know how to
interact with
ODBC

I know how to
interact with
ODBC

Oracle

MS Access

DB2

All you ever need to


learn is how to work
with me

ODBC

Ask me for data


and I will get it
for you

29

Understanding OLE DB

ODBC only works with databases.


OLE DB is the first important step
toward implementating Microsofts
vision of Universal Data.
Universal Data is a model in which
all data can be treated the same
way regardless of the source.

29

Understanding OLE DB
MS Access

Oracle
Index Server
SQL
Server

Oracle

DB2

ODBC

OLE DB
29

10

ADO Object Model

Microsoft Standard
A programming interface providing data
access to programming languages such as
VBScript or JavaScript
connection

recordset

command

errors

29

11

methods properties

objects

ADO Object Model


connection

recordset

command

errors

open
execute
close

bof
eof

commandtext

count
item

open
movefirst
movenext
close

execute

29

12

Connecting to a Database (conts)

Use ADO Objects in code to


Connecting

to Database

Retrieve
Manipulate

data

Database may reside


On

web server
On database server connected to web
server (n-tier)

29

13

Connecting to Data
asp

web
server

database
server

ADO connection
SQL

Internet
html
html
29

14

Connection Object

Methods
Open(conString,

user, pass)

Close()
Execute(strSQL)

Properties
ConnectionTimeout
ConnectionString

29

15

ADO Connections

Defined by its ConnectionString


property
Series

of name and value pairs


separated from each other by
semicolons
"Driver={SQL Server}; Server=gwen;
Database=intern; User Id=bob;
Password=xxx;"

Three Types of Connections


ODBC

DSN
ODBC DSN-less
OLE DB

29

16

ODBC DSN Connections

ODBC (Open Database Connectivity)

DSN (Data Source Name)

Open Standard developed in early 1990s


Provides simple programming interface to
relational databases
A saved entry in the Windows ODBC applet
Defines all connection information
Provides a simple name to handle connectivity

Web server admin must


make ODBC entry
Conn.Open("DSN=DSNname;" +
"Uid=Username;" +
"Pwd=Password;)
29

17

ODBC DSN Connections


After configure an ODBC Entry
Eg: Create an ADO Connection Object
Dim objConn
Set
objCon=Server.CreateObject(ADODB.Con
nection)
Dim strDSN = DSN=Northwind
objConn.Open(strDSN)
29

18

ODBC DSN-less Connections

Still Uses ODBC


Does not use a saved DSN entry
Advantages

asp
ado
odbc
data

Quicker. Saves a call to Windows Registry.


Avoids having to have the DSN created on the
web server.
More portable.
More flexible. Can pass database names as
variables.

Conn.Open("Driver={Microsoft Access Driver (*.mdb)};" +


"Dbq=" & Server.Mappath("Internship.mdb") & ";" +
"Uid=Username;" +
"Pwd=Password;)
29
19

OLE DB Connections

OLE DB
Microsoft

standard supported only on


Microsoft operating systems
Provides access to relational and nonrelational data
Generally less prone to errors than
ODBC
Dim Conn
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open("Provider=msdaora;" +
"Data Source=PSTDB;" +
"User Id=username;" +
"Password=password;) 29
20

Recordset Object

Methods

AddNew()
Delete()
Update()
Open()
Close

MoveFirst()
MoveLast()
MoveNext()
MovePrevious

Properties

BOF
EOF
AbsolutePage
AbsolutePosition
PageCount
PageSize
RecordCount

Constant

Value

Description

adCmdUnknown

Default

adCmdText

Text deffinition eg. SQL statement

adCmdTable

Name of Table

adCmdStoreProc

Store, or query within the data


29
source

21

Recordset Object (conts)


<!-- #include file=adojavas.inc -->
Syntax:
rs.Open(Source, Connection, CursorType, LockType, Options)

Note: CursorType adOpenForwardOnly (0),


adOpenKeyset (1), adOpenDynamic (2), or
adOpenStatic (3)

Dim objRs
Set objRs = Server.CreateObject(ADODB.Recordset)
objRs.Open(tblCustomer, objConn, , , adCmdTable)
29

22

eShopping Example

29

23

Using HTML Form Objects Form Tag

The <form></form> tags define the beginning


and ending of an entry form
Properties
Set

action to the ASP that will process the data


entered on the form.
Name the form whatever you want
Method always = POST

<form method="POST" action="AddNew.asp name="AddEvent">


text, text boxes, etc.
</form>
29

24

Using HTML Form Objects Drop-Down Menu

Similar to a Combo Box


Size

is number of lines that show

Set size=1 for traditional drop-down

Name

used for ASP reference


<option></option> define choices
<option selected> is default choice

<select size=1 name="Event"


<option selected> </option>
<option>Communication</option>
<option>Parts Received</option>
<option>In Shop</option>
29
</select>

25

Using HTML Form Objects Drop-Down Menu

Used to fill in choices from a recordset

<%SQL = "Select [RO] from tblCustomer


Dim RS
Set RS = Conn.Execute(SQL)%>
<select size="1" name="RO">
<%while(NOT RS.EOF)%>
<option><%Response.write(RS("RO"))%></option>
<%RS.MoveNext()
wend
RS.Close()
Set RS = Nothing%>
</select>
29

26

Using HTML Form Objects Drop-Down Menu (advanced)

Value Property

Allows you to track a value that is not shown

For example, you could track an ID while displaying


a name
Use value for ID value
Read value same as reading regular option

<select size="1" name=Emp">


<%while(NOT RS.EOF)%>
<option value=<%Response.write(RS(ID"))%>>
<%Response.write(RS(Name"))%></option>
<%RS.MoveNext()
wend..
29

27

Benefits

ASP is language independent: you


can use any language that supports
ActiveX scripts
Easy to learn (especially if you know
VBScript)
Security: it comes from the security
features of WindowsNT and IIS
ASP is free
29

28

Case Study
Using

Session Variables
Filling a Combo
Showing a Selected Record
Making And Saving Changes
Adding a New Record
Finding an Existing Record
Recordset Paging
Thanks for listening
29

29

You might also like