Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Mark Dixon, SoCCE SOFT 131Page 1 16 – 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

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 1: 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 1: 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 Database Management Systems DBMS provides facilities for: –creating and changing databases add/remove records add/remove fields add/remove data e.g. –home/small business Microsoft Access dBase –Large scale Microsoft SQL Server Oracle

10 Mark Dixon, SoCCE SOFT 131Page 10 MS Access

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

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

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

14 Mark Dixon, SoCCE SOFT 131Page 14 Example 1: 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

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

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

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

18 Mark Dixon, SoCCE SOFT 131Page 18 Example 2: 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

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

20 Mark Dixon, SoCCE SOFT 131Page 20 Example 3: 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 16 – Persistent data storage: relational databases and ADO."

Similar presentations


Ads by Google