chapter 25 – database: sql, ado and rds

53
2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1 Introduction 25.2 Relational Database Model 25.3 Relational Database Overview: Books.mdb 25.4 Structured Query Language 25.4.1 Basic SELECT Query 25.4.2 WHERE Clause 25.4.3 ORDER BY Clause 25.4.4 Using INNER JOIN to Merge Data from Multiple Tables 25.4.5 TitleAuthor Query from Books.mdb 25.4.6 Inserting a Record 25.4.7 Updating a Record 25.5 Registering Books.mdb as an ODBC Source 25.6 ActiveX Data Objects (ADO) 25.7 Remote Data Services (RDS

Upload: james-conner

Post on 03-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Chapter 25 – Database: SQL, ADO and RDS. Outline 25.1Introduction 25.2Relational Database Model 25.3Relational Database Overview: Books.mdb 25.4Structured Query Language 25.4.1Basic SELECT Query 25.4.2WHERE Clause 25.4.3ORDER BY Clause - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Chapter 25 – Database: SQL, ADO and RDS

Outline25.1 Introduction25.2 Relational Database Model25.3 Relational Database Overview: Books.mdb25.4 Structured Query Language

25.4.1 Basic SELECT Query25.4.2 WHERE Clause25.4.3 ORDER BY Clause25.4.4 Using INNER JOIN to Merge Data from Multiple Tables25.4.5 TitleAuthor Query from Books.mdb25.4.6 Inserting a Record25.4.7 Updating a Record

25.5 Registering Books.mdb as an ODBC Source25.6 ActiveX Data Objects (ADO)25.7 Remote Data Services (RDS

Page 2: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.1 Introduction

• Database– Integrated collection of data

• Database Management System (DBMS)– Has mechanisms for storing and organizing data

– Sophisticated queries and manipulations of data

• Relational Databases– Most popular database system in use today

• Structured Query Language (SQL – pronounced “sequel”)

– Almost universally used with relational databases

– Makes queries (request information) and manipulate data

Page 3: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.1 Introduction (II)

• In this chapter– Presentation of basic SQL queries using a database– Introduction of two Microsoft technologies

• ActiveX Data Objects (ADO)• Remote Data Services (RDS)

• These two technologies– Enable client browser to

• Retrieve information from a database on a Web server• Process that information on the client computer• Return modifications of the data to the Web server so that the data can

be updated in the database

• Manipulation of data in the client– Increases performance of Web-based database applications– Reduces overall load on the server computer

Page 4: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.2 Relational Database Model

• Relational Database Model– Logical representation of the data

– Consider relationships between data without worrying about physical implementation

• Relational Database– Composed of tables

– Any row of the table is called a record

– The first field is used as the primary key for referencing• Records are normally unique (by primary key)

• Primary key can be composed of more than one field or column

– Each column represents a different field (or attribute)

Page 5: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.2 Relational Database Model (II)

Relational Database Structure

Number Name DepartmentSalary Location

2360324568

35761

34589

47132

78321

JONES, A.

KERWIN, R.

LARSON, P.

MYERS, B.

NEUMANN, C.

STEPHENS, T.

413

413

642

611

413

611

1100

2000

1800

1400

9000

8000

NEW JERSEY

NEW JERSEY

LOS ANGELES

ORLANDO

NEW JERSEY

ORLANDO

Table: Employee

A record

A columnPrimary Key

Page 6: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.2 Relational Database Model (III)

• Different users interested in different parts of the table– SQL statements specify the data to select from the table

• SQL– Provides complete set of keywords

– Smaller databases can also be combined to form larger ones

– Results of a query called result sets (or record sets)

Example: Result set to show where departments are located

Department Location

413

642

611

NEW JERSEY

LOS ANGELES

ORLANDO

Page 7: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.3 Relational Database Overview: Books.mdb

• Overview of Database Books.mdb – This database will be used throughout the chapter to introduce

• Various database concepts

• Use of SQL to obtain useful information from the database and to manipulate the database

– Books.mdb consists of four tables:• Authors, Publishers, AuthorISBN and Titles• Primary key for each table is shown in italics

Page 8: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.3 Relational Database Overview: Books.mdb (II)

• Authors table description– AuthorID

• Integer representing author’s ID number in database (primary key)

– FirstName• String representing author’s first name

– LastName• String representing author’s last name

– YearBorn• String representing author’s year of birth

• Authors table data

AuthorID

FirstName

LastName

YearBorn

1 Harvey Deitel 1946

2 Paul Deitel 1968

3 Tem Nieto 1969

Page 9: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.3 Relational Database Overview: Books.mdb (III)

• Publishers table description– PublisherID

• Integer representing publisher’s ID number in database (primary key)

– PublisherName• String representing abbreviated name for publisher

• Publishers table data

PublisherID

PublisherName 1 Prentice Hall 2 Prentice Hall PTR

Page 10: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

• AuthorISBN table description– ISBN

• String representing ISBN number for a book (linking table - no primary key)

– AuthorID • Integer representing author’s ID number

25.3 Relational Database Overview: Books.mdb (IV)

Page 11: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.3 Relational Database Overview: Books.mdb (V)

ISBN AuthorID ISBN AuthorID ISBN AuthorID 0-13-010671-2 1 (continued from bottom of previous row) (continued from bottom of previous row)

0-13-010671-2 2 0-13-271974-6 1 0-13-904947-9 1

0-13-020522-2 1 0-13-271974-6 2 0-13-904947-9 2

0-13-020522-2 2 0-13-456955-5 1 0-13-904947-9 3

0-13-082925-0 2 0-13-456955-5 2 0-13-013249-7 1

0-13-082927-7 1 0-13-456955-5 3 0-13-013249-7 2

0-13-082927-7 2 0-13-528910-6 1 0-13-085609-6 1

0-13-082928-5 1 0-13-528910-6 2 0-13-085609-6 2

0-13-082928-5 2 0-13-565912-4 1 0-13-085609-6 3

0-13-082928-5 3 0-13-226119-7 2 0-13-016143-8 1

0-13-083054-2 1 0-13-020522-2 3 0-13-016143-8 2

0-13-083054-2 2 0-13-082714-2 1 0-13-016143-8 3

0-13-083055-0 1 0-13-082714-2 2 0-13-015870-4 1

0-13-083055-0 2 0-13-082925-0 1 0-13-015870-4 2

0-13-118043-6 1 0-13-565912-4 2 0-13-015870-4 3

0-13-118043-6 2 0-13-565912-4 3 0-13-012507-5 1

0-13-226119-7 1 0-13-899394-7 1 0-13-012507-5 2

0-13-226119-7 2 0-13-899394-7 2 0-13-085248-1 1

(continued on top of next row) (continued on top of next row) 0-13-085248-1 2

AuthorISBN table data

Page 12: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.3 Relational Database Overview: Books.mdb (VI)

• Titles table description– ISBN

• String representing ISBN number of the book (primary key)

– Title• String representing title of the book

– EditionNumber• String representing edition of the book

– YearPublished• String representing year in which book was published

– Description• String representing description of the book

– PublisherID• Integer representing publisher’s ID number (corresponds to ID

number in Publishers table)

Page 13: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

• Titles table data

ISBN

Title

Edition Number

Year- Published

PublisherID

0-13-226119-7 C How to Program 2 1994 1

0-13-528910-6 C++ How to Program 2 1997 1

0-13-899394-7 Java How to Program 2 1997 1

0-13-012507-5 Java How to Program 3 1999 1

0-13-456955-5 Visual Basic 6 How to Program 1 1998 1

0-13-016143-8 Internet and World Wide Web How to Program 1 1999 1

0-13-013249-7 Getting Started with Visual C++ 6 with an Introduction to MFC 1 1999 1

0-13-565912-4 C++ How to Program Instructor's Manual with Solutions Disk 2 1998 1

0-13-904947-9 Java How to Program Instructor's Manual with Solution Disk 2 1997 1

0-13-020522-2 Visual Basic 6 How to Program Instructor's Manual with Solution Disk 1 1999 1

0-13-015870-4 Internet and World Wide Web How to Program Instructor's Manual with Solutions Disk 1 1999 1

0-13-082925-0 The Complete C++ Training Course 2 1998 2

0-13-082927-7 The Complete Java Training Course 2 1997 2

0-13-082928-5 The Complete Visual Basic 6 Training Course 1 1999 2

0-13-085248-1 The Complete Java Training Course 3 1999 2

0-13-085609-6 The Internet and World Wide Web How to Program Complete Training Course 1 1999 2

0-13-082714-2 C++ How to Program 2/e and Getting Started with Visual C++ 5.0 Tutorial 2 1998 1

0-13-010671-2 Java How to Program 2/e and Getting Started with Visual J++ 1.1 Tutorial 2 1998 1

0-13-083054-2 The Complete C++ Training Course 2/e and Getting Started with Visual C++ 5.0 Tutorial 2 1998 1

0-13-083055-0 The Complete Java Training Course 2/e and Getting Started with Visual J++ 1.1 Tutorial 2 1998 1

0-13-118043-6 C How to Program 1 1992 1

0-13-271974-6 Java Multimedia Cyber Classroom 1 1996 2

25.3 Relational Database Overview: Books.mdb (VII)

Page 14: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Table relationships in books.mdb

• Lines between tables represent relationships– Example: Line between Publishers and Titles

• One-to-many relationship

• Every publisher can have an infinite number of books

– PublisherID is the foreign key in the Titles table• Must correspond to primary key in Publishers table

• Maintains Rule of Referential Integrity

• AuthorISBN table- – No primary key

– Linking table between Titles and Authors

Page 15: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4 Structured Query Language

• Structured Query Language (SQL) keywords used to– Query a database

– Insert records into a database

– Update existing records in a database SQL keyword

Description

SELECT Select (retrieve) fields from one or more tables. FROM Tables from which to get fields. Required in every SELECT. WHERE Criteria for selection that determine the rows to be retrieved. ORDER BY Criteria for ordering (sorting) of records. INSERT INTO Insert values into one or more tables. [Note: Some databases do

not require the SQL keyword INTO.] UPDATE Update existing data in one or more tables.

(Note: there are other keywords not included in this table)

Page 16: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.1 Basic SELECT Query

• SELECT * FROM TableName– TableName specifies table in database where data is located– * selects all rows and fields from TableName

• SELECT * FROM Authors– Selects the entire contents of the Authors table

• SELECT AuthorID, LastName FROM Authors– To select fields from table, replace * with comma-separated list of field

names to select

– Returns the following: AuthorID LastName

1 Deitel 2 Deitel 3 Nieto

Page 17: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.1 Basic SELECT Query (II)

• Order of fields– Specifying field names ensures that the fields are returned in

the same order, even if in different order in the table• Allows result set to be processed more efficiently by the application

– SQL statement using * does not ensure that fields will be returned in the expected order

• Do not use to specify field names to select from one or several table

• Field name with spaces– If contains spaces, must be enclosed in square brackets ([])

• Error if you forget to enclose in brackets

• Therefore, try to avoid field names with spaces

Page 18: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.2 WHERE Clause

• WHERE clause– Most cases, only necessary to locate records that satisfy certain selection

criteria

– SQL uses the WHERE clause to specify the selection criteria

– Can contain operators• <, >, <=, >=, =, <> and LIKE

• Simplest form:SELECT fieldName1, fieldName2, … FROM TableName WHERE Criteria

• SQL is case-sensitive on some systems– Check your database system document to determine the syntax to be used

for keywords

• All uppercase, all lowercase, combination?

Page 19: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.2 WHERE Clause (II)

• ExampleSELECT AuthorID, FirstName, LastName, YearBorn

FROM Authors

WHERE YearBorn > 1960

• Returns AuthorID FirstName LastName YearBorn

2 Paul Deitel 1968 3 Tem Nieto 1969

Page 20: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.2 WHERE Clause (III)

• LIKE Operator– Used for pattern matching with wildcard characters

• asterisk (*)

• question mark (?)

– Allows SQL to search for similar strings that “match a pattern”

– Asterisk• Indicates any number of characters in a row at location in pattern

• Surrounded by single-quote characters

– Question Mark• Indicates a single character at that position in the pattern

• Surrounded by single-quote characters

– Not supported by all database systems

Page 21: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.2 WHERE Clause (IV)

• LIKE Operator ExampleSELECT AuthorID, FirstName, LastName, YearBorn

FROM Authors

WHERE Lastname LIKE ‘d*’

– Returns records of all authors whose last name’s begin with the letter d

AuthorID

FirstName

LastName

YearBorn

1 Harvey Deitel 1946

2 Paul Deitel 1968

Page 22: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.2 WHERE Clause (V)

• LIKE Operator Example II

SELECT AuthorID, FirstName, LastName, YearBornFROM AuthorsWHERE Lastname LIKE ‘?i*’

– Returns records of all authors whose last name second letter is the letter i

AuthorID

FirstName

LastName

YearBorn

3 Tem Nieto 1969

Page 23: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.2 WHERE Clause (VI)

• LIKE Operator Example III– Query can be specialized to allow any character in a range of

characters in one position in the string• [startValue-endValue]

SELECT AuthorID, FirstName, LastName, YearBorn

FROM Authors

WHERE Lastname LIKE ‘?[a-i]*’

– Returns records of all authors in table because all fit range

AuthorID

FirstName

LastName

YearBorn

1 Harvey Deitel 1946

2 Paul Deitel 1968

3 Tem Nieto 1969

Page 24: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.3 ORDER BY Clause

• ORDER BY Clause– Sorts results of query into ascending or descending orderSELECT fieldName1, fieldName2, … FROM TableName ORDER BY fieldName ASCSELECT fieldName1, fieldName2, … FROM TableName ORDER BY fieldName DESC

• ExampleSELECT AuthorID, FirstName, LastName, YearBornFROM AuthorsORDER BY LastName ASC

– Returns authors sorted by last name in ascending order

AuthorID

FirstName

LastName

YearBorn

2 Paul Deitel 1968

1 Harvey Deitel 1946

3 Tem Nieto 1969

Page 25: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.3 ORDER BY Clause (II)

• The WHERE and ORDER BY clauses can be combined– Example:SELECT ISBN, Title, EditionNumber, YearPublished, PublisherIDFROM TitlesWHERE Title LIKE ‘*How to Program’ORDER BY Title ASC

– Returns:

ISBN

Title

Edition Number

Year Published

Publisher ID

0-13-118043-6 C How to Program 1 1992 1

0-13-226119-7 C How to Program 2 1994 1

0-13-528910-6 C++ How to Program 2 1997 1

0-13-016143-8 Internet and World Wide Web How to Program

1 1999 1

0-13-012507-5 Java How to Program 3 1999 1

0-13-899394-7 Java How to Program 2 1997 1

0-13-456955-5 Visual Basic 6 How to Program 1 1998 1

Page 26: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.3 ORDER BY Clause (III)

• ORDER BY can be applied to multiple fieldsORDER BY fieldName1 SortingOrder, fieldName2 SortingOrder, …

• When we construct a query– Create one long string containing query

– Multiple lines and indentation only used for readability when query statement is displayed in text

Page 27: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.4 Using INNER JOIN to Merge Data from Multiple Tables

• INNER JOIN Clause– Merges data from multiple tables into single view

– Referred to as joining the tables

– Placed in the FROM clause of an SQL query

• Simplest formSELECT fieldName1, fieldName2, …

FROM Table1 INNER JOIN Table2 ON Table1.field = Table2.field

Page 28: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.4 Using INNER JOIN to Merge Data from Multiple Tables (II)

• ON part of INNER JOIN– Specifies fields from each table to be compared to determine

records to be selected

– “TableName.” syntax required if fields have same name in both tables

– Example:SELECT FirstName, LastName, ISBN

FROM Authors INNER JOIN AuthorISBN

ON Authors.AuthorID = AuthorISBN.AuthorID

ORDER BY LastName, FirstName

Page 29: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.4 Using INNER JOIN to Merge Data from Multiple Tables (III)

FirstName

LastName

ISBN FirstName

LastName

ISBN FirstName

LastName

ISBN

Harvey Deitel 0-13-013249-7 (Continued from bottom of previous column) (Continued from bottom of previous column)

Harvey Deitel 0-13-271974-6 Harvey Deitel 0-13-020522-2 Paul Deitel 0-13-085609-6

Harvey Deitel 0-13-528910-6 Harvey Deitel 0-13-020522-2 Paul Deitel 0-13-013249-7

Harvey Deitel 0-13-083055-0 Harvey Deitel 0-13-010671-2 Paul Deitel 0-13-226119-7

Harvey Deitel 0-13-565912-4 Harvey Deitel 0-13-118043-6 Paul Deitel 0-13-899394-7

Harvey Deitel 0-13-083054-2 Paul Deitel 0-13-010671-2 Paul Deitel 0-13-565912-4

Harvey Deitel 0-13-899394-7 Paul Deitel 0-13-083055-0 Paul Deitel 0-13-528910-6

Harvey Deitel 0-13-904947-9 Paul Deitel 0-13-082927-7 Paul Deitel 0-13-085248-1

Harvey Deitel 0-13-226119-7 Paul Deitel 0-13-083054-2 Paul Deitel 0-13-456955-5

Harvey Deitel 0-13-082928-5 Paul Deitel 0-13-082714-2 Paul Deitel 0-13-271974-6

Harvey Deitel 0-13-456955-5 Paul Deitel 0-13-118043-6 Paul Deitel 0-13-013249-7

Harvey Deitel 0-13-015870-4 Paul Deitel 0-13-118043-6 Tem Nieto 0-13-082928-5

Harvey Deitel 0-13-085609-6 Paul Deitel 0-13-082928-5 Tem Nieto 0-13-565912-4

Harvey Deitel 0-13-085248-1 Paul Deitel 0-13-082925-0 Tem Nieto 0-13-456955-5

Harvey Deitel 0-13-082925-0 Paul Deitel 0-13-020522-2 Tem Nieto 0-13-085609-6

Harvey Deitel 0-13-016143-8 Paul Deitel 0-13-904947-9 Tem Nieto 0-13-016143-8

Harvey Deitel 0-13-082714-2 Paul Deitel 0-13-012507-5 Tem Nieto 0-13-020522-2

Harvey Deitel 0-13-082927-7 Paul Deitel 0-13-015870-4 Tem Nieto 0-13-015870-4

Harvey Deitel 0-13-012507-5 Paul Deitel 0-13-016143-8 Tem Nieto 0-13-904947-9

(Continued at top of next column) (Continued at top of next column) Tem Nieto 0-13-904947-9

Page 30: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

24.4.5 TitleAuthor Query from Books.mdb

• Predefined query: TitleAuthor– Table containing book title, ISBN number, author’s first and last names, year

published, and publisher’s name– Note the nested INNER JOIN structures– Note: spacing and indenting only for readability

SELECT Titles.Title, Titles.ISBN, Authors.FirstName, Authors.Lastname, Titles.YearPublished, Publishers.PublisherNameFROM

( Publishers INNER JOIN TitlesON Publishers.PublisherID =

Titles.PublisherID )INNER JOIN ( Authors INNER JOIN AuthorsISBN

ON Authors.AuthorID = AuthorISBN.AuthorID )ON Titles.ISBN = AuthorsISBN.ISBN

ORDER BY Titles.Title

Page 31: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

24.4.5 TitleAuthor Query from Books.mdb (II)

Portions of TitleAuthor query outputTitle ISBN First

Name Last Name

Year Published

Publisher Name

C How to Program 0-13-226119-7 Paul Deitel 1994 Prentice Hall

C How to Program 0-13-118043-6 Paul Deitel 1992 Prentice Hall

C How to Program 0-13-118043-6 Harvey Deitel 1992 Prentice Hall

C How to Program 0-13-226119-7 Harvey Deitel 1994 Prentice Hall

C++ How to Program 0-13-528910-6 Harvey Deitel 1997 Prentice Hall

C++ How to Program 0-13-528910-6 Paul Deitel 1997 Prentice Hall

Internet and World Wide Web How to Program

0-13-016143-8 Paul Deitel 1999 Prentice Hall

Internet and World Wide Web How to Program

0-13-016143-8 Harvey Deitel 1999 Prentice Hall

Internet and World Wide Web How to Program

0-13-016143-8 Tem Nieto 1999 Prentice Hall

Java How to Program 0-13-012507-5 Harvey Deitel 1999 Prentice Hall

Java How to Program 0-13-899394-7 Paul Deitel 1997 Prentice Hall

Java How to Program 0-13-899394-7 Harvey Deitel 1997 Prentice Hall

Java How to Program 0-13-012507-5 Paul Deitel 1999 Prentice Hall

Visual Basic 6 How to Program 0-13-456955-5 Harvey Deitel 1998 Prentice Hall

Visual Basic 6 How to Program 0-13-456955-5 Paul Deitel 1998 Prentice Hall

Visual Basic 6 How to Program 0-13-456955-5 Tem Nieto 1998 Prentice Hall

Page 32: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.6 Inserting a Record

• INSERT INTO operation– Inserts data into the table (e.g; adds a record)– Simplest form:

– The single quote character should be used as a delimiter for strings to be inserted into the database

INSERT INTO TableName ( fieldName1, fieldName2, …, fieldNameN )

Values ( value1, value 2, …, valueN )

Values to be inserted into fields – in order of fields listed before

Table into which record

will be inserted

List of field names into which to insert values

(not required if inserting complete record)

KEYWORDS

Page 33: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.6 Inserting a Record

• Sample insert operation– Note: AuthorID field not specified because it has been set up in Microsoft Access, our

database software, as an auto-numbered field

INSERT INTO Authors ( FirstName, LastName, YearBorn )Values ( ‘Sue’, ‘Smith’, 1960 )

• Result

AuthorID FirstName LastName YearBorn 1 Harvey Deitel 1946

2 Paul Deitel 1968

3 Tem Nieto 1969

4 Sue Smith 1960

Page 34: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.4.7 Updating a Record

• Modify a record with the UPDATE operationUPDATE TableName

SET fieldName1 = value1, fieldName2 = value2, …,

fieldNameN = valueN

WHERE criteria

• Example:UPDATE Authors

SET YearBorn = 1969

WHERE LastName = ‘Deitel’, FirstName = ‘Paul’– Changes YearBorn value for Paul Deitel in Authors table to 1969

Page 35: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.5 Registering Books.mdb as an ODBC Data Source

• Open DataBase Connectivity (ODBC) Application Programming Interface (API)– Developed by Microsoft to allow Windows applications to communicate in

a uniform manner with relational databases

• To execute an SQL query– Program must be able to access the database

• Database must be given a System Data Source Name (DSN) on the server

• Database must be registered as an ODBC source

• ODBC Driver written by vendors– Uses ODBC API to provide uniform access to the database

• For specific instructions– See section 25.5 in your textbook

Page 36: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.6 ActiveX Data Objects (ADO)

• Microsoft’s Universal Data Access (UDA)– Architecture designed for high-performance data access to

• Relational data sources

• Non-relational data sources

• Mainframe/legacy data sources

UDA Architecture:

Application or Browser

ADO

ODBC

OLE DB

Relational data sources

Non-relational data sources

Mainframe/legacy data

Page 37: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.6 ActiveX Data Objects (ADO)

• UDA consists of three primary components– OLE DB

• Core of UDA architecture

• Provides low-level access to any data source

– ODBC• C programming language library

• Uses SQL to access data

– ActiveX Data Controls• Simple object model

• Provides uniform access to any data source by interacting with OLE DB

Page 38: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.6 ActiveX Data Objects (ADO)

• ADO object model– Provides objects and collections

Object/Collection Description

Connection object The connection to the data source.

Command object Contains the query that will interact with the database (the data source) to manipulate data.

Parameter object Contains information needed by a Command object to query

the data source.

Parameters collection Contains one or more Parameter objects.

Error object Created when an error occurs while accessing data.

Errors collection Contains one or more Error objects.

Recordset object Contains zero or more records that match the database query.

Collectively this group of records is called a recordset.

Field object Contains the value (and other attributes) of one data source field.

Fields collection Contains one or more Field objects.

Page 39: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS)

• RDS– Emerging Microsoft technology for client-side database

manipulation over the internet– Recordsets retrieved on the server using ADO and sent to

client browser• Called disconnected recordset

– Client can execute queries against the recordset– Similar to Tabular Data Control

• RDS provides mechanism for sending updated records to Web server• Tabular Data Control does not

• RDS is still developing– Future releases will work with Personal Web Server (PWS)– Server-side setup complex and developing rapidly

Page 40: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (II)RDS Architecture

• RDS still developing– Future releases will work with PWS– Server-side setup complex and developing rapidly

ADO

ODBC

OLE DB

Relational data sources

Non-relational data sources

Mainframe/legacy data

PWS/IISServer

Client Browser RDS

Page 41: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (III)

• RDS implemented as client-side ActiveX control– Named RDS.Datacontrol

Properties Table

Methods Table

Property Description Connect The data source name (DSN) of a Web server’s database.

Sql The actual query to execute against the database.

Server Indicates the Web server to which the RDS.Datacontrol connects.

Recordset The actual recordset downloaded from the Web server.

Method Description Refresh Executes a query against the database. Also forces any

records that have not been uploaded to the server to upload.

SubmitChanges Sends updated records to the Web server.

CancelUpdate Cancels all records that were updated (but not sent to the server) and reverts them back to their original values.

Page 42: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (IV)

• RDS.Datacontrol– Transfers data asynchronously

• Transfer occurs in parallel with other executing code• ReadyState property stores control’s current state

– Uses constants to declare states used in the program

– state can have one of three values• 0 – no user activity• 1 – download state• 2 – upload state

State

Description

adcReadyStateLoaded The recordset does not contain any data because the query has not completed executing.

adcReadyStateInteractive Some records in the recordset are available, but others are still being downloaded (or uploaded).

adcReadyStateComplete All recordset records are available. The download (or upload) is complete.

Page 43: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.1 Define OBJECT

2.1 Open and define <SCRIPT> tag

2.2 Set predefined ReadyState constants

2.3 Set Initial StatusText.Value

3.1 Define deitelDC_OnReadyStateChange() Sub procedure

3.2 Open Select structure inside if structure for downloading actions

1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">2<HTML>3<!--Fig. 25.38: rds.html -->45<!--Creates an instance of the RDS.Datacontrol-->6<OBJECT CLASSID = "clsid:BD96C556-65A3-11D0-983A-00C04FC29E33"7 ID = deitelDC WIDTH = 0 HEIGHT = 0>8</OBJECT>910<SCRIPT LANGUAGE = VBScript>11<!--12Option Explicit1314<!-- Microsoft predefined constants -->15Const adcReadyStateLoaded = 216Const adcReadyStateInteractive = 317Const adcReadyStateComplete = 41819Dim state2021Public Sub window_OnLoad()22 ’ Set the state machine at the first state23 StatusText.Value = "Click Find."24 state = 0 25End Sub2627Public Sub deitelDC_OnReadyStateChange()28 If state = 1 Then 29 Select Case deitelDC.ReadyState30 ’ Just started downloading, there is no 31 ’ data to look at.

Page 44: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3.3 set StatusText.value downloading

3.4 Fill in fields for updating

3.5 Set final StatusText.Value downloading value

4.1 Open select structure inside ElseIf structure for uploading

4.2 Set first StatusText.value output for uploading Case

32 Case adcReadyStateLoaded33 StatusText.Value = "Downloading..."3435 ’ Partially downloaded the data, 36 ’ there is more to come.37 Case adcReadyStateInteractive38 StatusText.Value = "Still downloading..."3940 ’ Completely downloaded the data, 41 ’ there’s no more coming.42 Case adcReadyStateComplete4344 ’ Fill in the fields for updating45 ’ Get ISBN field to prove we got the right one46 FoundISBN.Value = deitelDC.Recordset( "ISBN" )47 48 ’ The title we want to modify49 FoundTitle.Value = deitelDC.Recordset( "Title" )5051 ’ OK for updating. Everything worked.52 StatusText.Value = "Finished downloading."53 state = 2 54 End Select5556 ElseIf state = 2 Then5758 Select Case deitelDC.ReadyState59 ’ Started uploading, there is no data sent.60 Case adcReadyStateLoaded61 ’ OK for updating. Everything worked.62 StatusText.Value = "Uploading..."

Page 45: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

4.3 Finish setting StatusText.value’s for uploading

4.4 Close all structures

5.1 Define OnClick() Sub procedure

5.2 Validate input values

6364 ’ Partially uploaded the data, there is more to send.65 Case adcReadyStateInteractive66 ’ OK for updating. Everything worked.67 StatusText.value = "Still Uploading..."6869 ’ Completely downloaded the data, 70 ’ there is no more coming.71 ’ Goto readystate = complete if it works72 Case adcReadyStateComplete 73 ’ OK for updating. Everything worked.74 StatusText.value = "Finished updating."75 state = 076 End Select77 End If78 End Sub7980 Public Sub Find_OnClick()81 ’ Validate the input values. Never assume valid 82 ’ data is in the text boxes.8384 ’ Server.Value will be used to designate the 85 ’ server with the database86 If Server.Value = "" Then87 Call Msgbox( "Please specify a web server. " & _88 "Suggest: http://xxx.xxx.xxx.xxx" )8990 ’ ISBN.Value is the record we want to search for91 ElseIf ISBN.value = "" Then92 Call MsgBox( "Please specify an ISBN to examine. " & _93 "Suggest: 0-13-226119-7" )

Page 46: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

5.3 Begin data download

5.4 Set values

5.5 Tell server to begin sending data - call deitelDC.Refresh() Sub procedure

5.6 Close structures

6.1 Define Update_OnClick() Sub procedure

6.2 Fetch value from text box, setup to go back to server

94

95 ’ All data is probably valid so begin the data download.

96 Else

97 ’ Request the data from.

98 deitelDC.Server = Server.Value

99

100 ’ Set the SQL query.

101 deitelDC.SQL = "SELECT ISBN, " & _

102 "Title FROM Titles WHERE ISBN = ’" & ISBN.Value & "’"

103

104 ’ Set the DSN to fetch the data.

105 deitelDC.Connect = "DSN=Books;"

106

107 ’ Tell the server to begin sending the data to us.

108 Call deitelDC.Refresh()

109 state = 1

110 End If

111End Sub

112

113Public Sub Update_OnClick()

114 ’ If everything worked above, we can change

115 ’ the record in the database

116 If state = 2 Then

117 ’ We are only updating this field in the database.

118 ’ so we fetch the value from the text box and set it to go

119 ’ back to the server for update

120 deitelDC.Recordset( "Title" ) = FoundTitle.Value

121

Page 47: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

6.3 Save changes and refresh

6.4 Close all structures and </SCRIPT> tag

7.1 Insert and define META tag

7.2 Insert and define searching INPUTs

7.3 Insert button INPUT: “Find”

123 Call deitelDC.SubmitChanges()124125 ’ Refresh after submit operation126 Call deitelDC.Refresh() 127 End If128End Sub129-->130</SCRIPT>131132<HEAD>133<META NAME = VI60_defaultClientScript CONTENT = VBScript>134</HEAD>135136<BODY>137RDS Example--Correct titles in a remote database<BR>138Record to Find<BR>139<TABLE BORDER = "0" CELLPADDING = "0">140<TR>141<TD>Server:</TD>142<TD><INPUT ID= server NAME= server VALUE= "http://xxx.xxx.xxx.xxx"

143 TYPE = "text" SIZE = 60></TD>144</TR>145<TR>146<TD>ISBN:</TD>147<TD><INPUT ID = ISBN NAME = ISBN VALUE = "0-13-226119-7"148 TYPE = "text"></TD>149</TR>150</TABLE>151<INPUT TYPE = "button" ID = "Find" NAME = "Find" VALUE= "Find"><BR>

152<BR>153Results<BR>

122 ’ Save these changes

Page 48: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

7.4 Insert and define Results INPUT elements

7.5 Insert button INPUT: “Update Title”

7.6 Insert StatusText INPUT textbox

154<TABLE BORDER = "0" CELLPADDING = "0">

155<TR>

156<TD>Found ISBN( readonly ):</TD>

157<TD><INPUT ID = FoundISBN READONLY NAME = FoundISBN TYPE = "text">

158</TD>

159</TR>

160<TR>

161<TD>Found Title:</TD>

162<TD><INPUT ID = FoundTitle NAME = FoundTitle TYPE = "text"></TD>

163</TR>

164</TABLE>

165<INPUT TYPE = "button" ID = "Update" NAME = "Update"

166 VALUE = "Update Title"><BR>

167<BR>

168Status:<INPUT TYPE = "text" ID = "StatusText" NAME = "StatusText"

169 SIZE = 30 READONLY VALUE = "Click Find."><BR>

170</BODY>

171</HTML>

Page 49: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (V)

Document Output 1

The page is loaded

Page 50: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (VI)

Document Output 2

The use has clicked the Find button which results in the Found ISBN and Found Title fields being populated

Page 51: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (VII)

Document Output 3

User has edited the Found Title field

Page 52: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (VIII)

Document Output 4

User has clicked Update Title

Page 53: Chapter 25 – Database: SQL, ADO and RDS

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

25.7 Remote Data Services (RDS) (IX)

Document Output 5

User has clicked Find