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

Slides:



Advertisements
Similar presentations
Introduction to SQL, OleDB interface to Access from VB.NET.
Advertisements

Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 03 – Database Design: Multiple Tables.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 SOFT – Module Introduction.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Some Basic Database Terminology
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Mark Dixon Page 1 5 – Persistent data storage: relational databases.
Mark Dixon Page 1 19 – Passing Data between pages: Forms, Sessions, & Query Strings.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Lecture 7 Interaction. Topics Implementing data flows An internet solution Transactions in MySQL 4-tier systems – business rule/presentation separation.
Concepts of Database Management Seventh Edition
1 Introducing Database Access from Web Applications by Sally Kyvernitis.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon Page 1 3 – Web applications: Server-side code (JSP)
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
PHP meets MySQL.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Mark Dixon Page 1 18 – Web applications: Server-side code (PhP)
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL.
Mark Dixon 1 05 – JSP Databases: Multiple Tables.
Mark Dixon 1 06 – JSP Databases: Multiple Tables.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Concepts of Database Management Seventh Edition
ASP.NET The Clock Project. The ASP.NET Clock Project The ASP.NET Clock Project is the topic of Chapter 23. By completing the clock project, you will learn.
Mark Dixon Page 1 Web-Application Development Workshop.
Database Management Systems.  Database management system (DBMS)  Store large collections of data  Organize the data  Becomes a data storage system.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Mark Dixon 1 21 – Databases: Multiple Tables. Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data.
When I want to work with SQL, I start off as if I am doing a regular query.
Mark Dixon Page 1 Tech – HTML. Mark Dixon Page 2 Admin Attendance Register: –log in to your profile.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Web applications: Databases & ASP.
Mark Dixon Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
Mark Dixon 1 07 – In-class test. Mark Dixon 2 In-class Test Date: Monday 5 th November 2012 Time: 11:05-120:55 Location: SMB109 Type: Individual 40% of.
Mark Dixon, SoCCE SOFT 131Page 1 14 – Databases: Multiple Tables.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
The purpose of a CPU is to process data Custom written software is created for a user to meet exact purpose Off the shelf software is developed by a software.
DBMS and SQL.
18 – Databases: Structured Query Language
17 – Modular Design in ASP.
17 – Persistent data storage: relational databases and ADO
19 – Databases: Multiple Tables
Presentation transcript:

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

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

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 

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

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

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)

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

Mark Dixon 8 Record Field Example: People (Database) IDSurnameForenamesPhoneGender 1DixonMark Male 2SmithJohn Male 3JonesSally Female Information organised into –tables (e.g. person) –fields (e.g. phone) –records (e.g. 1 Dixon Mark …) Person

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

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

Mark Dixon 11 MS Access

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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") + " "

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

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 + " "   

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:

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

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)

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

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];

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'

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

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';

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];

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

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];

Mark Dixon 50 Access Driver (for 32bit Office) gb/download/details.aspx?id=13255

Mark Dixon 51 Access Driver (for 64bit Office) gb/download/details.aspx?id=23734

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 address (You may want to use a Query String)