140 likes | 256 Views
21 – Web applications: Databases & ASP. 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:
E N D
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
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>
Example 1: People (ASP) rs <html> <head><title>Personal Address Book</title></head> <body> <p><center><b><font size=+2> Personal Address Book</font></b></center> <% 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 %> </body> </html>
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> <% 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 %> </body> </html> rs Dixon
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> <% 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 %> </body> </html> rs Dixon Smith
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> <% 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 %> </body> </html> rs Dixon Smith Jones
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> <% 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 %> </body> </html> rs Dixon Smith Jones Bloggs
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> <% 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 %> </body> </html> People.asp rs Dixon Smith Jones Bloggs Anderson
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). %> <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> Person.asp
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 %>
SQL all fields • 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:rs.Open "Person", csrs.Open "SELECT * FROM [Person]", cs
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];