Module 8: Accessing a Database Contents Overview Accessing Data from a Text File Using the Tabular Data Control
1 2
Accessing Data From an XML Document
11
Database Access Interfaces
19
Processing Forms
28
Lab 8: Retrieving Data from a Database Using ASP
39
Review
44
Information in this document is subject to change without notice. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Complying with all applicable copyright laws is the responsibility of the user. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation. If, however, your only means of access is electronic, permission to print one copy is hereby granted. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. 2000 Microsoft Corporation. All rights reserved. Microsoft, ActiveX, BackOffice, FrontPage 2000, Internet Explorer 5, Internet Information Server, MS-DOS, Notepad, and Windows 2000 are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Other product and company names mentioned herein may be the trademarks of their respective owners. Program Manager: Steve Merrill Development Lead: Basabjit Chakrabarty (NIIT) Instructional Designers: Sangeeta Nair, Vijayalakshmi Narayanaswamy (NIIT); Veena Nambier, Yatinder Walia (NIIT) Technical Contributors: Scott Swigart (3 Leaf Solutions); Gary Gumbiner (Great Barrier Technologies, Inc.) Graphic Artist: Scott Serna (Creative Assets) Editing Manager: Jennifer Linn Editor: Reid Bannecker
Production Manager: Miracle Davis Production Coordinator: Linda Lu Cannon (The Write Stuff) Build Coordinator: Eric Wagoner Testing Lead: Eric Meyers Testing: Bryan Urakawa, Chris and Edward Lead Product Manager, Internet Services: Hilary Vandal Manufacturing Manager: Rick Terek Operations Coordinator: John Williams Manufacturing Support: Laura King; Kathy Hershey Lead Product Manager, Release Management: Bo Galford Group Manager, Courseware Infrastructure: David Bramble General Manager: Robert Stewart
This module provides students with an overview of accessing a database. It explains the Tabular Data Control and the XML data island. It also describes the two standard data access interfaces. Then, it explains the procedure to create a data source name. This module also explains the Database Results Wizard. Finally, it explains how to process input forms by using Active Server Pages. At the end of this module, students will be able to:
!
Access data from a text file by using the Tabular Data Control (TDC).
!
Navigate through records by using the methods of TDC.
!
Access data from an XML document.
!
Describe the data access interfaces.
!
Create a data source name.
!
Connect to a data source by using Active Data Objects (ADO)
!
Process input form information by using Active Server Pages (ASP).
Materials and Preparation This section provides you with the required materials and preparation tasks that
are needed to teach this module.
Required Materials To teach this module, you need the following materials: !
Microsoft PowerPoint file, 1912A_08.ppt.
!
Module 8, “Accessing a Database”
!
Lab 8, “Retrieving Data From a Database Using ASP”
Preparation Tasks To prepare for this module, you should: !
Read all of the materials for this module.
!
Complete the lab.
iv
Module 8: Accessing a Database
Module Strategy Use the following strategy to present this module: !
Accessing Data From a Text File Using Tabular Data Control This topic provides an overview of tabular data control. First, explain how to bind a TDC to a table. Then, explain the methods provided by TDC to navigate through records. Then ask students to perform a practice on accessing data from a text file by using TDC.
!
Accessing Data From an XML Document This topic provides an overview to XML data island. First, explain how XML represents structured data. Then, how to create an XML data island with the help of the example provided. Then ask students to bind an HTML table to an XML data island.
!
Database Access Interfaces This topic provides an overview of the standard access interfaces and how to create a data source name. First, explain what is a database. Then, describe the two standard access interfaces namely, ODBC and OLE DB. Then, demonstrate the procedure to create a data source name on your computer. Then ask students to perform a practice on creating a data source name.
!
Processing Forms This topic explains how to process input forms. First, explain how to work with HTML forms. Then, describe how to establish a connection by using ADO. Finally, explain how to process input forms by using ADO. Finally, this topic explains the methods to navigate records.
Module 8: Accessing a Database
1
# Overview Slide Objective
To provide an overview of the module topics and objectives.
!
!
Database Access Interfaces
!
Processing Forms
!
Lab 8: Retrieving Data from a Database Using ASP
!
In this module, you will learn how to access data from a database by using the tabular data control, XML data island, and FrontPage 2000.
Accessing Data from an XML Document
!
Lead-in
Accessing Data from a Text File Using the Tabular Data Control
Review
Every organization maintains data pertaining to its business, employees, and finances. This data needs to be accessed, maintained, and updated on a regular basis. This can be done by storing this information in a database. Many Web sites include data that is extracted from databases or data files. Data is extracted with the help of scripts, such as Active Server Pages that run on the Web server. These scripts use SQL statements to retrieve data from a database and then send the results in an HTML format for display in a client browser.
Database technologies that run on the client are called client-side technologies. Client-side technologies return an HTML document along with a recordset to the server. The browser interprets the data and displays it to the user. The user can then work with the data without having to send queries to the server again. Two technologies that implement client-based database access are the Tabular Data Control and XML data islands. Server-side technologies, such as active data objects, have direct access to the data on the server and generate HTML pages. These HTML pages are then sent back to the client browser. After completing this module, you will be able to: !
Access data from a text file using the Tabular Data Control (TDC).
!
Navigate through records using the methods of TDC.
!
Access data from an XML document.
!
Describe the data-access interfaces.
!
Create a data source name.
!
Connect to a data source using Active Data Objects (ADO)
!
Process input form information using Active Server Pages (ASP).
2
Module 8: Accessing a Database
# Accessing Data from a Text File Using the Tabular Data Control Slide Objective
To provide an overview of the topic covered in the section.
Lead-in
After connecting to a data source, you can use the TDC to display the data on the Web page.
!
Binding a TDC to a Table
!
Navigating Records
!
Practice: Using the Tabular Data Control
TDC is a Microsoft ActiveX® control that enables you to display the contents of a text file on a Web page. The text file is read locally, which minimizes server load and provides the user with a quick access. The data can be filtered or sorted by the browser using the properties and methods of TDC. TDC does not require a Web server component. When a Web page loads into a browser, the TDC makes a request for the text file. The text file on the Web server is cached locally by the browser. The data from the file is read into the TDC, and then displayed in the controls of a document. You can use a script to filter or sort the data on the client, with no server involvement. Note When reading data from a text file, you may have files in a format that need to be separated by a delimiter. A comma-separated file uses a comma (,) as its delimiter. TDC obtains data from a comma separated value file. The tables in a comma separated value file are in a text format. All fields are separated by commas.
Module 8: Accessing a Database
3
Binding a TDC to a Table Slide Objective
To connect a TDC to a data source.
Lead-in
Properties of the TDC have to be set in order to connect to the Web server.
Consider the Web site for a travel agency that provides services, such as checking flight availability and booking tickets, to its users. The Web site needs to provide information, such as the flight numbers and departure dates. The Web site consists of a table with two columns, flight number and date. If
you want to display the table using HTML, the following code accomplishes this. Delivery Tip
Open the file tdc.htm from \Inetpub\wwwroot\Sampapp s\Ch08 in Internet Explorer and show the results to the students. From the View menu, select Source and explain the code to the students.
Creating a table in HTML involves a lot of code, which is inconvenient to edit
or update. The table above can also be displayed by using a TDC control.
4
Module 8: Accessing a Database
To instantiate the TDC control, you need to use the <OBJECT> tag. The clsid attribute of the <OBJECT> tag is a unique ID assigned to the TDC control. The TDC control declaration is shown below: width="180" height="52" ID="flight">
</object>
The ID attribute allows you to refer to the TDC control later when writing scripts, and corresponds to the datasrc attribute. The datasrc attribute identifies the TDC and the datafld attribute specifies the name of a column in the text file that contains the data. The above table can also be displayed by using TDC control as shown in the following code: <TABLE DATASRC="#flight"> <THEAD> <TR> <TD>Flight Number</TD> <TD>Scheduled Date</TD> </TR> </THEAD> <TBODY>
To connect the TDC to a text file, the following properties can be set: !
DataURL The DataURL property specifies the location of a file as a URL. The DataURL property is specified in the <OBJECT> tag for the TDC. However, changing this property after the page has been loaded causes the file to be loaded on the next reset event.
!
FieldDelim The FieldDelim property identifies the character that is used to mark the end of the file. FieldDelim separates one data field from the next. The default character is a comma, but you can set the default to any other character.
!
RowDelim The RowDelim property identifies the character used to mark the end of each row. RowDelim separates one data row from the next. The default value is the newline character, but you can set the default to any other
character.
!
TextQualifier The TextQualifier property is a single-character string that is used to surround fields that can include special characters, such as newlines, tabs, or commas. You can also use these special characters to delimit fields or rows. The default value is the double-quotation mark.
Module 8: Accessing a Database
!
5
Language The Language property specifies the language used to create the file. By default, the value is English.
!
UseHeader The UseHeader property is a Boolean expression that contains an initial line that names each column. By default, the value is False. If UseHeader is set to True, the first line of the data file should match the following syntax: fieldname:type, fieldname:type The fieldname variable is an arbitrary text string. The type variable can assume one of the following values.
Value
Description
String
Textual data (default)
Date
Calendar dates
Boolean
Logical data (Yes/No, True/False, 0/not 0)
Int
Integral numbers ( – 3, 1, 5)
Float
Floating-point numbers (3.141, 2.77, 0)
6
Module 8: Accessing a Database
Navigating Records
Slide Objective
To provide an overview of the various methods for navigating through records.
!
Nextpage()
!
FirstPage()
!
Previouspage()
!
LastPage()
Lead-in
You can limit the number of records displayed at one time by using the DATAPAGESIZE attribute.
Sub btnnext_OnClick()
Sub btnnext_OnClick() mytable.Nextpage() mytable.Nextpage() End Sub End Sub Sub btnprev_OnClick() Sub btnprev_OnClick() mytable.Previouspage() mytable.Previouspage() End Sub End Sub (View Navigate.htm from Sampapps)
You can create buttons in a document that enable users to navigate through the records of a recordset. This allows you to display only one record at a time. The methods that you can use to move from one record to another are: !
Nextpage() This method causes the next set of records to be displayed in a table.
!
Previouspage() This method causes the previous set of records to be displayed in a table.
!
Firstpage() This method causes the first set of records to be displayed in a table.
!
Lastpage() This method causes the last set of records to be displayed in a table.
Binding a table to a TDC control allows you to display multiple records at once. The table automatically creates as many rows as in the recordset. You can use the datapagesize attribute of the TDC control to limit the number of records displayed at one time.
Module 8: Accessing a Database
7
Consider the following example that uses the datapagesize attribute to display one record at a time. Also, the Nextpage and Previouspage methods are used to display the previous and next set of records: Delivery Tip
Open the file Navigate. htm from \Inetpub\wwwroot\1912\Sam papps\Ch08 in Internet Explorer and show the results to the students. From the View menu, select Source and explain the code to the students.
<HTML> <HEAD><TITLE>USING ADO</TITLE> <SCRIPT LANGUAGE="VBSCRIPT"> mytable.Nextpage() end Sub Sub btnprev_OnClick() mytable.previouspage() end Sub --> </SCRIPT> </HEAD> <BODY> <CENTER>
Practice: Using the Tabular Data Control Slide Objective
To introduce the practice.
Lead-in
In this practice, you will use the tabular data control to view the data from a text file.
In this practice, you will create a Web page using FrontPage. You will use the
Tabular Data Control to view the data from a text file called employee.txt. Also, you will create four buttons to navigate through all the records. You will then view the page in Internet Explorer to see the results.
! Create a Web page and add a TDC using FrontPage 2000 1. In FrontPage 2000, create a new one-page Web under http://localhost/1912/Practices/Mod08/TDC. 2. Import the data source file employee.txt from \Inetpub\wwwroot\1912\Practices\Mod08 to http://localhost/1912/Practices/Mod08/TDC. a. On the File menu, click Import. b. In the Import dialog box, click Add File. c. Browse to \Inetpub\wwwroot\1912\Practices\Mod08\employee.txt and click Open. d. Click OK to close the Import dialog box. 3. On the Insert menu, point to Advanced, and then click ActiveX Control. 4. In the Insert ActiveX Control dialog, click Tabular Data Control, and then click OK. Note In case you don’t see Tabular Data Control in the Insert ActiveX Control dialog box, click the Customize button in the Insert ActiveX Control dialog box, and select Tabular Data Control from the list that appears to add it to the Insert ActiveX Control dialog box.
Module 8: Accessing a Database
9
! Set the properties for the TDC 1. Right-click the TDC on the page, and then click ActiveX Control
Properties on the shortcut menu. 2. Type Employee in the Name field of the ActiveX Control Properties dialog box. The Name field sets the Id of the ActiveX control. 3. Click the Parameters tab. This dialog box displays the properties of the TDC. You need to set two properties, UseHeader and DataURL. a. Set the value for the UseHeader property to 1. Click the UseHeader attribute, click the Modify button, type 1 in the Data field, and then click the OK button. b. Next, set the DataURL property to specify the URL for the data file. Scroll down to the DataURL attribute, click the Modify button and type employee.txt in the Data field, and then click OK. 4. Click OK to close the ActiveX Control Properties dialog box.
! Create a table 1. On the Table menu, point to Insert, and select the Table option. 2. In the Insert Table dialog box, set the value of Rows to 1 and Columns to 3, and then click OK. 3. Now switch to the HTML view in FrontPage and modify the table code as follows: a. Specify the names of the columns as First Name, Last Name, and Designation. b. Add a row with three columns, where the datafld attribute of each column is set to a column name in employee.txt: First Name, Last Name, and Designation. c. Set the datasrc attribute to #employee. d. Set the datapagesize attribute to 1, and the Id attribute to mytable. Your completed code should resemble the following:
! Add navigation buttons 1. In the Normal pane of FrontPage, on the Insert menu, point to Form, and then click Push Button, to add four navigation buttons: NEXT, PREVIOUS, LAST, and FIRST. Note After you add a button, make sure that you remove the form code or move the buttons outside the form. 2. Double-click each button and specify a value and name for each from the following list. Button
Name
Value
NEXT
btnnext
NEXT
PREVIOUS btnprevious
PREVIOUS
FIRST
btnfirst
FIRST
LAST
btnlast
LAST
3. For each button, add an event procedure for the OnClick event. Your code should resemble the following: <SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript> mytable.Nextpage() End Sub Sub btnprevious_OnClick() mytable.previouspage()
End Sub Sub btnfirst_OnClick() mytable.firstpage() End Sub Sub btnlast_OnClick() mytable.lastpage() End Sub --> </SCRIPT>
! View the document in Internet Explorer 5 • Save the file as employeedata.htm, and then preview it in Internet Explorer 5.0 by clicking the Preview in Browser icon on the FrontPage toolbar.
Module 8: Accessing a Database
11
# Accessing Data From an XML Document Slide Objective
To provide an overview of accessing data from an XML document.
Lead-in
Internet Explorer 5.0 supports embedded XML
documents called XML data islands.
!
Representing Structured Data
!
Using an XML Data Island
!
Binding an HTML Table to an XML Data Island
!
Practice: Binding an HTML Table to an XML Data Island
Internet Explorer 5.0 provides a number of features for Web developers, such as data binding through combining ActiveX objects on the client and Web server. In addition, Internet Explorer 5.0 also supports embedded XML documents, called XML data islands. XML data islands allow the client browser to manipulate data. XML data has a defined structure, rather than plain HTML that only contains presentation information. In HTML, data in tables and form elements intermingle with the style and presentation details. XML has the advantage of displaying its structure to the Web browser without including source or presentation information.
12
Module 8: Accessing a Database
Representing Structured Data Slide Objective
!
To provide an overview of structured data.
XML: $
Lead-in
$
XML provides a structured representation of data that enables precise declaration of content.
!
Provides a format for describing structured data Provides a structured representation of data that is easy to implement
XML can be used to markup: $
An ordinary document
$
A structured record, such as a purchase order
$
$
An object with data and methods, such as an ActiveX control A data record, such as the result set of a query
XML is a markup language that provides a format for describing structured data. XML enables precise declarations of content and provides a structured representation of data that is easy to implement. XML provides a data standard that can encode the content of, and be used to mark up, the following: !
An ordinary document.
!
A structured record, such as a purchase order.
!
An object with data and methods, such as an ActiveX control.
!
A data record, such as the result set of a query.
The data that is on the client computer can be manipulated, edited, and presented in multiple views without returning to the server. Since data is exchanged in the XML format, it can be easily merged from different sources. XML differentiates between the user interface and structured data. HTML specifies how to display data in a browser, and XML defines the content. XML enables you to display and process the data by applying style sheets.
Module 8: Accessing a Database
Using an XML Data Island Slide Objective
To provide an overview of XML data island.
Lead-in
XML data islands are XML data embedded in an HTML page.
!
An XML data island can be embedded in an HTML page by using the XML element
You need to use an XML data island in order to access data from an XML document, unlike TDC that accesses data from a text file. An XML data island is XML data embedded into an HTML page. XML data islands enable you to link to an external file. They enable you to use an XML document without having to load it through the use of a script or the <OBJECT> tag. An XML data island can be embedded in an HTML page by using the XML element. The XML element marks the beginning of the data island. The XML element is available in the HTML Document Object Model. An XML data island can be specified either by embedding directly in the HTML page or by referencing an XML document from the HTML document.
13
14
Module 8: Accessing a Database
Embedding an XML data island You can embed XML tags directly in an HTML page by using the <XML> and </XML> tags as shown in the following code, which contains an XML data island: <HTML> <HEAD> <title>XML Data Island</title> </HEAD> <BODY> <XML ID="MyXMLData"> <?xml version="1.0" ?> <employees>
In the example above, the ID attribute MyXMLData represents the name to refer the data island. The methods and properties of the XML element can be used to access the root and child nodes. In the example, <employees> represents the root node and <employee> represents the child nodes. Note Unlike HTML, XML is case-sensitive. The following table displays the properties and methods of an XML element: Method or Property
Description
XMLDocument
To access data within the XML element
DocumentElement
Returns the root node
ChildNodes
Returns a list containing the child nodes
Item
Returns individual child node identified with an index value.
Text
Returns the data contained in a node
Referencing an XML data island You can also reference an external file that contains XML content by using the XML tag. The SRC attribute specifies the path to the required XML file: <XML SRC="http://localhost/xmlFile.xml"></XML>
Module 8: Accessing a Database
Binding an HTML Table to an XML Data Island Slide Objective
To bind an HTML table to an XML data island.
Lead-in
You will now learn to bind an HTML table to an XML data island.
!
For each TD, set the DATAFLD attribute <?xml version="1.0" ?> <?xml version="1.0" ?> <XML ID="MyXMLData"> <XML ID="MyXMLData"> <employees> <employees> <employee> <employee> <name>Roberts</name> <name>Roberts</name> <salary>23500</salary> <salary>23500</salary> </employee> </employee> </employees> </employees> </XML> </XML>
When an XML data island is embedded or referenced in an HTML page, HTML elements can be bound directly to the XML data island. You can bind an HTML table to an XML data island and define an element that binds each column of the table. This is known as data binding. XML data can also be bound to an HTML TABLE element.
15
16
Module 8: Accessing a Database
Consider the following sample code that shows an XML data island in an HTML page: <HTML> <BODY> <XML ID="xmlisland"> <?xml version="1.0"?> <company> <employee> <FirstName>Andrew</FirstName> <LastName>Fuller</LastName> <Designation>Vice President Sales</Designation> </employee>
Practice: Binding an HTML Table to an XML Data Island Slide Objective
To introduce the practice.
Lead-in
In this practice, you will bind an HTML table to an XML data island.
In this exercise, you will embed an XML data island in an HTML page and bind the elements to an HTML table.
! Create a form in FrontPage 2000 1. In FrontPage, create a new one-page Web under http://localhost/1912/Practices/Mod08/XML. 2. Switch to the HTML view, and title the page data binding. 3. Under the <BODY> tag, create an XML data island. The XML data island code should resemble the following: <?xml version="1.0"?>
! Add a table to the page 1. On the Table menu, point to Insert, and click Table. 2. In the Insert Table dialog box, specify the value of Rows as 1, Columns as 2, and then click OK.
3. Switch to the HTML view in FrontPage, and modify the table code as follows: a. Specify the names for the columns as Title and Price. b. Add a row with the datafld attributes set as the column names in the XML data, Name and Price. c. Set the datasrc attribute to xmlisland. Your completed code should resemble the following: <TABLE border=1 width =”100%’ DATASRC="#xmlisland"> <THEAD><TR> <TD>TITLE</TD> <TD>PRICE</TD> </TR></THEAD> <TBODY> <TR> <TD><DIV DATAFLD="Name"></DIV></TD> <TD><DIV DATAFLD="Price"></DIV></TD> </TR> </TBODY> </TABLE>
4. Save the page.
! View the page • Click the Preview in Browser button on the FrontPage toolbar to view the page.
Module 8: Accessing a Database
19
# Database Access Interfaces Slide Objective
To provide an overview of database access interfaces.
!
Standard Database Access Interfaces
Lead-in
!
Connecting to a Data Source
!
Practice: Creating a Data Source Name
!
Demonstration: Connecting to a Database Using FrontPage 2000
Microsoft provides database access interfaces to access data from databases.
The Web site for a travel agency, mentioned in the beginning of the module, provides services, such as checking seat availability and booking flights, to its users. The site requires its users to fill a form and submit it. When the user submits the form, an ASP script running on the agency's Web server processes the form. The ASP script takes the form input and, if required, connects to a database. After processing the form data, it sends the results back to the user as a Web page that appears in their browser. Note that in this example, data is accessed from a database to display to the user. However, you can also write data to a database. Database-access interfaces are used for both accessing and writing data to a database. Microsoft's database-access interfaces are designed to provide high performance access to information across an organization. Database-access interfaces provide access to a variety of data and information sources that work with various tools and languages. The standard database interfaces are: !
Open Database Connectivity (ODBC)
!
Object Linking and Embedding Database (OLE DB)
20
Module 8: Accessing a Database
Standard Database Access Interfaces Slide Objective
To present the standard database interfaces.
Data Consumers ASP Page
Other Applications
ADO
Lead-in
To access data from a database, Microsoft provides interfaces, such as ODBC and OLE DB.
Visual Basic Application
OLE DB Service Components Cursor Engine
Query Processor
Data Providers ODBC Provider ODBC
Database
Simple Data Provider
OLE DB Provider
Non Mainframe Relational Data Data
Database
A database is a collection of data stored in a tabular form. A database stores related data on a particular subject, such as tracking customer orders or maintaining employee information. A database allows you to maintain a large amount of data in a structured way. You can easily access, update, and delete data from a database. A Microsoft Access database contains tables, queries, forms, and reports to access data pages. Data in linked tables may reside in another Access database, in an external data source such as Microsoft Excel, or in an ODBC data source such as Microsoft SQL Server™. A Microsoft Access database is saved with the extension .mdb. Database systems provide a driver that conforms to a standard database interface. The standard database interfaces are ODBC and OLE DB.
ODBC ODBC is designed to allow access to relational database systems, such as Access and Oracle. Most database systems provide an ODBC driver. The driver provides the application with functionality to access a database. In order to
switch between databases, you need to switch the ODBC drivers. Note A driver is a program that translates data queries of an application into commands that help access a database. ODBC is an international standard for manipulating relational data using SQL query syntax across different data sources.
Module 8: Accessing a Database
21
Features of ODBC !
ODBC is SQL-based.
!
ODBC interface supports a broad range of tools.
!
ODBC has a plug-in model that simplifies implementation. The disadvantage of using ODBC is that it is limited to relational databases only, such as SQL.
OLE DB OLE DB is an open standard designed to access all kinds of data. OLE DB has an advantage over ODBC in that it imposes no specific limitation on either the query syntax or the structure of data. For example, you can use OLE DB to
access SQL databases as well as any other type of database that provides an OLE DB driver.