Tài liệu Module 8: Accessing a Database pdf

50 437 0
Tài liệu Module 8: Accessing a Database pdf

Đ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

Module 8: Accessing a Database Contents Overview Accessing Data from a Text File Using the Tabular Data Control 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 Module 8: Accessing a Database iii Instructor Notes Presentation: 140 Minutes Lab: 60 Minutes 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 # 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) 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 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 ! Properties of TDC $ DataURL $ TextQualifier $ FieldDelim $ Language $ RowDelim $ UseHeader (View tdc.htm from Sampapps) 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 Flight Number Scheduled Date F001 02/03/00 F002 02/05/00 F003 02/04/00 F004 02/05/00 F005 02/03/00 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 Module 8: Accessing a Database To instantiate the TDC control, you need to use the tag The clsid attribute of the tag is a unique ID assigned to the TDC control The TDC control declaration is shown below: 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: Flight Number Scheduled Date 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 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 ! 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) 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 32 Module 8: Accessing a Database Using a Recordset Object Lead-in The Recordset object allows your application to access data returned from a table or a SQL query ! Declaring a Recordset object set rs = server.createobject("ADODB.recordset") set rs = server.createobject("ADODB.recordset") ! Opening a Recordset object set rs = _ set rs = _ cn.Execute("Select fname, lname from details") cn.Execute("Select fname, lname from details") The Recordset object of ADO provides methods and properties for manipulating data The Recordset object can be used to add, update, or delete records in the recordset When a Recordset object is created, a cursor is automatically opened The following table shows the methods of a Recordset object: Method Description Open Opens a cursor on a recordset Close Closes an open recordset MoveFirst Moves to the first record in the recordset MoveLast Moves to the last record in the recordset MovePrevious Moves to the previous record in the recordset MoveNext Moves to the next record in the recordset Module 8: Accessing a Database 33 Consider the following example that uses the Connection and Recordset object to retrieve data from an access database, Details The data source name is MyDatabase: USING ADO Accessing Data From a Database ID First Name Last Name Designation 34 Module 8: Accessing a Database Processing Forms Using Active Server Pages Slide Objective To describe the role of ASPs in the processing of HTML forms Lead-in Microsoft ASPs are serverside scripts that enable you to retrieve data from a database HTML forms enable users to query a database and retrieve specific information With ADO, you can create scripts that collect user form information, create a custom database query, and return information to the user ASP is a server-side scripting environment With ASP, you can combine HTML pages, scripts, and ActiveX controls to create interactive Web pages ASP scripts run when a browser requests an ASP file from a Web server The Web server reads the ASP file, carries out the commands, and sends the results to the browser in standard HTML format Using the ASP Request object, you can retrieve information entered into an HTML form When a form submits its information to the Web server, the browser requests the ASP file This is specified in the ACTION attribute of the tag The ASP file contains scripts that process the form, such as displaying a table of results or querying information from a database The ASP Request object provides the QueryString collection and the Form collection for retrieving form information Module 8: Accessing a Database 35 Consider the following example that consists of two files, form.htm and Connect.asp The file form.htm consists of a form with fields for first name, last name, and designation The file Connect.asp retrieves the form inputs and stores the data in the database, db1.mdb The code for form.htm is as follows: MODULE 8

First Name

Last Name

Designation

36 Module 8: Accessing a Database The Request object of ASPs is used to retrieve the form inputs and the data is stored in a database The Connection object and the Recordset object are used to display data from the database The code for Connect.asp is as follows: USING ADO Accessing Data From a Database First Name Last Name Designation Module 8: Accessing a Database Navigating Records Slide Objective ! Lead-in MoveFirst ! MoveNext ! To describe the methods used to navigate records MovePrevious ! MoveLast Sub Previous_onClick Sub Previous_onClick rs.movePrevious rs.movePrevious If rs.bof then If rs.bof then rs.movefirst rs.movefirst End if End if End Sub End Sub ADO provides methods to navigate through the records in a recordset Sub Next_onClick Sub Next_onClick rs.MoveNext rs.MoveNext If rs.eof then If rs.eof then rs.MoveLast rs.MoveLast End If End If End Sub End Sub Delivery Tip Open ADO.asp from \Inetpub\wwwroot\1912\Sam papps and explain the use of loops and EOF properties with the Recordset object Sub Sub until rs.EOF until rs.EOF response.write "" response.write "" response.write "" & _ response.write "" & _ rs("FName") & "" rs("FName") & "" response.write "" & _ response.write "" & _ rs("LName") & "" rs("LName") & "" response.write "" response.write "" rs.MoveNext rs.MoveNext loop loop (View ADO.asp from Sampapps) The results of a query are stored as records in rows There are methods that allow you to navigate through the records in a recordset 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 There are four methods that you can use to move from one record to another They are: ! MoveFirst ! MovePrevious ! MoveNext ! MoveLast The following code points to the first record in the recordset: Sub Previous_onClick rs.MoveFirst End Sub When using the MovePrevious and MoveNext methods, you must be careful not to move past the end of the recordset The BOF and EOF properties of the recordset object check for the first and last record The BOF property is set to True if the record pointer is positioned before the first record in the recordset Similarly, the EOF property is set to True when the record pointer is positioned after the last record 37 38 Module 8: Accessing a Database The following code uses the MovePrevious and the MoveLast property to navigate through the records in the recordset The code also shows the use of the BOF and EOF properties: Sub window_onLoad Set rs = MyDSO.Recordset End Sub Sub Previous_onClick rs.movePrevious If rs.bof then rs.movefirst End if End Sub Sub Next_onClick rs.MoveNext If rs.eof then rs.MoveLAst End If End Sub The following example uses the Do…Until loop to move through all records in … a recordset until the value of EOF is True: until rs.EOF response.write response.write response.write response.write rs.MoveNext loop "" "" & rs("FName") & "" "" & rs("LName") & "" "" Module 8: Accessing a Database 39 Lab 8: Retrieving Data from a Database Using ASP Slide Objective To introduce the lab Lead-in In this lab, you will retrieve data from a database using ASP Explain the lab objectives To retrieve data from a database using ASP Objectives After completing this lab, you will be able to: ! Add data to a database by using ASP ! Access data from a database by using the ADO Connection object Prerequisites Before working on this lab, you must have: ! Created a Web page using Microsoft FrontPage 2000 ! Retrieved form input by using ASP Scenario Contoso Ltd wants to create a guest book where visitors can enter the company details Visitors need to enter the customer id, company name, name of the person to contact, contact title, address, city, and country The information of the visitors is stored in a database Also, visitors can view the guest book database for information about a particular company Estimated time to complete this lab: 60 minutes 40 Module 8: Accessing a Database Exercise Creating a Guest Form In this exercise, you will create a form in FrontPage 2000 ! Create a form in FrontPage 2000 Create a new one-page Web under http://localhost/1912/Mywebpages/Lab08web On the page, add a form with seven text boxes and a Submit button Label these text boxes as Customer ID, Company Name, Contact Name, Contact Title, Address, City, and Country Specify the name for the text boxes as txtid, txtname, txtcontact, txttitle, txtaddress, txtcity, and txtcountry respectively Set the form's Action property to database.asp (You will create database.asp in a later exercise) Save the page as Labform.htm Module 8: Accessing a Database Exercise Create a Data Source Name In this exercise, you will create a data source name ! Create a data source name • Create a data source name with the following specifications: Name: MyNewDatabase Driver: Microsoft Access Driver Database: Contoso.mdb You will find Contoso.mdb under /Labs/Lab08/Starter 41 42 Module 8: Accessing a Database Exercise Accessing data from a database In this exercise, you will create an ASP file to access data from a database ! Access data from a database Open a new page Use the REQUEST object to retrieve form input Open a connection with the data source MyNewDatabase by using the CONNECTION object of ADO Create a recordset object to retrieve from and insert data into the database Create a SQL query to insert visitor data into the database Create a table to retrieve visitor data from the database To see an example of what your ASP should look like, see Appendix A or the Student CD-ROM To view the instructor sample code, seeAppendix A, Lab 8, Exercise Save the file as database.asp Module 8: Accessing a Database Exercise Viewing the file In this exercise, you will view the file ! View the file Open the file Labform.htm in Internet Explorer 5.0 Enter information about a customer, submit the information, and view the results 43 44 Module 8: Accessing a Database Review Slide Objective To reinforce module objectives by reviewing key points Lead-in The review questions cover some of the key concepts taught in the module ! Access data from a text file using 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 ! Access a database by using the Database Results Wizard in FrontPage 2000 ! Connect to a data source by using Active Data Objects (ADO) ! Process input form information by using Active Server Pages (ASPs) How you instantiate a TDC control? To instantiate a TDC control you use the tag The clsid attribute assigns a unique id to the TDC control and the ID attribute is the name with which you can refer to the TDC control later Which attribute of a TDC control allows you to specify the number of records to be displayed at one time? The datapagesize attribute of the TDC control allows you to limit the number of records displayed at one time What is an XML data island? An XML data island is XML data embedded in an HTML page It allows you to use an XML document without having to load it by means of a script or the tag Which object of ADO is used to establish a connection with a data source? The Connection object of ADO is used to establish a connection with a data source Module 8: Accessing a Database Which method of ADO is used to move the first record in a recordset? The MoveFirst method of ADO is used to move to the first record in a recordset 45 THIS PAGE INTENTIONALLY LEFT BLANK ... 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... /Labs/Lab08/Starter 41 42 Module 8: Accessing a Database Exercise Accessing data from a database In this exercise, you will create an ASP file to access data from a database ! Access... 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

Ngày đăng: 21/12/2013, 19:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan