You are on page 1of 6

8hort Course Lab Exercise

Copyright © 2002 Temasek Polytechnic
Continuing Education Centre (CEC)
Master Active Server Pages in 24 Hours / 10 Oct 2002


1


TEMA8EK POLYTECHN¡C
TEMA8EK ¡T 8CHOOL
Active 8erver Pages 3.0


Laboratory Exercise 4
8erver 8ide 8cripting - A8P { Database Access }

At the end of this lab exercise, students should be able to:

• Connect to a Access database using ASP (VBScript) programming
• Read from a Access database table using ASP (VBScript) programming
• Insert records to a Access database table using ASP (VBScript) programming



Please read “Day 15, 16 & 17” of the textbook.
The lecturer will go through the example programs with you. Please try the exercises
(Just do it) yourself after the examples.

0 Create a Access database
Create an Access database called WidgetWorld.mdb, save it into c:\temp\yourname\…
Refer to P520 (or the subject web site) for the table information.

1 Opening the connection and list properties of connection (P514)

The Program
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="& _
server.mappath("WidgetWorld.mdb")

Dim objProp
For Each objProp in Conn.Properties
Response.Write objProp.Name & ": " & objProp.Value & "<BR>"
Next

Conn.Close
Set Conn=Nothing

%>


2.1 Opening a connection, save as a file (P514)

The Program (use Macromedia Dreamweaver)

8hort Course Lab Exercise



Copyright © 2002 Temasek Polytechnic
Continuing Education Centre (CEC)
Master Active Server Pages in 24 Hours / 10 Oct 2002


2

<%
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & _
server.mappath("WidgetWorld.mdb")

%>

2.2 List Properties using the connection created (P514)

The Program
<% @ Language=VBScript %>
<% Option Explicit %>

<!--#include file="connect02.asp"-->

<html>
<head><title>database</title></head>
<body>

<%
Dim objProp
For Each objProp in objConn.Properties
Response.Write objProp.Name & ": " & objProp.Value & "<BR>"
Next

objConn.Close
Set objConn=Nothing

%>

</body>
</html>


3.1 Reading from a database (P521)

The program
<% @ Language=VBScript %>
<% Option Explicit %>

<!--#include file="connect02.asp"-->

<html>
<head><title>database</title></head>
<body>

<%
Const adOpenForwardOnly = 0

8hort Course Lab Exercise



Copyright © 2002 Temasek Polytechnic
Continuing Education Centre (CEC)
Master Active Server Pages in 24 Hours / 10 Oct 2002


3

Const adLockOptimistic = 3
Const adLockPessimistic = 2
Const adLockReadOnly = 1
Const adCmdTable = 2

Dim objRS
Set objRS = Server.CreateObject ("ADODB.Recordset")
objRS.Open "tblExpenses", objConn, , , adCmdTable

Do While Not objRS.EOF
Response.Write "Name: " & objRS("EmployeeName")
Response.Write "<P>"
objRS.MoveNext
Loop

objRS.Close
set objRS=Nothing
objConn.Close
Set objConn=Nothing

%>

</body>
</html>

3.2 Reading from a database (P521)

The program
<% @ Language=VBScript %>
<% Option Explicit %>

<!--#include file="connect02.asp"-->

<html>
<head><title>database</title></head>
<body>

<%
Const adOpenForwardOnly = 0
Const adLockOptimistic = 3
Const adLockPessimistic = 2
Const adLockReadOnly = 1
Const adCmdTable = 2

Dim objRS
Set objRS = Server.CreateObject ("ADODB.Recordset")
objRS.Open "tblExpenses", objConn, , , adCmdTable

Do While Not objRS.EOF

8hort Course Lab Exercise



Copyright © 2002 Temasek Polytechnic
Continuing Education Centre (CEC)
Master Active Server Pages in 24 Hours / 10 Oct 2002


4

Response.Write "Name: " & objRS("EmployeeName") & "<br>"
Response.Write "Employee Number: " & objRS("EmployeeNumber") & "<br>"
Response.Write "Expense Amout: " & objRS("ExpenseAmount") & "<br>"
Response.Write "Expense Reason: " & objRS("ExpenseReason") & "<br>"
Response.Write "<P>"
objRS.MoveNext
Loop

objRS.Close
set objRS=Nothing
objConn.Close
Set objConn=Nothing
%>

</body>
</html>

The Exercise (Just do it)
(P531)Create a database, with a table called tblEmployees, it should have the following
fields:
Field Name Datatype
Name Text
IDNumber Number
Salary Currency
BeganWorking Date/Time

Write an ASP page to open the table and display the records. Test it first with the table
empty and then add at least six entries and test it again.


4.1 Create a Access database
Create an Access database called users.mdb, save it into c:\temp\yourname\…
Refer to the subject web site for the table information.

4.2 Add record to a table and display table (P535)

The Program
<% @ Language=VBScript %>
<% Option Explicit %>

<!--#include file="connect03.asp"-->

<html>
<head><title>database</title></head>
<body>

<%
Const adOpenForwardOnly = 0
Const adLockOptimistic = 3

8hort Course Lab Exercise



Copyright © 2002 Temasek Polytechnic
Continuing Education Centre (CEC)
Master Active Server Pages in 24 Hours / 10 Oct 2002


5

Const adLockPessimistic = 2
Const adLockReadOnly = 1
Const adCmdTable = 2

Dim objRS
Set objRS = Server.CreateObject ("ADODB.Recordset")
objRS.Open "tblUsers", objConn, , adLockOptimistic, adCmdTable

objRs.AddNew
objRs("FirstName") = "Gene"
objRs("LastName") = "Williams"
objRs("Email") = "blank@blah.net"
objRs("Username") = "ww123"
objRs("Password") = "pass"
objRs.Update
objRs.MoveFirst
%>

<table width="600" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Name</td>
<td>EMail</td>
<td>Username</td>
</tr>

<% Do While Not objRs.EOF %>

<tr>
<td> <%=objRS("LastName")%> , <%=objRS("FirstName")%> </td>
<td> <%=objRS("Email")%> </td>
<td> <%=objRS("Username")%> </td>
</tr>

<%
objRS.MoveNext
Loop
objRS.Close
set objRS=Nothing
objConn.Close
Set objConn=Nothing

%>
</table>
</body>
</html>





8hort Course Lab Exercise



Copyright © 2002 Temasek Polytechnic
Continuing Education Centre (CEC)
Master Active Server Pages in 24 Hours / 10 Oct 2002


6


The Exercise (Just do it)
(P537) Create a page for your users to register. This is a two-page process. The first
page (a .htm file) has the form that the user enters his information into. The form then
passes that information to the second page (a .asp file), which enters the information
into the database.

mdb") %> 2.mdb)}.Write objProp.Properties Response.Open "DRIVER={Microsoft Access Driver (*.Name & ": " & objProp.CreateObject("ADODB.Close Set objConn=Nothing %> </body> </html> 3. DBQ=" & _ server.mappath("WidgetWorld.<% Dim objConn Set objConn = Server.asp"--> <html> <head><title>database</title></head> <body> <% Dim objProp For Each objProp in objConn.1 Reading from a database (P521) The program <% @ Language=VBScript %> <% Option Explicit %> <!--#include file="connect02.asp"--> <html> <head><title>database</title></head> <body> <% Const adOpenForwardOnly = 0 Copyright © 2002 Temasek Polytechnic Continuing Education Centre (CEC) Master Active Server Pages in 24 Hours / 10 Oct 2002 2 .2 List Properties using the connection created (P514) The Program <% @ Language=VBScript %> <% Option Explicit %> <!--#include file="connect02.Connection") objConn.Value & "<BR>" Next objConn.

Recordset") objRS. objConn.Close Set objConn=Nothing %> </body> </html> 3. .Open "tblExpenses".Write "<P>" objRS.CreateObject ("ADODB.2 Reading from a database (P521) The program <% @ Language=VBScript %> <% Option Explicit %> <!--#include file="connect02.Const adLockOptimistic = 3 Const adLockPessimistic = 2 Const adLockReadOnly = 1 Const adCmdTable = 2 Dim objRS Set objRS = Server. objConn.CreateObject ("ADODB. adCmdTable Do While Not objRS.Close set objRS=Nothing objConn. adCmdTable Do While Not objRS.MoveNext Loop objRS. .EOF Response.Recordset") objRS. .Open "tblExpenses".Write "Name: " & objRS("EmployeeName") Response.asp"--> <html> <head><title>database</title></head> <body> <% Const adOpenForwardOnly = 0 Const adLockOptimistic = 3 Const adLockPessimistic = 2 Const adLockReadOnly = 1 Const adCmdTable = 2 Dim objRS Set objRS = Server.EOF Copyright © 2002 Temasek Polytechnic Continuing Education Centre (CEC) Master Active Server Pages in 24 Hours / 10 Oct 2002 3 . .

with a table called tblEmployees.2 Add record to a table and display table (P535) The Program <% @ Language=VBScript %> <% Option Explicit %> <!--#include file="connect03.Write "Expense Reason: " & objRS("ExpenseReason") & "<br>" Response.Close Set objConn=Nothing %> </body> </html> (P531)Create a database.mdb. it should have the following fields: Field Name Datatype Name Text IDNumber Number Salary Currency BeganWorking Date/Time Write an ASP page to open the table and display the records.Write "Expense Amout: " & objRS("ExpenseAmount") & "<br>" Response.Write "Name: " & objRS("EmployeeName") & "<br>" Response.asp"--> <html> <head><title>database</title></head> <body> <% Const adOpenForwardOnly = 0 Const adLockOptimistic = 3 Copyright © 2002 Temasek Polytechnic Continuing Education Centre (CEC) Master Active Server Pages in 24 Hours / 10 Oct 2002 The Exercise (Just do it) 4 . 4. save it into c:\temp\yourname\… Refer to the subject web site for the table information.1 Create a Access database Create an Access database called users.Write "Employee Number: " & objRS("EmployeeNumber") & "<br>" Response.MoveNext Loop objRS.Response.Write "<P>" objRS. Test it first with the table empty and then add at least six entries and test it again. 4.Close set objRS=Nothing objConn.

Const adLockPessimistic = 2 Const adLockReadOnly = 1 Const adCmdTable = 2 Dim objRS Set objRS = Server.AddNew objRs("FirstName") = "Gene" objRs("LastName") = "Williams" objRs("Email") = "blank@blah. .Recordset") objRS. objConn. adCmdTable objRs.Update objRs.Close Set objConn=Nothing %> </table> </body> </html> Copyright © 2002 Temasek Polytechnic Continuing Education Centre (CEC) Master Active Server Pages in 24 Hours / 10 Oct 2002 5 .Open "tblUsers".MoveNext Loop objRS.EOF %> <tr> <td> <%=objRS("LastName")%> .CreateObject ("ADODB.net" objRs("Username") = "ww123" objRs("Password") = "pass" objRs. <%=objRS("FirstName")%> </td> <td> <%=objRS("Email")%> </td> <td> <%=objRS("Username")%> </td> </tr> <% objRS. adLockOptimistic.MoveFirst %> <table width="600" border="1" cellspacing="0" cellpadding="0"> <tr> <td>Name</td> <td>EMail</td> <td>Username</td> </tr> <% Do While Not objRs.Close set objRS=Nothing objConn.

The first page (a . The Exercise (Just do it) Copyright © 2002 Temasek Polytechnic Continuing Education Centre (CEC) Master Active Server Pages in 24 Hours / 10 Oct 2002 6 .asp file).htm file) has the form that the user enters his information into.(P537) Create a page for your users to register. which enters the information into the database. The form then passes that information to the second page (a . This is a two-page process.