Asp-Ado
Asp-Ado
Singh Paramjit
Accessing a Database from an ASP Page
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
What is ADO?
ADO can be used to access databases from your web
pages.
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 Database Connection
Before a database can be accessed from a web page, a
database connection has to be established.
Create an ODBC Database Connection
If you have an ODBC database called "northwind" you can
connect to the database with the following ASP code:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "northwind"
%>
With an ODBC connection, you can connect to any
database, on any computer in your network, as long as an
ODBC connection is available.
ADO Recordset
Create an ADO Table Recordset
After an ADO Database Connection has been created, as
demonstrated in the previous chapter, it is possible to create
an ADO Recordset.
Suppose we have a database named "Northwind", we can
get access to the "Customers" table inside the database with
the following lines:
<% set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb“
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Customers", conn %>
ADO Recordset
Create an ADO SQL Recordset
We can also get access to the data in the "Customers"
table using SQL:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb“
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
%>
ADO Recordset
Extract Data from the Recordset
After a recordset is opened, we can extract data from
recordset.
Suppose we have a database named "Northwind", we can get
access to the "Customers" table inside the database with the
following lines:
<% set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb“
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from Customers", conn
for each x in rs.fields
response.write(x.name)
response.write(" = ")
response.write(x.value)
next %>
Display the Field Names and Field Values
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb“
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM Customers", conn
Cont…
do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br />")
next
Response.Write("<br />")
rs.MoveNext
loop
rs.close
conn.close
%>
</body>
</html>
Output Result
CustomerID = ALFKI
CompanyName = Alfreds Futterkiste
ContactName = Maria Anders
Address = Obere Str. 57
City = Berlin
PostalCode = 12209
Country = Germany
ADO Queries
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT Companyname, Contactname FROM
Customers WHERE CompanyName LIKE 'A%'"
rs.Open sql, conn
%>
ADO Queries Cont….
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close %>
</table>
</body>
</html>
Output
Companyname Contactname
Alfreds Futterkiste Maria Anders
ADD Record
<html>
<body>
<form method="post" action="demo_add.asp">
<table>
<tr>
<td>CustomerID:</td>
<td><input name="custid"></td>
</tr><tr>
<td>Company Name:</td>
<td><input name="compname"></td>
</tr>
<td>Contact Name:</td>
<td><input name="contname"></td>
</tr><tr>
</table>
<br /><br />
<input type="submit" value="Add New">
<input type="reset" value="Cancel">
</form>
</body>
</html>
Demo_add.asp
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
Demo_add.asp
sql="INSERT INTO customers
(customerID,companyname,"
sql=sql &
"contactname,address,city,postalcode,country)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("custid") & "',"
sql=sql & "'" & Request.Form("compname") & "',"
sql=sql & "'" & Request.Form("contname") & "',"
Demo_add.asp
on error resume next
conn.Execute sql,recaffected
if err<>0 then
Response.Write("No update permissions!")
else
Response.Write("<h3>" & recaffected & " record
added</h3>")
end if
conn.close
%>
</body>
</html>
Thanx!