How To Connect To Database
How To Connect To Database
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
Sample code
o
2.
3.
4.
5.
6.
7.
Browser
Server
7
2
6
ASP Engine
ADO Objects
5
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 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
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")
"
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 "
"
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>
RecordSet Properties
BOF
o
beginning of a recordset
EOF
o
end of a recordset
RecordCount
o
number of records
RecordSet Methods
AddNew
o
Close
o
Delete
o
MoveNext
o
Open
o