0% found this document useful (0 votes)
33 views19 pages

How To Connect To Database

The document discusses how to connect to a database and retrieve data from it using ASP. It describes using ODBC and ADO to connect, forming SQL statements, opening connections and recordsets, navigating through recordsets, and retrieving field values. Code samples are provided to connect to a database, execute a SQL statement, loop through the recordset, and output field values. Key objects and methods like Connection, Recordset, MoveNext, and field access are explained.

Uploaded by

Yuv Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views19 pages

How To Connect To Database

The document discusses how to connect to a database and retrieve data from it using ASP. It describes using ODBC and ADO to connect, forming SQL statements, opening connections and recordsets, navigating through recordsets, and retrieving field values. Code samples are provided to connect to a database, execute a SQL statement, loop through the recordset, and output field values. Key objects and methods like Connection, Recordset, MoveNext, and field access are explained.

Uploaded by

Yuv Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

How to Connect to Database

ODBC (Open Database Connectivity)


ADO (ActiveX Data Object)
ASP Code To Connect to Database
Recordset Object
Navigating through Recordset

ODBC
Stands for Open Database Connectivity
Is a standard database access method developed
by the SQL Access group in 1992
Has the goal of making it possible to access any
data from any application, regardless of DBMS
being used
Uses a middle layer called a driver between the
application (e.g., SQL program) and the DBMS
(e.g., MS Access)

ODBC
SQL Applications
(Programs)
ODBC Driver Manager
(provided by MS)
ODBC drivers for specific DB
(provided by specific vendor)
SQL DBMS or Access DBMS

ADO
Stands for ActiveX Data Objects
Is a high-level interface for accessing data
objectsway of looking at data objects
(developed by Microsoft)
What is ADOs relationship to ASP?
o

ASP code can manipulate ADO objects, using


VBScript sintax

Sample code
o

See later slides

ADO on the Web Server


1.

Browser requests ASP


page from Server

2.

Server sends ASP


page to ASP Engine

3.

ASP Engine executes


Server-side scripts

4.

ASP Engine calls


ADO objects (if any)

5.
6.
7.

ADO objects do their


thing
ASP Engine returns
HTML to Server
Server returns HTML
to Browser

Browser

Server
7
2
6
ASP Engine

ADO Objects
5

ADO & COM


ADO (ActiveX Data Objects) objects are COM
(Component Object Model) objects specifically
designed to interact with ODBC objects.
The purpose of ADO objects is to gain access to
database through ASP
ODBC (Open Database Connectivity) -standard database access method developed by
MS --Access, dBase4, Paradox, FoxPro, etc.

ADO Objects
Command
o to give command to a database

Connection
o link between ASP application and the database

RecordSet
o will be used most often
o container for a Cursor, a temporary table

To Access Database from ASP


Page
1. Form an SQL statement.
2. Create and Open ADO connection to the
database.
3. Create and Open ADO recordset object.
4. Extract data from the recordset
5. Close recordset object
6. Close connection object

To Form SQL Statement


<%
' Form SQL String
Dim strSQL
strSQL=""
strSQL=strSQL & " SELECT firstName, lastName
strSQL=strSQL & " FROM Customers
. . .
. . .
%>

"

To Connect to Database
// Assume that strSQL contains SQL statement to be
// executed.
Dim conn, recSet
Set conn=Server.CreateObject(ADODB.Connection)
conn.Provider=Microsoft.Jet.OLEDB.4.0
conn.Open Server.MapPath(mydatabase.mdb)
Set recSet = Server.CreateObject("ADODB.Recordset")
conn.Open strSQL conn

ASP Code to Connect to Database


(Alternate)
// Assume that strSQL contains SQL statement to be
// executed.
Dim DBSource, conn, recSet
DBSource =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("dealer.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
Set recSet = Server.CreateObject("ADODB.Recordset")
conn.Open DBSource
' Execute SQL
recSet.Open strSQL, conn, 1, 1

Connecting to DB
(Alternate)
1. Create SQL Statement
strSQL=SELECT firstName, lastName FROM Customers

2. Connect to Physicsl DB
DBSource =
"Provider=Microsoft.Jet.OLEDB.4.0; _
Data Source=" & Server.MapPath("dealer.mdb")

o Create Connection Object

Set conn = Server.CreateObject("ADODB.Connection")

Create RecordSet object

Set recSet = CreateObject(ADODB.RecordSet)

Open the RecordSet

recSet.Open strSQL, conn, 1, 1

Complete Code for


Connecting to Database
<%
Dim conn, recSet, strSQL
' Form SQL String
strSQL=""
strSQL=strSQL & " SELECT firstName, lastName
strSQL=strSQL & " FROM Customers "

"

' Open database connection


Set conn=Server.CreateObject(ADODB.Connection)
conn.Provider=Microsoft.Jet.OLEDB.4.0
conn.Open Server.MapPath(mydatabase.mdb)
Set recSet = Server.CreateObject("ADODB.Recordset")
conn.Open strSQL conn
%>

RecordSet
Current
Record
Pointer

Navigating through a
RecordSet
If recSet.EOF and recSet.BOF Then
no records
Response.Write(No matching records found)
Else
Do While Not recSet.EOF
val1 = recSet(firstName)
val2 = recSet(firstName)
Reponse.Write(val1 & val2 & <br>)
recSet.MoveNext
Loop
End If

Complete Code
<%@ language=VBScript %>
<%
Dim strSQL
strSQL=""
strSQL=strSQL & " SELECT firstName, lastName
strSQL=strSQL & " FROM Customers "

"

' Open database connection

Set conn=Server.CreateObject(ADODB.Connection)
conn.Provider=Microsoft.Jet.OLEDB.4.0
conn.Open Server.MapPath(mydatabase.mdb)
Set recSet=Server.CreateObject(ADODB.Recordset)
conn.Open strSQL, conn
%>

<html>
<head>
<title>All Customers</title>
</head>
<body>
<h2>All Customers</h2>
<%
Do While not recSet.eof
Response.Write(recSet(firstName") & " " & _
recSet("nameLast") &
recSet.MoveNext
loop
recSet.close
Set recSet = Nothing
conn.close
Set conn = Nothing
%>
</body>
</html>

" <br> ")

RecordSet Properties
BOF
o

beginning of a recordset

EOF
o

end of a recordset

RecordCount
o

number of records

RecordSet Methods
AddNew
o

Add new record to the record set

Close
o

Close the recordset

Delete
o

Delete the current record

MoveNext
o

Move record pointer to the next record

Open
o

Open the recordset (execute the query)

You might also like