Exercises :Active Server Pages (ASP)
Exercises :Active Server Pages (ASP)
Active Server Pages has the capability
of working with the information stored in databases. The connection of web servers (web servers
need to be running ASP technology) to databases is through ActiveX Data Objects
(ADO) technology. Basically ADO is a library
of objects that enable applications to communicate with 'data sources' (does
not have to be a database necessarily.
It could be the file system of your computer. Microsoft's vision of 'universal data'). In
this tutorial we will use;
1. The
connection object to connect to the
data source for reading data.
2. The
recordset object to represent the
particular data we will read.
3. The
command object to allow invoking
operations (for example deleting data) on the data source.
The main task is to configure the properties
of each object. Then one of the methods
of the object is used to carry out the particular task at hand (for example
reading/writing data to/from datasource).
Exercise
1 Read
appointments from the database 'database.mdb'.
You are provided with;
·
A space on a server that the server
administrator has named 'pertinax'. The
URL for the server is 'http://pertinax.cms.shu.ac.uk/' and the URL for
your space on that server is 'http://pertinax.cms.shu.ac.uk/general/your
directory'.
A
username allows access to your particular space.
·
A password.
·
A Data Source Name (DSN).
·
A database (Access database) that
server administrator has named 'database.mdb'.
Note
that server administrator has tied your DSN to a database named 'database.mdb'
in your space. This means one DSN for
one database.
You have to use the recordset object for storing the data
that comes from the database that has been defined in the connection object. There are five steps to extract data and present
it.
Tasks;
A. Create
recordset object using createobject().
B. Create
the SQL statement for the retrieval of data.
C. Define
a connection object for the recordset
object to use as active connection.
D. Use
the recordset's open method, passing
the SQL statement to populate the recorset
with data from the source.
E. Create
HTML that formats the display of data.
1. Now
let us construct the HTML page.
For
this purpose this has to be a simple page.
It has to have a form that would include a button. If button is clicked we would connect to the
ASP file called say 'car_booking.asp'.
The bit of code that does this is
action="http://hadrian.cms.shu.ac.uk/general3/your
directory/car_booking.asp">
Save
the file in your home-space and name it 'car_booking.html'.
2. Now
let us construct the ASP page. (Do not
include the text inside the curly brackets)
<%@
Language=VBScript %>
{Task
A}
<%set
objdbconnection = server.createobject("ADODB.Connection")
{Task
C&D}
objdbconnection.open
"Your DSN"
{Task
B}
SQLquery = "select
fname,lname,carname,apptime,appday from clients,cars,appointments where
clients.clientid=appointments.clientid and cars.carid=appointments.carid"
set
rslist = objdbconnection.execute(sqlquery)
%>
{Task
E}
<%while
not rslist.eof%>
<%rslist.movenext
wend%>
3. Download the database
"database.mdb"
4. FTP the two files and the database to the
server.
5. Use your browser window to navigate to your
server space. You should see the three
items.
6.
Open the HTML file and click on the button.
You should see the appointments in your browser window.
Exercise 2
For this exercise you could try to
write to the database. A little bit more
complicated. We now need three files and
the database on the server.
1. You could alter the 'car_booking.html'
to include another form that would provide a second button say 'Add a New Car
to Database'. This form would connect to
a ASP file called say 'add_car.asp'.
This bit of code could be
action="http://pertinax.cms.shu.ac.uk/general/your
directory/add_car.asp">
2. 'add_car.asp file would read one
complete record for table 'cars' in the
database. You would need a form here
that would provide a button say 'Add Car to Database' that when clicked would
connect to a third file named say 'car_added.asp'. The bit of the code that does this could be
Action="http://hadrian.cms.shu.ac.uk/general3/your
directory/car_added.asp">
Car
ID
Car
Name
Car
Size (Choose a number from 1 to 4)
3. The third file, in this case called
'car_added.asp' inserts the record into the database. Just one line of code is required....
Exercises :Active Server Pages (ASP)
0 commentaires: