Vous êtes sur la page 1sur 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 POLYTECHNC
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.

Vous aimerez peut-être aussi