1. Trang chủ
  2. » Công Nghệ Thông Tin

Module 8: Accessing a Database

50 419 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Accessing a Database
Tác giả Steve Merrill, Basabjit Chakrabarty, Sangeeta Nair, Vijayalakshmi Narayanaswamy, Veena Nambier, Yatinder Walia, Scott Swigart, Gary Gumbiner, Scott Serna, Jennifer Linn, Reid Bannecker, Miracle Davis, Linda Lu Cannon, Eric Wagoner, Eric Meyers, Bryan Urakawa, Chris, Edward, Hilary Vandal, Rick Terek, John Williams, Laura King, Kathy Hershey, Bo Galford, David Bramble, Robert Stewart
Trường học Microsoft Corporation
Chuyên ngành Database Access
Thể loại module
Năm xuất bản 2000
Thành phố Redmond
Định dạng
Số trang 50
Dung lượng 0,93 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Contents Overview 1 Accessing Data from a Text File Using the Accessing Data From an XML Document 11... Access data from a text file by using the Tabular Data Control TDC.. Accessing D

Trang 1

Contents

Overview 1

Accessing Data from a Text File Using the

Accessing Data From an XML Document 11

Trang 2

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

Trang 3

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)

! 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

Presentation:

140 Minutes

Lab:

60 Minutes

Trang 4

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

Trang 5

# Overview

Tabular Data Control

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)

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

Trang 6

# Accessing Data from a Text File 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

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 (,)

After connecting to a data

source, you can use the

TDC to display the data on

the Web page

Note

Trang 7

width="180" height="52" ID="flight">

<param name="FieldDelim" value=",">

<param name="DataURL" value="source.txt">

<param name="Useheader" value="True">

</object>

<object classid=

"clsid:333C7BC4-460F-11D0-BC04-0080C7055A83"

width="180" height="52" ID="flight">

<param name="FieldDelim" value=",">

<param name="DataURL" value="source.txt">

<param name="Useheader" value="True">

</object>

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

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

menu, select Source and

explain the code to the

students

Trang 8

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:

<object classid="clsid:333C7BC4-460F-11D0-BC04-0080C7055A83" width="180" height="52" ID="flight">

<param name="FieldDelim" value=",">

<param name="DataURL" value="source.txt">

<param name="Useheader" value="True">

</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:

<TD><DIV DATAFLD="Flight Number"></DIV></TD>

<TD><DIV DATAFLD="Scheduled Date"></DIV></TD>

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

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

Trang 9

! 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)

Trang 10

Sub btnnext_OnClick()

mytable.Nextpage()End Sub

Sub btnprev_OnClick()

mytable.Previouspage()End Sub

Sub btnprev_OnClick()

mytable.Previouspage()End Sub

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:

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

Slide Objective

To provide an overview of

the various methods for

navigating through records

Lead-in

You can limit the number of

records displayed at one

time by using the

DATAPAGESIZE attribute

Trang 11

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:

<HTML>

<HEAD><TITLE>USING ADO</TITLE>

<SCRIPT LANGUAGE="VBSCRIPT">

<! Sub btnnext_OnClick() mytable.Nextpage() end Sub

Sub btnprev_OnClick() mytable.previouspage() end Sub

<param name="FieldDelim" value=",">

<param name="DataURL" value="employee.txt">

<param name="Useheader" value="True">

<TD><SPAN DATAFLD="First Name"></SPAN></TD>

<TD><SPAN DATAFLD="Last Name"></SPAN></TD>

<TD><SPAN DATAFLD="Designation"></SPAN></TD>

</TR>

</TBODY>

</TABLE>

<INPUT TYPE="button" VALUE="Next" NAME="btnnext" >

<INPUT TYPE="button" VALUE="Prev" NAME="btnprev" >

Explorer and show the

results to the students From

the View menu, select

Source and explain the

code to the students

Trang 12

Practice: Using the Tabular Data Control

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

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

Trang 13

! 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:

<TABLE border="1" width="100%" datasrc="#employee" id="mytable" datapagesize="1">

<TD ><SPAN datafld="First Name"> </SPAN></TD>

<TD><SPAN datafld="Last Name"> </SPAN></TD>

<TD><SPAN datafld="Designation"> </SPAN></TD>

</TR>

</TABLE>

Trang 14

! 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

2 Double-click each button and specify a value and name for each from the following list

PREVIOUS btnprevious PREVIOUS

3 For each button, add an event procedure for the OnClick event Your code

should resemble the following:

<SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>

<! Sub btnnext_OnClick() 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

Note

Trang 15

# Accessing Data From an XML Document

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

Trang 16

Representing Structured Data

! XML:

to implement

control

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

representation of data that

enables precise declaration

of content

Trang 17

Using an XML Data Island

by using the XML element

<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

Trang 18

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:

used to access the root and child nodes In the example, <employees>

represents the root node and <employee> represents the child nodes

Unlike HTML, XML is case-sensitive

The following table displays the properties and methods of an XML element:

XMLDocument To access data within the XML element

DocumentElement Returns the root node

ChildNodes Returns a list containing the child nodes

with an index value

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

Trang 19

Binding an HTML Table to an XML Data Island

Trang 20

Consider the following sample code that shows an XML data island in an HTML page:

Trang 21

Practice: Binding 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:

Trang 22

! 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">

4 Save the page

! View the page

Click the Preview in Browser button on the FrontPage toolbar to view the

page

Trang 23

# Database Access Interfaces

FrontPage 2000

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)

Slide Objective

To provide an overview of

database access interfaces

Lead-in

Microsoft provides database

access interfaces to access

data from databases

Trang 24

Standard Database Access Interfaces

Visual Basic Application

OLE DB ADO

ODBC

Simple Data Provider

Mainframe Data

Non Relational Data

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

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

provides interfaces, such as

ODBC and OLE DB

Note

Trang 25

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

Ngày đăng: 05/11/2013, 12:15

TỪ KHÓA LIÊN QUAN