Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP

2 Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using server-side code to write data to databases Objectives, by end of this week’s sessions, you should be able to: –create an ASP web page that allows the user to store data in database

3 Mark Dixon, SoCCE SOFT 131Page 3 Database Permissions 1 In order for ASP to write to a database –Need to give write access to Internet Guest Account for database file (People.mdb) Right-click on file in Windows Explorer (the following screens are for Windows 2000)

4 Mark Dixon, SoCCE SOFT 131Page 4 Database Permissions 2 Click Security tab Click Add button

5 Mark Dixon, SoCCE SOFT 131Page 5 Database Permissions 3 Select Internet Guest Account IUSR_ … Click Add button Click OK button

6 Mark Dixon, SoCCE SOFT 131Page 6 Database Permissions 4 Select Internet Guest Account Ensure write access is on

7 Mark Dixon, SoCCE SOFT 131Page 7 Writing data to a database create recordset open recordset –dynamic cursor, pessimistic locking to add a record –use to AddNew method rs.AddNew to delete a record –use the Delete method rs.Delete to change existing data –assign a new value to fields rs.Fields("Surname").Value = "Fred"

8 Mark Dixon, SoCCE SOFT 131Page 8 Example 1: Person Edit (html) Person's Details Person's Details <% ' ASP code will go here (next slide). %> Surname: " > PersonEdit.asp

9 Mark Dixon, SoCCE SOFT 131Page 9 Example 1: Person Edit (ASP) <% Const cs = "…" Dim rs Dim Surname Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs, 3, 3 If Session("curID") <> "" Then rs.Find "[ID] = " & Session("curID") If Request.Form("btnPrev") <> "" Then rs.MovePrevious ElseIf Request.Form("btnNext") <> "" Then rs.MoveNext ElseIf Request.Form("btnSave") <> "" Then rs. Fields("Surname") = Request.Form("txtSurname") rs. Update End If Session("curID") = rs.Fields("ID").Value Surname = rs.Fields("Surname").Value rs.Close Set rs = Nothing %>

10 Mark Dixon, SoCCE SOFT 131Page 10 People Database (with Hobbies) IDSurnameForenamesPhoneemail 1DixonMark01752 232556mark.dixon@plymouth.ac.uk 2SmithJohn01752 111111john.smith@john.smith.ac.uk 3JonesSally01752 888888sally.jones@sally.jones.com 4BloggsFred01752 123123fred.bloggs@aaaaaa.com 5AndersonGenny01752 987987genny@bbbb.cccc.com HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person

11 Mark Dixon, SoCCE SOFT 131Page 11 SQL & MS access queries MS Access –Queries: select data from database –really SQL select statements –can use queries to test SQL code MS Access: People.mdb

12 Mark Dixon, SoCCE SOFT 131Page 12 SQL: Joining tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Two tables Matching records IDSurnameForenamesPhoneemailHobbyIDDescriptionPersonID 1DixonMark01752 232556mark.dixon@plymouth.ac.uk1Archery1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk2Herpetology1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk3Music1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk6Hitting people with swords1 2SmithJohn01752 111111john.smith@john.smith.ac.uk4Football2 2SmithJohn01752 111111john.smith@john.smith.ac.uk5Rugby2

13 Mark Dixon, SoCCE SOFT 131Page 13 SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; IDSurname 1Dixon 2Smith

14 Mark Dixon, SoCCE SOFT 131Page 14 Query Strings Data can be added to end of URL: http://localhost/page.asp?Surname=Bob ASP code can use this data: –Request.QueryString("Surname") would return the value "Bob" Form method=get –data automatically added to query string Query String


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP."

Similar presentations


Ads by Google