Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.

Similar presentations


Presentation on theme: "Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO."— Presentation transcript:

1 Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO

2 Mark Dixon Page 2 Question: Self-Contained Are the following routines self contained? Dim g Dim w Sub Square(ByRef res As Double, ByVal n1 As Double) res = n1 * n1 End Sub Function u(num As Double) As Double Return num * (w + g) End Function 

3 Mark Dixon Page 3 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using relational databases for persistent data storage 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 using ActiveX Data Objects (ADO) –use SQL in your programs to create more complex record-sets

4 Mark Dixon Page 4 Persistent Data Storage So far –programs (web-pages) 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)

5 Mark Dixon Page 5 Example: People (analysis) SPECIFICATION User Requirements –need to have access to people's details Software Requirements –Functional: –Display list of people from a database –Non-functional should be viewable anywhere in the world

6 Mark Dixon Page 6 Record Field Example: People (Database) IDSurnameForenamesPhoneGender 1DixonMark01752 232556Male 2SmithJohn01752 111111Male 3JonesSally01752 888888Female Information organised into –tables (e.g. person) –fields (e.g. phone) –records (e.g. 1 Dixon Mark 01752 232556 …) Person

7 Mark Dixon Page 7 How many fields? How many records? Questions: Music (Database) 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 9 3

8 Mark Dixon Page 8 DBMS Database Management Systems (DBMS) provide 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

9 Mark Dixon Page 9 MS Access Music database

10 Mark Dixon Page 10 ActiveX Data Objects 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

11 Mark Dixon Page 11 Using Record Sets Const cs = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\People.mdb;" Dim rs As Object rs = CreateObject ("ADODB.Recordset") rs. Open ("Person", cs) Do Until rs.EOF () … rs. MoveNext() Loop rs. Close() rs = Nothing Connection string – identify database Open record set with table Move to next record Close record set

12 Mark Dixon Page 12 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

13 Mark Dixon Page 13 Connection Strings: UDL files Generate connection strings –Right click on desktop –Select New, Text Document –Rename to *.UDL (Yes to warning message) –Double click –Select provider (MS Jet 4.0 OLE DB Provider) –Click Next –Select or enter DB name –Click Test Connection button –Click OK –Open with Notepad, cut & paste text

14 Mark Dixon Page 14 People.aspx Example: People (rs) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub

15 Mark Dixon Page 15 People.aspx Example: People (s) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s

16 Mark Dixon Page 16 People.aspx Example: People (recordset) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s

17 Mark Dixon Page 17 People.aspx Example: People (Open) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s

18 Mark Dixon Page 18 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s

19 Mark Dixon Page 19 People.aspx Example: People (EOF) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s

20 Mark Dixon Page 20 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon

21 Mark Dixon Page 21 People.aspx Example: People (MoveNext) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon

22 Mark Dixon Page 22 People.aspx Example: People (Loop) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon

23 Mark Dixon Page 23 People.aspx Example: People (EOF) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon

24 Mark Dixon Page 24 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith Dixon

25 Mark Dixon Page 25 People.aspx Example: People (MoveNext) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith

26 Mark Dixon Page 26 People.aspx Example: People (Loop) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith

27 Mark Dixon Page 27 People.aspx Example: People (EOF) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith

28 Mark Dixon Page 28 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith Jones Dixon Smith

29 Mark Dixon Page 29 People.aspx Example: People (MoveNext) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith Jones

30 Mark Dixon Page 30 People.aspx Example: People (Loop) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith Jones

31 Mark Dixon Page 31 People.aspx Example: People (EOF) rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub s Dixon Smith Jones

32 Mark Dixon Page 32 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub Dixon Smith Jones s

33 Mark Dixon Page 33 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub Dixon Smith Jones s

34 Mark Dixon Page 34 People.aspx Example: People rs Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub Dixon Smith Jones s

35 Mark Dixon Page 35 Example: People v2 Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs) s = "" Do Until rs.EOF() If rs.Fields("Gender").value = "Male" Then s = s & rs.Fields("Surname").Value & " " End If rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub Display Surname of Male people:

36 Mark Dixon Page 36 Example: People v3 Display Surname of Male people: Sub Page_Load() Const cs = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb; " Dim rs As Object Dim s As String Dim sql As String rs = CreateObject("ADODB.Recordset") sql = " SELECT * FROM [Person] WHERE [Gender] = 'Male'; " rs.Open(sql, cs) s = "" Do Until rs.EOF() s = s & rs.Fields("Surname").Value & " " rs.MoveNext() Loop parData.InnerHtml = s rs.Close() rs = Nothing End Sub SQL statement

37 Mark Dixon Page 37 SQL: Queries main purpose of databases: –get information back out: searching Structured Query Language –dedicated to interacting with databases 3 rd Generation Language (such as VB, C++) –code describes how to do task 4 th Generation Language (such as SQL) –code describes what to do (not how to do it)

38 Mark Dixon Page 38 SQL: SELECT statement SELECT statement –used to get data –can be embedded in VB, via rs.Open: rs.Open("Person", cs) rs.Open("SELECT * FROM [Person]", cs) all fields

39 Mark Dixon Page 39 SQL: WHERE & ORDER BY 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];

40 Mark Dixon Page 40 SQL: strings (text data) Possible confusion: SELECT * FROM Person WHERE Surname = Smith this will look for field called Smith - gives error need single (SQL) quotes to signify literal text SELECT * FROM Person WHERE Surname = 'Smith'

41 Mark Dixon Page 41 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

42 Mark Dixon Page 42 Questions: SQL Create an SQL statement to extract Track Title of records by Aerosmith 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 SELECT [Track Title] FROM Track WHERE [Artist Name] = 'Aerosmith'; MS Access: Music.mdb

43 Mark Dixon Page 43 Questions: SQL Create an SQL statement to extract all fields of songs by Disturbed, ordered by track name 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 SELECT * FROM Track WHERE [Artist Name] = 'Disturbed' ORDER BY [Track Title]; MS Access: Music.mdb

44 Mark Dixon Page 44 Example: People v4 User controls what is displayed:

45 Mark Dixon Page 45 SQL: DISTINCT records SELECT [Artist Name] FROM [Track]; Artist Name Black Sabbath Aerosmith Alien Ant Farm Disturbed Artist Name Black Sabbath Aerosmith Alien Ant Farm Disturbed SELECT DISTINCT [Artist Name] FROM [Track];

46 Mark Dixon Page 46 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: Get the People v1 example (from the lecture) working. 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 it displays the data in an html table. Task 5: Modify your page so that records for males are displayed in blue, and records for females are displayed in red. Task 6: Modify your page as per version 4. You will need to: –Add a form to the page, and three submit buttons –In your code, detect when a button has been pressed (have a look at previous weeks) Task 7: Modify your page so that the user can type a letter, and only names starting with that letter are displayed. Task 8: Modify your page so that the user can type a series of numerical digits and only phone numbers containing those digits are displayed. Task 9: Modify your code so that the user can order the data by surname, or email address (You may want to use a Query String)


Download ppt "Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO."

Similar presentations


Ads by Google