Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 17 – Persistent data storage: relational databases and ADO."— Presentation transcript:

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

2 Mark Dixon, SoCCE SOFT 131Page 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 –use a relational database to store an application's data between executions –create an entity-relationship diagram from a database

3 Mark Dixon, SoCCE SOFT 131Page 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)

4 Mark Dixon, SoCCE SOFT 131Page 4 Record Field Flat files: Data Duplication Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track

5 Mark Dixon, SoCCE SOFT 131Page 5 Relations (tables) Track TitleArtist ID Paranoid1 Falling in Love2 Pink2 Love in an Elevator2 Smooth Criminal3 Meaning of Life4 The Game4 Voices4 Down with the Sickness4 Artist ID Artist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS Track Artist Primary Key Foreign Key

6 Mark Dixon, SoCCE SOFT 131Page 6 Normalisation Part of database design Process of breaking data down Codd –7 stages of normalisation Mathematical Difficult to apply stages

7 Mark Dixon, SoCCE SOFT 131Page 7 Exercise: Prescriptions Identify duplication and separate: DateSurnameForenamesDrug Name 6 Jan 04JonesAlisonCo-codamol 11 Jan 04SmithBobTegretol 18 Jan 04HopeJohnCo-codamol 5 Feb 04JohnsonSallyCo-codamol 8 Feb 04SmithBobTegretol 10 Feb 04SmithBobSorbitol Prescription

8 Mark Dixon, SoCCE SOFT 131Page 8 Exercise: Solution DatePatientIDDrugID 6 Jan 0411 11 Jan 0422 18 Jan 0431 5 Feb 0441 8 Feb 0422 10 Feb 0423 Prescription PatientIDSurnameForenames 1JonesAlison 2SmithBob 3HopeJohn 4JohnsonSally Patient DrugIDDrug Name 1Co-codamol 2Tegretol 3Sorbitol Drug

9 Mark Dixon, SoCCE SOFT 131Page 9 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

10 Mark Dixon, SoCCE SOFT 131Page 10 Entity-relationship diagrams Each table in db –stores details of entity shown as rectangular box Relationships between tables –represent relationships between entities shown as line between entities (boxes) PersonHobby

11 Mark Dixon, SoCCE SOFT 131Page 11 Relationship Types One-to-one One-to-many Many-to-one Many-to-many –(can't be implemented in relational database) ABABABAB

12 Mark Dixon, SoCCE SOFT 131Page 12 Exercise: Which relationship type? 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 Hobby

13 Mark Dixon, SoCCE SOFT 131Page 13 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

14 Mark Dixon, SoCCE SOFT 131Page 14 MS Access Music database

15 Mark Dixon, SoCCE SOFT 131Page 15 ActiveX Data Objects (what & why) ActiveX Data Objects (ADO) –common database interface allow you to write code for any DBMS VB or VB Script code ADO MS Access MS SQL Server … … DB front end

16 Mark Dixon, SoCCE SOFT 131Page 16 Enabling ADO Project menu –References item Microsoft ActiveX Data Objects Library (latest 2.7)

17 Mark Dixon, SoCCE SOFT 131Page 17 ADO RecordSet Object Used to interact with tables Properties –BOF: true if at start of recordset (before first record) –EOF: true if at end of recordset (after last record) –Fields: used to get and set data values Methods –Open: used to open recordset –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 recordset

18 Mark Dixon, SoCCE SOFT 131Page 18 Example: Music Private Sub btnLoad_Click() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs. Open "Track", cs rs. MoveFirst Do Until rs. EOF lstTracks.AddItem rs.Fields("TrackTitle").Value rs. MoveNext Loop rs. Close Set rs = Nothing End Sub btnLoadlstTracks

19 Mark Dixon, SoCCE SOFT 131Page 19 Connection Strings Connection string – identify data source Const cs = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=D:\Music.mdb;" & _ "Persist Security Info=False" Private Sub btnLoad_Click() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "Track", cs … End Sub

20 Mark Dixon, SoCCE SOFT 131Page 20 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

21 Mark Dixon, SoCCE SOFT 131Page 21 Searching for Data Recordset methods –Find: searches for the next record to match given criteria string: e.g. "Name = 'Smith' " ( " are for VB string) ( ' are for database string)

22 Mark Dixon, SoCCE SOFT 131Page 22 Example: Music v2 Private Sub lstTracks_Click() Dim rs As ADODB.Recordset Dim strCriteria As String Set rs = New ADODB.Recordset rs.Open "Track", cs, adOpenDynamic strCriteria = "TrackTitle = '" & _ lstTracks.List(lstTracks.ListIndex) & "'" rs. Find strCriteria txtTrackTitle.Text = rs.Fields("TrackTitle").Value rs.Close Set rs = Nothing End Sub

23 Mark Dixon, SoCCE SOFT 131Page 23 Changing Data Recordset methods –AddNew: inserts a new record and makes it current –Update: sends changes back to DB –Delete: deletes currently selected record

24 Mark Dixon, SoCCE SOFT 131Page 24 Example: Music v3 Private Sub txtTrackTitle_Change() Dim rs As ADODB.Recordset Dim strCriteria As String Set rs = New ADODB.Recordset rs.Open "Track", cs, adOpenDynamic, adLockPessimistic strCriteria = "TrackTitle = '" & _ lstTracks.List(lstTracks.ListIndex) & "'" rs.Find strCriteria rs.Fields("TrackTitle").Value = txtTrackTitle.Text rs.Update rs.Close Set rs = Nothing End Sub


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 17 – Persistent data storage: relational databases and ADO."

Similar presentations


Ads by Google