11 – persistent data storage: relational databases and ado

21
Mark Dixon, SoCCE SOFT 131 Page 1 11 – Persistent data storage: relational databases and ADO

Upload: kenley

Post on 16-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

11 – Persistent data storage: relational databases and ADO. Session Aims & Objectives. Aims To introduce the fundamental ideas involved in persistent data storage and relational databases Objectives, by end of this week’s sessions, you should be able to: create a relational database table - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 1

11 – Persistent data storage: relational databases and ADO

Page 2: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 2

Session Aims & Objectives• Aims

– To introduce the fundamental ideas involved in persistent data storage and relational databases

• Objectives,by end of this week’s sessions, you should be able to:

– create a relational database table– create a web page (ASP) that displays data

from a single table in a database

Page 3: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 3

Persistent Data Storage• So far

– all programs lose data when closed

• Not realistic– typically data stored to persistent storage device

(e.g. hard disk, key drive, floppy disk, CD-RW)

• Use either– flat files– database (relational, or object oriented)

Page 4: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 4

Example: People (Specification)• User requirement:

– Display list of people from database online

• How:– Combine our knowledge of:

• ASP (active server pages)• ADO (activeX data objects)

Page 5: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 5

Record Field

Example: People (Database)

ID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected]

2 Smith John 01752 111111 [email protected]

3 Jones Sally 01752 888888 [email protected]

4 Bloggs Fred 01752 123123 [email protected]

5 Johnson Genny 01752 987987 [email protected]

• Information organised into– tables (e.g. person)– fields (e.g. phone)– records (e.g. 1 Dixon Mark 01752 232556 …)

Person

Page 6: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 6

• How many fields?

• How many records?

Example: Music (Database)

Track Title Artist Name Country

Paranoid Black Sabbath UK

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Page 7: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 7

Database Management Systems• DBMS provides facilities for:

– creating and changing databases• add/remove records• add/remove fields• add/remove data

– For example:• Microsoft Access• dBase• Borland Paradox• MySQL• Microsoft SQL Server• Oracle

home/small business

large scale

Page 8: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 8

MS Access

Music database

Page 9: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 9

ActiveX Data Objects (what & why)

• ActiveX Data Objects (ADO)– common database interface

• allow you to write code for any DBMS

VB orVB Script

codeADO

MS Access

MS SQL Server

…DB front end

Page 10: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 10

ADO Record Set Object• Used to interact with tables• Properties

– BOF: true if at start of record set (before first record)– EOF: true if at end of record set (after last record)– Fields: used to get and set data values

• Methods– Open: used to open record set– MoveFirst: moves focus to first record– MovePrevious: moves focus to previous record– MoveNext: moves focus to next record– MoveLast: moves focus to last record– Close: closes record set

Page 11: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 11

Using Record Sets

<%Const cs = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Music.mdb; Persist Security Info=False"Dim rs Set rs = CreateObject("ADODB.Recordset") rs.Open "Person", cs

Do Until rs.EOF … rs.MoveNext Loop

rs.Close Set rs = Nothing%>

Connect string – identify database

Open record set with table

Move to next record

Close record set

Page 12: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 12

UDL files• Generate connection strings

– Right click on desktop– Select New, Text Document– Rename to *.UDL (Yes to warning message)

– Double click– Select provider– Click Next– Select or enter DB name– Click Test Connection button– Click OK– Open with Notepad, cut & paste text

Page 13: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 13

People.asp Example: People<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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

Page 14: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 14

Example: 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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

Dixon

Page 15: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 15

Example: 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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

DixonSmith

Page 16: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 16

Example: 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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

DixonSmithJones

Page 17: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 17

Example: 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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

DixonSmithJonesBloggs

Page 18: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 18

Example: 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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

DixonSmithJonesBloggsAnderson

Page 19: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 19

Example: People (recordset 6)<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>

rsID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected] Smith John 01752 111111 [email protected] Jones Sally 01752 888888 [email protected] Bloggs Fred 01752 123123 [email protected] Anderson Genny 01752 987987 [email protected]

DixonSmithJonesBloggs

People.asp

Anderson

Page 20: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 20

Tutorial Exercise: People• Task 1: Create your own People database:

– Open MS Access– Create a new database file– Create a new table– Create fields– Enter data

• Task 2: Create the asp page (as per the lecture) to display data from the database.

• Task 3: Modify your page so that it displays phone number as well as the person's name.

• Task 4: Modify your page so that the user can type a letter, and only names starting with that letter are displayed.

Page 21: 11 – Persistent data storage: relational databases and ADO

Mark Dixon, SoCCE SOFT 131 Page 21

Tutorial Exercise: Music• Task 1: Create your own Music Database.

• Task 2: Create an asp page to display data from this database.

• Task 3: Modify your page so that the user can type the name of an artist, and only tracks by that artist are displayed

• Task 4: Make your page case in-sensitive (i.e. UPPER or lower case makes no difference)