Contents
Overview 1
Accessing Data from a Text File Using the
Tabular Data Control 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
Module 8: Accessing a
Database
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
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
Module 8: Accessing a Database iii Instructor Notes
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)
!
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
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).
Slide Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
how to access data from a
database by using the
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.
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.
Note
Module 8: Accessing a Database 3 Binding a TDC to a Table
!
Properties of TDC
$
DataURL
$
FieldDelim
$
RowDelim
$
<THEAD>
<TR>
<TD>Flight Number</TD>
<TD>Scheduled Date</TD>
</TR>
</THEAD>
<TBODY>
<TR><TD>F001</TD> <TD>02/03/00</TD></TR>
<TR><TD>F002</TD> <TD>02/05/00</TD></TR>
<TR><TD>F003</TD> <TD>02/04/00</TD></TR>
<TR><TD>F004</TD> <TD>02/05/00</TD></TR>
<TR><TD>F005</TD> <TD>02/03/00</TD></TR>
</TBODY>
</TABLE>
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.
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.
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
<TD><DIV DATAFLD="Flight Number"></DIV></TD>
<TD><DIV DATAFLD="Scheduled Date"></DIV></TD>
</TR>
</TBODY>
</TABLE>
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.
Nextpage()
!
Previouspage()
(View Navigate.htm from Sampapps)
Sub btnnext_OnClick()
mytable.Nextpage()
End Sub
Sub btnnext_OnClick()
mytable.Nextpage()
End Sub
Sub btnprev_OnClick()
mytable.Previouspage()
End Sub
Sub btnprev_OnClick()
mytable.Previouspage()
End Sub
!
FirstPage()
!
LastPage()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()
<!--
Sub btnnext_OnClick()
mytable.Nextpage()
end Sub
Sub btnprev_OnClick()
mytable.previouspage()
end Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<CENTER><h2>Binding Data to a Table</h2></CENTER>
<OBJECT CLASSID="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"
width="180" height="52" id="employee">
<param name="FieldDelim" value=",">
<param name="DataURL" value="employee.txt">
<param name="Useheader" value="True">
</OBJECT>
<TABLE DATASRC="#employee" ID="mytable" DATAPAGESIZE="1">
<THEAD>
<TR>
<TD>First Name</TD>
<TD>Last Name</TD>
<TD>Designation</TD>
</TR>
</THEAD>
<TBODY>
<TR>
<TD><SPAN DATAFLD="First Name"></SPAN></TD>
<TD><SPAN DATAFLD="Last Name"></SPAN></TD>
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. 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.
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.
Note
Module 8: Accessing a Database 9
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:
<TABLE border="1" width="100%" datasrc="#employee"
id="mytable" datapagesize="1">
<TR>
<TD width="33%">First Name</TD>
<TD width="33%">Last Name</TD>
<TD width="34%">Designation</TD>
</TR>
<TR>
<TD ><SPAN datafld="First Name"> </SPAN></TD>
<TD><SPAN datafld="Last Name"> </SPAN></TD>
<TD><SPAN datafld="Designation"> </SPAN></TD>
</TR>
</TABLE>
10 Module 8: Accessing a Database !
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.
After you add a button, make sure that you remove the form code or
move the buttons outside the form.
• 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.
Note
Module 8: Accessing a Database 11 #
##
#
Accessing Data From an XML Document
!
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
$
A data record, such as the result set of a queryXML 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.
Slide Objective
To provide an overview of
structured data.
Lead-in
XML provides a structured
representation of data that
<INPUT TYPE="TEXT"
DATASRC="#MyXMLData"
DATAFLD="salary">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.
Slide Objective
To provide an overview of
XML data island.
Lead-in
XML data islands are XML
data embedded in an HTML
page.
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>
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>
Note
Module 8: Accessing a Database 15 Binding an HTML Table to an XML Data Island
!
For each TD, set the DATAFLD attribute
<?xml version="1.0" ?>
<XML ID="MyXMLData">
<employees>
<employee>
<name>Roberts</name>
<salary>23500</salary>
</employee>
</employees>
</XML>
<?xml version="1.0" ?>
<XML ID="MyXMLData">
<employees>
<employee>
<name>Roberts</name>
<salary>23500</salary>
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>
<employee>
<FirstName>Laura</FirstName>
<LastName>Callahan</LastName>
<Designation>Inside Sales Coordinator</Designation>
</employee>
<employee>
<FirstName>Robert</FirstName>
<LastName>King</LastName>
<Designation>Sales Representative</Designation>
</employee>
<employee>
<FirstName>Steven</FirstName>
<LastName>Buchanan</LastName>
<Designation>Sales Manager</Designation>
</employee>
</company>