Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 34 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
34
Dung lượng
909,29 KB
Nội dung
Contents Overview 1 Lesson: BuildingandConsumingaWebServiceThat Returns Data 2 Review 15 Lab 7.1: Troubleshooting an ADO.NET Application 16 Course Evaluation 25 Module7:BuildingandConsumingaWebServiceThatUsesADO.NET Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, places or events is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. 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. 2001-2002 Microsoft Corporation. All rights reserved. Microsoft, MS-DOS, Windows, Windows NT, Win32, Active Directory, ActiveX, BizTalk, IntelliSense, JScript, MSDN, SQL Server, Visual Basic, Visual C#, Visual C++, Visual J#, Visual Studio, and Windows Media are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Module7:BuildingandConsumingaWebServiceThatUsesADO.NET iii Instructor Notes This module teaches students how to build a simple Webservicethat queries a database. It also explains how to consume the Webservice in a client application. After completing this module, students will be able to: ! Build aWeb service. ! Consume aWebservice in a client application. ! Troubleshoot errors in a Microsoft ® ADO.NET application. The lab for this module is optional. To teach this module, you need the following materials: ! Microsoft PowerPoint ® file 2389B_07.ppt ! Module 7, “Building andConsumingaWebServiceThatUses ADO.NET” ! Lab 7.1, “Debugging a Windows Application and an XML WebServiceThatUses ADO.NET” To prepare for this module: ! Read all of the materials for this module. ! Complete the practices and labs. ! Read the latest .NET Development news at http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contenti d=28000519 This module contains code examples that are linked to text hyperlinks at the bottom of PowerPoint slides. These examples enable you to teach from code examples that are too long to display on a slide. All the code examples for this module are contained in one .htm file. Students can copy the code directly from the browser or from the source, and paste it into a development environment. To display a code sample, click a text hyperlink on a slide. Each link opens an instance of Microsoft Internet Explorer and displays the code associated with the link. At the end of each example is a link that displays a table of contents of all examples in this module. After you have finished teaching the code for an example, close the instance of the browser to conserve resources. If time constraints occur, one or more practices in this module can be presented as instructor-led demonstrations. Presentation: 30 Minutes Lab (optional): 120 Minutes Note Required materials Preparation tasks Hyperlinked code examples Practices iv Module7:BuildingandConsumingaWebServiceThatUsesADO.NET Use Microsoft SQL Server ™ Query Analyzer to run the SQL script named setuplab7.sql in the Lab07 folder on the Instructor computer. This will create a copy of the Employees table named EmployeesLatest. It will also create a new login called MaryJane with a password of secret. Do NOT run this script on the student computers. Remind the students that they must use SQL Server Query Analyzer to run the SQL script named lab6setup.sql in the Lab06_1 folder on the student computers. This will ensure that all of the correct stored procedures for this lab have been created. The application is not designed to be a complete production quality app. The design of the app assumes that if the user adds a new customer they will have to add an order for that customer too. If they do not, there is no way to cause that customer to appear in the initial Customers grid on refresh because the select statement for that grid is based on EmployeeID. Without Order information associated with the added Customer there is no EmployeeID associated to select on. Classroom lab setup Note Module7:BuildingandConsumingaWebServiceThatUsesADO.NET v How to Teach This Module This section contains information that will help you to teach this module. Lesson: BuildingandConsumingaWebServiceThat Returns Data This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: This lesson describes aWebservice as an application component. You should stress thatWeb services accept XML as input and generate XML as output. Keep this discussion at a high level. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are some other examples of Web services? Passport, Microsoft .NET My Services ! How could Web services be used in your organization? Give examples. Technical Notes: You might want to build a simple ASP.NET Webservice by creating a new Webservice in Microsoft Visual Studio ® .NET and clearing the comment from the “Hello World” public function. Then, view the Service1.asmx page in Internet Explorer and test the function. Do not spend more than 5 minutes on this demonstration. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are some other ways that you could fill the local DataSet with data other than by using a DataAdapter? ! What is the purpose of creating an empty, local instance of a DataSet? ! Why use a strongly typed DataSet? Transition to Practice Exercise: Now that you have seen examples of creating aWeb service, you can practice creating aWebservice programmatically. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: The solution for the WebService is located in \Program Files\Msdntrain\2389\Practices\Mod07\Lesson1\ClientRosterService. Technical Notes: Stress that when you create aWeb reference, you are importing method and class definitions from the Webservice into your local project. If the Webservice returns data as a DataSet rather than an XML stream, you should use the XML Schema Definition (XSD) file defined in the Webservice to build a container for the returned data in the client application. To call aWeb method, you must create a variable that points to the Web service. You can then use the Web methods as if they were local to your project. What Is aWeb Service? How to Build aWebServiceThat Returns Database Information How to Consume aWebService vi Module7:BuildingandConsumingaWebServiceThatUsesADO.NET Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are some ways to find out the reference to Web services? ! How do you use Universal Description, Discover, and Integration (UDDI)? Transition to Practice Exercise: Now that you have seen examples of consumingaWeb service, you can practice consumingaWebservice in a client application. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: The solution for the client application is located in \Program Files\Msdntrain\2389\Practices\Mod07\Lesson2\ClientList. Module7:BuildingandConsumingaWebServiceThatUsesADO.NET vii Review: BuildingandConsumingaWebServiceThatUsesADO.NET This section provides the answers to review questions at the end of this module. 1. You are buildingaWebService to return database information to a client application. How should you pass back query results to the client application? You should define a typed DataSet in the Web service. Use a database connection and execute a query to populate an instance of the dataset. Return the DataSet to the client. Alternatively, you could build an XML document that contains the query results in hierarchical format. Then, pass the results to the client as a stream. In this case, the client application could load a DataSet from this stream, inferring or reading schema information as needed. 2. You are buildinga client application that calls aWeb service. What must you do before calling aWeb method to return a DataSet? You must add aWeb reference to the Visual Studio .Net project. This allows you to create a local instance of the typed DataSet defined in the Webservice to capture results. This also allows you to call the Webservice methods as if they were local to your application. Module7:BuildingandConsumingaWebServiceThatUsesADO.NET 1 Overview ! BuildingandConsumingaWebServiceThat Returns Data ! Lab 7.1: Troubleshooting an ADO.NET Application ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Web services allow applications to communicate regardless of operating system or programming language. Web services can be implemented on any platform and are defined through public standards organizations. Sharing data through Web services allows the Web services to be independent of each other while simultaneously enabling them to loosely link themselves into a collaborating group that performs a particular task. In this module, you will learn how to create aWebservicethat returns data. After completing this module, you will be able to: ! Build aWeb service. ! Consume aWebservice in a client application. ! Troubleshoot errors in a Microsoft® ADO.NET application. Introduction Objectives 2 Module7:BuildingandConsumingaWebServiceThatUsesADO.NET Lesson: BuildingandConsumingaWebServiceThat Returns Data ! What Is aWeb Service? ! How to Build aWebServiceThat Returns Database Information ! How to Consume aWebService ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Web services are making possible a new era of distributed application development. By using ADO.NET, you can build Web services that return data, and these Web services can be consumed by multiple applications locally or across the Internet. After completing this lesson, you will be able to: ! Explain what aWebservice is. ! Build aWebservicethat returns data. ! Consume aWeb service. Introduction Lesson objectives [...]... the Webservice Finally, you call aWeb method to return data to the client and populate the DataSet Module 7:BuildingandConsumingaWebServiceThatUsesADO.NET 11 You can use Universal Description, Discovery, and Integration (UDDI) to find out what methods are available from the Webservice When you find the Webservicethat you want to access, you need to add aWeb reference to thatWeb service. .. XML-based response message for use in calculating the total charge to the customer 4 Module7:BuildingandConsumingaWebServiceThatUsesADO.NET How to Build aWebServiceThat Returns Database Information ! Web services that return database information typically: " " " " " Establish a connection to a data source Define the structure of a Typed DataSet (by using an xsd file) Create an empty instance... schema of the DataSet returned by the Webservice 5 In the Class View, expand ClientList and then expand localhost Notice thata class named CustDS has been created In the next procedure, you will create a strongly typed DataSet based on this class 14 Module7:BuildingandConsumingaWebServiceThatUsesADO.NET ! Use the methods and classes from aWebservice 1 From the Data tab of the toolbox, add... described above in the procedure Test your application Module 7:BuildingandConsumingaWebServiceThatUsesADO.NET 15 Review ! BuildingandConsumingaWebServiceThat Returns Data ! Lab 7.1: Troubleshooting an ADO.NET Application *****************************ILLEGAL FOR NON-TRAINER USE****************************** 1 You are building a WebService to return database information to a client application... you create parameterized queries by using the SqlDataAdapter object, use named arguments to mark parameters When you create parameterized queries by using the OleDbDataAdapter object, use the “?” character to mark parameters 'Example of a WebService that returns a DataSet Imports System .Web. Services Public Class Service1 Inherits System .Web. Services.WebService 'This method accepts a city name as a query... DataSet ! Return the DataSet to the client application for further processing Module 7:BuildingandConsumingaWebServiceThatUsesADO.NET Example The following example defines aWeb method that takes a customer’s city as input, queries the Customers table in the Northwind database, and returns a DataSet with information about all of the customers in that city 'Connect to the Northwind DataBase... that is accessible by using standard Web protocols such as HTTP and XML AWebservice can be used locally by a single application, or published on the Internet for use by many different applications Web services allow applications to share data and functionality, and use XML-based messaging to communicate between systems that use different component models, operating systems, and programming languages... operating system and in any language can access these services The decentralized nature of Web services enables both the client and the Webservice to function as autonomous units Therefore, there are countless ways to consume aWebservice For example, you can include a call to a Webservice in aWeb application, a middleware component, or even another Webservice No matter what type of Webservice client.. .Module 7:BuildingandConsumingaWebServiceThatUsesADO.NET 3 What Is aWeb Service? ! Programmable logic accessible through standard Web protocols XML WebService TP HT WebService XML WebService L XM XML WebWebServiceService NET My Services Client *****************************ILLEGAL FOR NON-TRAINER USE****************************** Definition A Webservice is a unit of programmable... Webservicethat returns data To build aWebservicethat returns data, you first create a new Webservice by using Visual Studio NET Typically, this WebService will: ! Establish a connection to a data source ! Define the structure of a typed DataSet (by using an xsd file) ! Create an empty instance of the typed DataSet ! Run a query or perform calculations, and fill the DataSet A DataAdapter is commonly . Building and Consuming a Web Service That Uses ADO. NET How to Build a Web Service That Returns Database Information ! Web services that return database information. Consuming a Web Service That Uses ADO. NET ! Lab 7.1, “Debugging a Windows Application and an XML Web Service That Uses ADO. NET To prepare for this module: