520 likes | 601 Views
20 – Persistent data storage: relational databases and ADO. 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. Session("score") = 74.
E N D
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. Session("score") = 74 If Session("i") > 25 Then g = g + 1 End If
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
Admin: SQL Book Gennick J (2006) SQL Pocket Guide (2nd edition). O'Reilly.ISBN: 0-596-52688-1
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
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)
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-functionalshould be viewable anywhere in the world
Example: People (Database) • Information organised into • tables (e.g. person) • fields (e.g. phone) • records (e.g. 1 Dixon Mark 01752 586225 …) Field Record Person
Questions: Music (Database) 3 • How many fields? • How many records? 9 Track
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
ActiveX Data Objects • ActiveX Data Objects (ADO) • common database interface • allow you to write code for any DBMS MS Access VB.Netcode MS SQL Server ADO … DB front end …
Using Record Sets Connection string – identify database Open connection Read next record Read field data Close connection 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") & "<br />" Loop cn.Close
Example: People (code) People.aspx <%@ Page Language="VB" %> <%@ Import Namespace="System.Data.OleDb" %> <script runat="server"> 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub </script> Must include database library
Example: People (r) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub
Example: People (s) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s
Example: People (ExecuteReader) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s
Example: People (s) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s
Example: People (Read) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s
Example: People (Surname) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />
Example: People (Loop) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />
Example: People (Read) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />
Example: People (Surname) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br /> Dixon<br />Smith<br />
Example: People (Loop) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br /> Dixon<br />Smith<br />
Example: People (Read) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br /> Dixon<br />Smith<br />
Example: People (Surname) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />Smith<br />Jones <br /> Dixon<br /> Dixon<br />Smith<br />
Example: People (Loop) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />Smith<br />Jones <br /> Dixon<br /> Dixon<br />Smith<br />
Example: People (Read) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />Smith<br />Jones <br /> Dixon<br /> Dixon<br />Smith<br />
Example: People (Close) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />Smith<br />Jones <br /> Dixon<br /> Dixon<br />Smith<br />
Example: People (Display) People.aspx 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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub s Dixon<br />Smith<br />Jones <br /> Dixon<br /> Dixon<br />Smith<br />
Example: Countries Countries.aspx 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 html by hand put br tags between data <br /> Need data on separate lines
Example: Countries (error) Countries.aspx 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 = <br /> Do While r.Read() s = s & r(“Name") Loop cn.Close parData.InnerHtml = s End Sub Try putting br tag here VB does not understand html Need data on separate lines
Example: Countries Countries.aspx • Need double quotes around tag • (VB sees html as literal string) 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 = "<br />" Do While r.Read() s = s & r(“Name") Loop cn.Close parData.InnerHtml = s End Sub runs, but br in wrong place
Example: Countries Countries.aspx 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 = "<br />" 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?
Example: Countries Countries.aspx 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 = "<br />" Do While r.Read() s = s & r(“Name") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub Move br tag inside loop. after field data View Source
Embedding html in VB • html must be string (inside double quotes) • follows normal pattern for expressions: data data data data s = s+"<b>"+r("Name")+"</b>" operator operator operator
Embedding html in VB (errors) s = s + "<i>" r("Gender") + "</i>" missing operator s = s + rs("Height") + </i>" missing double quote s = s + <ul> + r("Height") html tag must be inside double quotes s = s + "<ul>" + r("<b>Height") looks for field in database called <b>Height
Questions: HTML in VB • Are these correct (assume variables and fields exist)? g = g + rs("Surname<br />") h = h + "<ol>" r("Width") a = "<p>" + a + "</p>" html = html + "<img src=‘face.gif’ />" h = <table> + h + "</table>"
Example: People v2 • 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 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") & "<br />" End If Loop cn.Close parData.InnerHtml = s End Sub
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") & "<br />" Loop cn.Close parData.InnerHtml = s End Sub SQL statement
SQL: Queries • main purpose of databases: • get information back out: searching • Structured Query Language • dedicated to interacting with databases • 3rd Generation Language (such as VB, C#) • code describes how to do task • 4th Generation Language (such as SQL) • code describes what to do (not how to do it)
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
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];
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 textSELECT * FROM Person WHERE Surname = 'Smith'
SQL & MS access queries • MS Access • Queries: select data from database • really SQL select statements • can use queries to test SQL code
Questions: SQL Track • Create an SQL statement to extract Track Title of records by Aerosmith SELECT [Track Title] FROM Track WHERE [Artist Name] = 'Aerosmith';
Questions: SQL Track • Create an SQL statement to extract all fields of songs by Disturbed, ordered by track name SELECT * FROM Track WHERE [Artist Name] = 'Disturbed' ORDER BY [Track Title];
Example: People v4 • User controls what is displayed:
SQL: DISTINCT records SELECT [Artist Name]FROM [Track]; SELECT DISTINCT [Artist Name]FROM [Track];
Access Driver (for 32bit Office) • http://www.microsoft.com/en-gb/download/details.aspx?id=13255