21 – Web applications: Databases & ASP Mark Dixon, SoCCE SOFT 131 Page 1 Session Aims & Objectives • Aims – To introduce the fundamental ideas involved in using server-side code to read data from databases • Objectives, by end of this week’s sessions, you should be able to: – create an ASP web page that displays data read from a database Mark Dixon, SoCCE SOFT 131 Page 2 People Database ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Mark Dixon, SoCCE Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 SOFT 131 email [email protected] [email protected] [email protected] [email protected] [email protected] Page 3 Example 1: People (html) People.asp <html> <head> <title>Personal Address Book</title> </head> <body> <p><center><b><font size=+2> Personal Address Book </font></b></center> <% ' ASP code will go here (next slide) %> </body> </html> Mark Dixon, SoCCE SOFT 131 Page 4 Example 1: People (ASP) <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> rs <% Const cs = "Provider=…;Data Source=D:\People.mdb; " Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs Do Until rs.EOF Response.Write rs.Fields("Surname").Value Response.Write "<br>" rs.MoveNext Loop rs.Close Set rs = Nothing %> ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 email [email protected] [email protected] [email protected] [email protected] [email protected] </body> </html> Mark Dixon, SoCCE SOFT 131 Page 5 Example 1: People (recordset 1) <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> rs <% Const cs = "…" Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs Do Until rs.EOF Response.Write rs.Fields("Surname").Value Response.Write "<br>" rs.MoveNext Loop rs.Close Set rs = Nothing %> ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 email [email protected] [email protected] [email protected] [email protected] [email protected] Dixon </body> </html> Mark Dixon, SoCCE SOFT 131 Page 6 Example 1: People (recordset 2) <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> rs <% Const cs = "…" Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs Do Until rs.EOF Response.Write rs.Fields("Surname").Value Response.Write "<br>" rs.MoveNext Loop rs.Close Set rs = Nothing %> ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 email [email protected] [email protected] [email protected] [email protected] [email protected] Dixon Smith </body> </html> Mark Dixon, SoCCE SOFT 131 Page 7 Example 1: People (recordset 3) <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> rs <% Const cs = "…" Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs Do Until rs.EOF Response.Write rs.Fields("Surname").Value Response.Write "<br>" rs.MoveNext Loop rs.Close Set rs = Nothing %> ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 email [email protected] [email protected] [email protected] [email protected] [email protected] Dixon Smith Jones </body> </html> Mark Dixon, SoCCE SOFT 131 Page 8 Example 1: People (recordset 4) <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> rs <% Const cs = "…" Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs Do Until rs.EOF Response.Write rs.Fields("Surname").Value Response.Write "<br>" rs.MoveNext Loop rs.Close Set rs = Nothing %> ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 email [email protected] [email protected] [email protected] [email protected] [email protected] Dixon Smith Jones Bloggs </body> </html> Mark Dixon, SoCCE SOFT 131 Page 9 Example 1: People (recordset 5) <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> People.asp rs <% Const cs = "…" Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs Do Until rs.EOF Response.Write rs.Fields("Surname").Value Response.Write "<br>" rs.MoveNext Loop rs.Close Set rs = Nothing %> ID 1 2 3 4 5 Surname Dixon Smith Jones Bloggs Anderson Forenames Mark John Sally Fred Genny Phone 01752 232556 01752 111111 01752 888888 01752 123123 01752 987987 email [email protected] [email protected] [email protected] [email protected] [email protected] Dixon Smith Jones Bloggs Anderson </body> </html> Mark Dixon, SoCCE SOFT 131 Page 10 Example 2: Person (html) <html> <head> <title>Person's Details</title> </head> <body> <p><center><b><font size=+2>Person's Details</font></b></center> <% ' ASP code will go here (next slide). %> Person.asp <form name="frmPerson" action="Person.asp" method=post> <input name="btnPrev" type="submit" value="Previous"> <input name="btnNext" type="submit" value="Next"> </form> </body> </html> Mark Dixon, SoCCE SOFT 131 Page 11 Example 2: Person (ASP) <% Const adOpenDynamic = 3 Const cs = "…" Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs, adOpenDynamic If Session("curID") <> "" Then rs.Find "[ID] = " & Session("curID") If Request.Form("btnPrev") <> "" Then rs.MovePrevious ElseIf Request.Form("btnNext") <> "" Then rs.MoveNext End If End If Session("curID") = rs.Fields("ID").Value Response.Write rs.Fields("Surname").Value & "<br>" Response.Write rs.Fields("Forenames").Value rs.Close Set rs = Nothing %> Mark Dixon, SoCCE SOFT 131 Page 12 SQL • Structured Query Language • 4th Generation Language – code describes what (not how) – (VB 3rd Generation) • SELECT statement – used to get data – can be embedded in VB, via rs.Open: all fields rs.Open "Person", cs rs.Open "SELECT * FROM [Person]", cs Mark Dixon, SoCCE SOFT 131 Page 13 SQL • WHERE clause – used to restrict data SELECT * FROM [People] WHERE [age]>=18; • ORDER BY clause – used to change order of data SELECT * FROM [People] ORDER BY [Surname]; Mark Dixon, SoCCE SOFT 131 Page 14
© Copyright 2026 Paperzz