Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Mark Dixon 2 Questions: Session variables Write a line of VB code to put 74 into a session variable called score. Write VB code that adds 1 to a variable called g, when a session variable called i is over 25. If Session("i") > 25 Then g = g + 1 End If Session("score") = 74

3 Mark Dixon 3 Question: Self-Contained Are the following routines self contained? Dim g As Double Dim w As Double 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 

4 Mark Dixon 4 Admin: SQL Book Gennick J (2006) SQL Pocket Guide (2 nd edition). O'Reilly. ISBN: 0-596-52688-1

5 Mark Dixon 5 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.Net) 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

6 Mark Dixon 6 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)

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

8 Mark Dixon 8 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 586225 …) Person

9 Mark Dixon 9 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

10 Mark Dixon 10 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

11 Mark Dixon 11 MS Access

12 Mark Dixon 12 ActiveX Data Objects ActiveX Data Objects (ADO) –common database interface allow you to write code for any DBMS VB.Net code ADO MS Access MS SQL Server … … DB front end

13 Mark Dixon 13 Using Record Sets Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Server.MapPath (" People.accdb ") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open () r = cmd. ExecuteReader () s = "" Do While r.Read () s = s & r("Surname") & " " Loop cn.Close Connection string – identify database Open connection Read next record Close connection Read field data

14 Mark Dixon 14 People.aspx Example: People (code) Sub Page_Load() Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection (cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand ("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub Must include database library

15 Mark Dixon 15 People.aspx Example: People (r) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub

16 Mark Dixon 16 People.aspx Example: People (s) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s

17 Mark Dixon 17 People.aspx Example: People (ExecuteReader) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s

18 Mark Dixon 18 People.aspx Example: People (s) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s

19 Mark Dixon 19 People.aspx Example: People (Read) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s

20 Mark Dixon 20 People.aspx Example: People (Surname) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon

21 Mark Dixon 21 People.aspx Example: People (Loop) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon

22 Mark Dixon 22 People.aspx Example: People (Read) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon

23 Mark Dixon 23 People.aspx Example: People (Surname) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith

24 Mark Dixon 24 People.aspx Example: People (Loop) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith

25 Mark Dixon 25 People.aspx Example: People (Read) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith

26 Mark Dixon 26 People.aspx Example: People (Surname) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith Dixon Smith Jones

27 Mark Dixon 27 People.aspx Example: People (Loop) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith Dixon Smith Jones

28 Mark Dixon 28 People.aspx Example: People (Read) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith Dixon Smith Jones

29 Mark Dixon 29 People.aspx Example: People (Close) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith Dixon Smith Jones

30 Mark Dixon 30 People.aspx Example: People (Display) r Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub s Dixon Dixon Smith Dixon Smith Jones

31 Mark Dixon 31 Countries.aspx Example: Countries Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath(“ Countries.accdb ") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Country ;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r(“ Name ") Loop cn.Close parData.InnerHtml = s End Sub Need data on separate lines html by hand put br tags between data

32 Mark Dixon 32 Countries.aspx Example: Countries (error) Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath(“Countries.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Country;", cn) cn.Open() r = cmd.ExecuteReader() s = Do While r.Read() s = s & r(“Name") Loop cn.Close parData.InnerHtml = s End Sub Need data on separate lines Try putting br tag here VB does not understand html

33 Mark Dixon 33 Countries.aspx Example: Countries Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath(“Countries.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Country;", cn) cn.Open() r = cmd.ExecuteReader() s = " " Do While r.Read() s = s & r(“Name") Loop cn.Close parData.InnerHtml = s End Sub Need double quotes around tag (VB sees html as literal string) runs, but br in wrong place

34 Mark Dixon 34 Countries.aspx Example: Countries Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath(“Countries.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Country;", cn) cn.Open() r = cmd.ExecuteReader() s = " " Do While r.Read() s = s & r(“Name") Loop cn.Close parData.InnerHtml = s End Sub Move br tag inside loop. Which bit of code pulls data from database?

35 Mark Dixon 35 Countries.aspx Example: Countries Sub Page_Load() Dim cs As String = " Provider=Microsoft.ACE.OLEDB.12.0; " + _ "Data Source=" + Server.MapPath(“Countries.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Country;", cn) cn.Open() r = cmd.ExecuteReader() s = " " Do While r.Read() s = s & r(“Name") & " " Loop cn.Close parData.InnerHtml = s End Sub Move br tag inside loop. after field data View Source

36 Mark Dixon 36 Embedding html in VB html must be string (inside double quotes) follows normal pattern for expressions: data operator data operator s = s + " " + r("Name") + " "

37 Mark Dixon 37 Embedding html in VB (errors) s = s + " " r("Gender") + " " missing operator s = s + rs("Height") + " missing double quote s = s + + r("Height") html tag must be inside double quotes s = s + " " + r(" Height") looks for field in database called Height

38 Mark Dixon 38 Questions: HTML in VB Are these correct (assume variables and fields exist)? g = g + rs("Surname ") h = h + " " r("Width") a = " " + a + " " html = html + " " h = + h + " "   

39 Mark Dixon 39 Example: People v2 Sub Page_Load() Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() If r("Gender") = True Then s = s & r("Surname") & " " End If Loop cn.Close parData.InnerHtml = s End Sub Display Surname of Male people:

40 Mark Dixon 40 Example: People v3 Display Surname of Male people: Sub Page_Load() Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim sql As String = " SELECT * FROM Person WHERE Gender = True; " Dim s As String cmd = New OleDbCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub SQL statement

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

42 Mark Dixon 42 SQL: SELECT statement SELECT statement –used to get data –can be embedded in VB, via: – … v = " SELECT * FROM [Person]"... cmd = New OleDbCommand(v, cn) all fields

43 Mark Dixon 43 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];

44 Mark Dixon 44 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'

45 Mark Dixon 45 SQL & MS access queries MS Access –Queries: select data from database –really SQL select statements –can use queries to test SQL code

46 Mark Dixon 46 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';

47 Mark Dixon 47 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];

48 Mark Dixon 48 Example: People v4 User controls what is displayed:

49 Mark Dixon 49 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];

50 Mark Dixon 50 Access Driver (for 32bit Office) http://www.microsoft.com/en- gb/download/details.aspx?id=13255

51 Mark Dixon 51 Access Driver (for 64bit Office) http://www.microsoft.com/en- gb/download/details.aspx?id=23734

52 Mark Dixon 52 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 1 20 – Persistent data storage: relational databases and ADO."

Similar presentations


Ads by Google