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

Module 3: Using ADO.NET to Access Data

56 459 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

Định dạng
Số trang 56
Dung lượng 1,02 MB

Nội dung

Contents Overview 1 Overview of ADO.NET 2 Connecting to a Data Source 10 Accessing Data With DataSets 12 Using Stored Procedures 28 Accessing Data with DataReaders 37 Binding to XML Data 44 Lab 3: Using ADO.NET to Access Data 49 Review 50 Module 3: Using ADO.NET to Access Data BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY 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, BackOffice, MS-DOS, Windows, Windows NT, <plus other appropriate product names or titles. The publications specialist replaces this example list with the list of trademarks provided by the copy editor. Microsoft is listed first, followed by all other Microsoft trademarks in alphabetical order. > are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. <The publications specialist inserts mention of specific, contractually obligated to, third-party trademarks, provided by the copy editor> Other product and company names mentioned herein may be the trademarks of their respective owners. Module 3: Using ADO.NET to Access Data iii BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Instructor Notes One of the reasons for the widespread use of ASP was its easy access to data stores. ASP.NET extends this feature with the introduction of ADO.NET. This module focuses on using ADO.NET for accessing data from various data sources. Students also learn how to display data and customize the display of data. In the lab, students will connect to a database, fill a DataSet, and customize the data display by using a Repeater control. After completing this module, students will be able to: ! Describe the ADO.NET object model. ! Connect to a data source using ADO.NET. ! Retrieve data from a database using DataReaders, and DataSets. ! Display the data from a database on the client using list-bound controls. ! Customize the look of a Repeater control with templates. ! Use stored procedures to return RecordSets. ! Read data from an XML file into a DataSet. Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module. Required Materials To teach this module, you need the following materials: ! Microsoft ® PowerPoint ® file 2063A_03.ppt ! Module 3, “Using ADO.NET to Access Data” (2063A_03.doc) ! Lab 3, “Using ADO.NET to Access Data” (2063A_L03.doc) ! Animation: “Using ADO.NET to Access Data” (2063A_03A001.swf) Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module. ! Complete all the demonstrations. ! Complete the lab. ! Go through the animation. ! Study material on the differences between ADO and ADO.NET. Presentation: 180 Minutes Lab: 30 Minutes iv Module 3: Using ADO.NET to Access Data BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Multimedia Presentation This section provides multimedia presentation procedures that do not fit in the margin notes or are not appropriate for the student notes. Using ADO.NET to Access Data ! To present the animation Action Say this Before starting the animation, say this: There are two ways of accessing data from a database using ADO.NET; using DataSets or Using DataReaders. This animation demonstrates how these methods work, and highlights the differences between the two methods. Click DataSet The DataSet method is a disconnected way of accessing data from a database. In this method, when a user requests for data from a database, the DataSetCommand object is used to create a DataSet, which basically is a collection of DataTables from the database and also retains the relationships between these tables. Notice that once a DataSet is populated, it is disconnected from the database. In order to display the data from the DataSet, you set up a DataView for the desired table. The DataView is then bound to a list-bound control for displaying purposes. You can use any of the three listbound controls, DataGrid, Repeater, or DataList in order to display data. The data in the listbound control is then displayed on the client. An important point here is that the use of DataView to display data is valid only in the Beta 1 and older versions of ASP.NET. From the Beta 2 version onwards, you can directly bind your DataSet to a listbound control. Click DataReader This method is similar to the ADO way of accessing data using recordsets. In this method, when a user requests for data from a database, the Command object retrieves the data into a DataReader. A DataReader is a read-only/forward-only view on the data A DataReader works similarly to a recordset in ADO, allowing you to simply loop through the records. Like ADO recordsets, DataReaders are connected to the database. You need to explicitly close the connection when you are finished reading data. Module 3: Using ADO.NET to Access Data v BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Module Strategy Use the following strategy to present this module: ! Overview of ADO.NET This section provides students with an overview of ADO.NET. The section begins with an animation on using ADO. After the animation has been viewed, describe each of the objects shown in the model. Since students are familiar with ADO, this is a good place discuss some of the main differences between ADO and ADO.NET. When talking about using namespaces, explain their significance to the students. ! Connecting to a Data Source From this section onwards, students will actually start working with ADO.NET. Tell them that all the examples in this module use SQLConnection objects rather than ADOconnection objects. Mention that using ADOConnection objects is very similar, and point them to the .NET framework SDK. In some instances, you could demonstrate how the same example would look using ADOConnection objects. ! Accessing Data with DataSets ADO.NET provides two ways of accessing data, using DataSets and DataReaders. This section focuses on accessing data using DataSets. DataSets represent a new concept, so spend additional time on this section. The demonstration actually shows every aspect of data access with ADO.NET. Go through the demonstration carefully, and make sure that the students understand the details. There is also an exercise later in this section that enables students to practice these concepts. When talking about using templates with a Repeater control, do not spend time delving into the details of these controls. Point students to the .NET framework SDK for more information about these controls. ! Using Stored Procedures Most students, who have worked with the SQL database and ADO, will have experience with using stored procedures. This section provides them information on how to use stored procedures and parameterized stored procedures with ADO.NET. ! Accessing Data with DataReaders This section focuses on accessing data using DataReaders. Make sure that you point out to the students that when using DataReaders, the database connection is always open. When they are finished reading data, they need to explicitly close the connection. ! Binding to XML Data XML is fast emerging as the most popular language for exchanging data. This section provides students with information on how to read XML data using ADO.NET. Most students will already know about XML documents. However, for students who are not familiar with XML, it will be useful to show an example of an XML document and how it is displayed on the client. Module 3: Using ADO.NET to Access Data 1 BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Overview ! Overview of ADO.NET ! Connecting to a Data Source ! Accessing Data with DataSets ! Using Stored Procedures ! Accessing Data with DataReaders ! Binding to XML Data One of the reasons for the widespread use of ActiveX ® Server Pages (ASP) was that it facilitated access to data stores. ASP.NET extends this feature with the introduction of ADO.NET, which offers a rich suite of data handling and data binding functions for manipulating all types of data. After completing this module, you will be able to: ! Describe the ADO.NET object model. ! Connect to a data source by using ADO.NET. ! Retrieve data from a database by using DataReaders, and DataSets. ! Display the data from a database on the client by using list-bound controls. ! Customize the look of a Repeater control with templates. ! Use stored procedures to return recordsets. ! Read data from an XML file into a DataSet. Topic Objective To provide an overview of the module topics and objectives. Lead-in In this module, you will learn about the data binding features in ASP.NET. 2 Module 3: Using ADO.NET to Access Data BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY # Overview of ADO.NET ! Animation: Using ADO.NET to Access Data ! The ADO.NET Object Model ! RecordSets vs. DataSets ! Using Namespaces ADO.NET is not a revision of ADO, but a new way to manipulate data that is based on disconnected data and XML. Although ADO is an important data access tool with ASP, it does not provide all the necessary features for developing robust and scalable Web applications. Despite ADO's rich object model and relatively ease of use, it is connected by default, relies on an OLE DB provider to access data, and is entirely COM based. ADO.NET has been designed to work with disconnected data sets. Disconnected sets of records are useful because they are local views of the data that are faster to process and transmit, and they also reduce network traffic. ADO.NET utilizes XML as the universal transmission format. This guarantees interoperability as long as the receiving component runs on a platform where an XML parser is available. When the transmission occurs through XML, it is no longer necessary that the receiver be a COM object. The receiving component has no architectural restrictions whatsoever. Any software component can share ADO.NET data, as long as it agrees to use the same XML schema for the format of the transmitted data. In this section, you will learn about ADO.NET. You will learn about the new and modified objects in ADO.NET. You will also learn about some of the new namespaces that are included in ASP.NET. Topic Objective To introduce the topics included in this section. Lead-in ASP.NET offers a new means to retrieve the data with the introduction of ADO.NET. Module 3: Using ADO.NET to Access Data 3 BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Animation: Using ADO.NET to Access Data In this animation, you will learn how to access data using ADO.NET and how you can display that data in an ASP.NET page. To view the animation, open the 2063A_03A001.swf file from the Media folder. Topic Objective To provide a high-level overview of how to access data by using ADO.NET. Lead-in In this animation, you will learn about how ADO.NET accesses data and how you can display that data in an ASP.NET page. Delivery Tip Run the Macromedia Flash animation Using ADO.NET to Access Data. (2063A_03A001.swf) For details on how to run and describe the animation, see the Multimedia Presentation section in the Instructor Notes for this module. 4 Module 3: Using ADO.NET to Access Data BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY The ADO.NET Object Model DataSetCommand Connection Database Database Command .ASPX Page List-Bound Control List-Bound Control DataReader Company: Northwind Traders Company: Northwind Traders .ASPX Page DataView DataView DataSet ADO.NET evolved from the ADO data access model. ADO.NET allows you to develop applications that are robust and scalable, and can use XML. ADO.NET has some of the same objects as ADO (like the Connection and Command objects), and introduces new objects such as DataSets, DataReaders, and DatasetCommands. Some of objects in ADO.NET represent updates of existing objects (such as Connection and Command), while others are completely new (such as DataReader, DataSet, DataView, and DataSetCommand. Connection Objects Connection objects are used to talk to databases. They have properties such as dataSource, userID, and password that are needed to access a particular datasource. Commands travel over connections and result sets are returned in the form of streams, which can be read by DataReaders, or pushed into DataSet objects. Command Objects Command objects contain the information that is submitted to a database. A command can be a stored procedure call, an update statement, or a statement that returns results. You can also use input and output parameters and return values. DataReader Objects The DataReader is a read-only/forward-only view on the data. DataReaders provide a simple and lightweight way of traversing through record sets. For example, if you wanted to simply show the results of a search list in a web page, using a DataReader is an ideal way to accomplish this. Topic Objective To describe the ADO.NET object model. Lead-in ADO.NET has some of the same objects as ADO (like Connection and Command), and introduces new objects such as DataSets, DataReaders and DatasetCommands. [...]... PURPOSES ONLY Module 3: Using ADO.NET to Access Data 13 Using DataSets to Read Data Topic Objective To describe how to retrieve data from a database by using DataSets ! Create the Database Connection Lead-in ! Store the Query in a DataSetCommand Now that we can establish a connection, we need to be able to execute statements against the database to retrieve data Dim cmdAuthors As SQLDataSetCommand... Module 3: Using ADO.NET to Access Data Using DataViews Topic Objective To explain the role of DataViews in accessing data from a database by using DataSets Lead-in To access data from a DataSet, you need DataViews ! DataViews can be customized to present a subset of data from a DataTable ! The DefaultView property returns the default DataView for the table Dim dv as DataView Dim dv as DataView dv = ds.Tables("Authors").DefaultView... PURPOSES ONLY 10 Module 3: Using ADO.NET to Access Data Connecting to a Data Source Topic Objective To describe how to connect to a data source by using ADO.NET Lead-in Connecting to a data source is the first step in data access ! Using SQLConnection Dim strConn As String = _ Dim strConn As String = _ "server=localhost; uid=sa;pwd=; database=northwind" "server=localhost; uid=sa;pwd=; database=northwind"... updating data in a database is to use stored procedures You can use stored procedures to read and modify data from a database You can call stored procedures both from DataSetCommand and Command objects BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Module 3: Using ADO.NET to Access Data 29 Calling Stored Procedures Topic Objective To explain how to use stored procedures to retrieve... After connecting to a database, the next step is to access data from it ADO.NET provides two ways of accessing data, using DataSets and DataReaders In this section, you will learn how to access data by using DataSets You will also learn about using DataViews and displaying data in list-bound controls At the end of the section, you will learn how to customize a list-bound control by using templates BETA... ONLY 18 Module 3: Using ADO.NET to Access Data Example using a custom view Dim dv as DataView dv = New DataView (ds.Tables("Authors")) dv.RowFilter = "state = 'CA'" dgAuthors.DataSource = dv dgAuthors.DataBind() The following illustration shows the default format of the DataGrid control BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Module 3: Using ADO.NET to Access Data 19... Data # Accessing Data With DataSets Topic Objective To introduce the topics included in the section ! Using DataSets to Read Data Lead-in ! Storing Multiple Tables in a DataSet ! Using DataViews ! Displaying Data in the DataGrid Control ! Demonstration: Displaying Data in a DataGrid ! Practice: Viewing a DataSet ! Using Templates ! Using the Repeater Control ! Demonstration: Displaying Data in a Repeater... PREPARATION PURPOSES ONLY 28 Module 3: Using ADO.NET to Access Data # Using Stored Procedures Topic Objective To introduce the topics in this section Lead-in Another important task when working with data from a data source is to be able to modify it ! Calling Stored Procedures ! Passing Parameters ! Calling Action Stored Procedures ! Demonstration: Calling Stored Procedures In the past, data processing has... ONLY 6 Module 3: Using ADO.NET to Access Data The DataSetCommand object exists in two forms: SQLDataSetCommand objects and ADODataSetCommand objects The data source is Microsoft® SQL Server 7.0 (or later) for SQLDataSetCommand objects and any other OLE DB provider for ADODataSetCommand objects BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY Module 3: Using ADO.NET to Access Data. . .Module 3: Using ADO.NET to Access Data 5 DataReaders are returned after executing a command They work similarly to a recordset in ADO, allowing you to simply loop through the records ADO.NET includes two types of DataReaders: SQLDataReader for SQL data, and ADODataReader for ADO data The DataReader object is database specific The behavior for the SQLDataReader may differ from . Module 3, Using ADO. NET to Access Data (2063A_03.doc) ! Lab 3, Using ADO. NET to Access Data (2063A_L03.doc) ! Animation: Using ADO. NET to Access Data . how to connect to a data source by using ADO. NET. Lead-in Connecting to a data source is the first step in data access. Note Module 3: Using ADO. NET to Access

Ngày đăng: 27/10/2013, 07:15

TỪ KHÓA LIÊN QUAN