Module 16 (Optional):

64 391 0
Tài liệu đã được kiểm tra trùng lặp
Module 16 (Optional):

Đ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

Contents Overview 1 Overview of ADO.NET 2 Connecting to a Data Source 10 Accessing Data with DataSets 12 Using Stored Procedures 26 Lab 16: Using ADO.NET to Access Data 34 Accessing Data with DataReaders 42 Binding to XML Data 50 Review 56 Module 16 (Optional): Using Microsoft ADO.NET to Access Data 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, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and Window NT 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. Module 16 (Optional): Using Microsoft ADO.NET to Access Data iii Instructor Notes This module focuses on using ADO.NET to access data from various data sources. After completing this module, students 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 DataGrid controls. ! Use stored procedures to read data from a data source. ! Read data from an XML file into DataSets. 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 Microsoft ® PowerPoint ® file 2349B_16.ppt. Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module. ! Complete all of the demonstrations. ! Complete the lab. ! Go through the animation. Presentation: 120 Minutes Lab: 60 Minutes iv Module 16 (Optional): Using Microsoft ADO.NET to Access Data 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 Start animation There are two ways to access data from a database by using ADO.NET: by using a DataSet or by using a DataReader. This animation demonstrates how these two methods work and highlights their differences. Click Start Click DataSet The DataSet method is a disconnected way to access data from a database. In this method, when a user requests data from a database, the DataAdapter object is used to create a DataSet, which is basically a collection of data tables from the database that also retains the relationships between these tables. Notice that, after a DataSet is populated, it is disconnected from the database. 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 list-bound controls, DataGrid, Repeater, or DataList, to display data. The data in the list-bound control is then displayed on the client. An important point to make here is that the use of a DataView to display data is necessary only in ASP.NET Beta 1. From the Beta 2 version onward, you can directly bind the DataSet to a list-bound control. Click DataReader This method is similar to the Microsoft ActiveX ® Data Objects (ADO) way of accessing data by using recordsets. In this method, when a user requests data from a database, the Command object retrieves the data into a DataReader. A DataReader is a read-only/forward-only view of the data. A DataReader works similarly to a Recordset in ADO, allowing you to simply loop through the records. Like the ADO Recordset, the DataReader is connected to the database. You must explicitly close the connection when you are finished reading data. Module 16 (Optional): Using Microsoft ADO.NET to Access Data v 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 a description of the objects used when connecting to a database both with a DataReader and a DataSet. Point out to students that there are Microsoft SQL Server ™ and ADO versions of many of these objects. After describing the process of accessing data through a DataReader and a DataSet, show the animation. Because students may be familiar with ADO, this may be an ideal time to 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 point onward, students will actually start working with ADO.NET. Tell them that all of the examples in this module use SqlConnection objects rather than OleDbConnection objects. Direct the students to the Microsoft .NET Framework software development kit (SDK) documentation for more information. ! Accessing Data with DataSets ADO.NET provides two ways to access data, the DataSet and the DataReader. This section focuses on accessing data by using the DataSet. The DataSet represents a new concept, so spend additional time on this section. The demonstrations actually show every aspect of data access with ADO.NET. Go through the demonstrations carefully, and make sure that the students understand the details. ! Using Stored Procedures Most students who have worked with a SQL Server database and ADO will have experience with using stored procedures. This section provides the students with information about how to use stored procedures and parameterized stored procedures with ADO.NET. ! Lab16: Using ADO.NET to Access Data The lab for this module is encountered in the middle of the module. This is because the module is long and also because the lab does not use material from the last two sections in the module. vi Module 16 (Optional): Using Microsoft ADO.NET to Access Data ! Accessing Data with DataReaders This section focuses on accessing data by using a DataReader. Point out to the students that, when they use a DataReader, the database connection is always open. When they are finished reading data, they must 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 by 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 16 (Optional): Using Microsoft ADO.NET to Access Data 1 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 ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** ADO.NET, offers a rich suite of data handling and data binding functions for manipulating all types of data. ADO.NET is an evolution of the ADO data access model that directly addresses user requirements for developing scalable applications. It was designed specifically for the Web with scalability, statelessness, and XML in mind. 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 DataGrid controls. ! Use stored procedures. ! Read data from an XML file into DataSets. 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 16 (Optional): Using Microsoft ADO.NET to Access Data " "" " Overview of ADO.NET ! The ADO.NET Object Model ! RecordSets vs. DataSets ! Using Namespaces ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** ADO.NET is not a revision of Microsoft ® ActiveX ® Data Objects (ADO), but a new way to manipulate data that is based on disconnected data and XML. Although ADO is an important data access tool it is connected by default, relies on an OLE DB provider to access data, and it is entirely Component Object Model (COM)-based. ADO.NET has been designed to work with disconnected datasets. Disconnected datasets reduce network traffic. ADO.NET uses 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 uses 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. Topic Objective To introduce the topics included in this section. Lead-in ASP.NET offers a new means to retrieve data with the introduction of ADO.NET. Module 16 (Optional): Using Microsoft ADO.NET to Access Data 3 The ADO.NET Object Model DataAdapter Connection Database Database Command .ASPX Page List-Bound Control List-Bound Control DataReader Company: Northwind Traders Company: Northwind Traders .ASPX Page DataView DataView DataSet ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** ADO.NET evolved from the ADO data access model. By using ADO.NET, you can develop applications that are robust and scalable, and that 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 the Dataset, DataReader, and DataAdapter. Connection Objects Connection objects are used to talk to databases. They have properties, such as DataSource, UserID, and Password, which are needed to access a particular DataSource. Commands travel over connections, and result sets are returned in the form of streams that can be read by DataReaders or pushed into DataSet objects. There are two kinds of connection objects in ADO.NET: SqlConnection and OleDbConnection. 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. In ADO.NET, you can use two kinds of command objects: SqlCommand and OleDbCommand. Topic Objective To describe the ADO.NET object model. Lead-in ADO.NET includes some of the same objects as ADO (like Connection and Command ), and introduces new objects, such as DataSets , DataReaders and DataAdapters . Delivery Tip Point out that there are ADO and SQL Server versions of the Connection , Command , DataAdapter , and DataReader objects. 4 Module 16 (Optional): Using Microsoft ADO.NET to Access Data DataReader Objects A DataReader is somewhat synonymous with a read-only/forward-only view of the data. The DataReader API supports flat as well as hierarchical data. A DataReader object is returned after executing a command against a database. It works similarly to a recordset in ADO; however the format of the returned DataReader object is different from a recordset. For example, you may use the DataReader to show the results of a search list in a Web page. ADO.NET includes two types of DataReader objects: the SqlDataReader for Microsoft SQL Server ™ version 7.0 (or later) data, and the OleDbDataReader for ADO data. The DataReader object is database-specific. The behavior of the SqlDataReader may differ from the behavior of the OleDbDataReader and additional DataReader objects that are introduced in the future. You use the OleDbCommand and SqlCommand objects and the ExecuteReader method to transfer data into a DataReader. DataSet Objects The DataSet object is similar to the ADO Recordset object, but more powerful, and with one other important distinction: the DataSet is always disconnected . The DataSet provides a rich object model to work with when passing data between various components of an enterprise solution. The DataSet object represents a cache of data, with database-like behavior. It contains tables, columns, relationships, constraints, and data. Data coming from a database, an XML file, code, or user input can be entered into DataSet objects and converted into files, forms, or databases. The behavior of a DataSet is completely consistent regardless of the underlying database, SQL Server or OLE DB. As changes are made to the DataSet, they are tracked in a way similar to the way changes are tracked in a word processing document. The DataSet object has a collection of DataTable objects. A DataTable represents one table of in-memory data. It contains a collection of columns that represents the table's schema. A DataTable also contains a collection of rows, representing the data contained in the table. You use the OleDbDataAdapter and SqlDataAdapter objects and the Fill method to get data into a DataSet. DataView Objects A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. By using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. A DataView provides a dynamic view of data whose content, ordering, and membership reflect changes to the underlying DataTable as they occur. This is different from the Select method of the DataTable, which returns a DataRow array from a table per a particular filter and/or sort order, and whose content reflects changes to the underlying table, but whose membership and ordering remain static. The dynamic capabilities of the DataView make it ideal for data- binding applications. [...]... describe the animation, see the Multimedia Presentation section in the Instructor Notes for this module In this animation, you will learn how to access data by using ADO.NET and how you can display that data in an ASP.NET page To view the animation, open the file 2349B_16A001.htm file from the Media folder Module 16 (Optional): Using Microsoft ADO.NET to Access Data 7 Recordsets vs DataSets Topic Objective... strongly typed DataSet by using the XSD.exe tool that is provided with the NET Framework SDK The use of this tool is outside the scope of this module You will see how to easily create and use a typed DataSet using Visual Studio NET in this module s lab Module 16 (Optional): Using Microsoft ADO.NET to Access Data 15 Storing Multiple Tables in a DataSet Topic Objective To explain how to retrieve and store... Data with DataSets project from the following location: \Democode\Mod16\Demo16.1 The code reads in data from database tables, creates views, and updates the database Tip You can examine the database tables and stored procedures by using the Server Explorer window Data Connections entries 22 Module 16 (Optional): Using Microsoft ADO.NET to Access Data Running this program produces output... project from the following location: \Democode\Mod16\Demo16.2 Running this program displays a button labeled “Load Data” and two DataGrid controls Clicking the button reads region and customer data from a database and displays them in the grids A DataView object is used to display only those customers in Argentina 26 Module 16 (Optional): Using Microsoft ADO.NET to Access Data " Using Stored... the DataAdapter object to a new command string strSql = "select * from orders"; mySqlDataAdapter.SelectCommand.CommandText = strSql; 3 Call Fill again mySqlDataAdapter.Fill(myDataSet,"Orders"); 16 Module 16 (Optional): Using Microsoft ADO.NET to Access Data The following code shows how you can add two tables from two different queries, one for authors and the other for titles, to the same DataSet //... data store objects System.Data.SqlClient SQL Server specific implementations of ADO.NET objects System.Data.Common Classes shared by the NET data providers System.Data.SqlTypes SQL data types 10 Module 16 (Optional): Using Microsoft 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... and should only be used on platforms, such as Microsoft Windows® 9x, that do not support Integrated Security For more information about Integrated Security, see the NET Framework SDK documentation Module 16 (Optional): Using Microsoft ADO.NET to Access Data 11 Using OleDbConnection For the OLE DB Managed Provider, the connection string format is quite similar to the connection string format used in OLE... for ODBC (MSDASQL) is disabled For access to Open Database Connectivity (ODBC) data sources, an ODBC NET Data Provider is available as a separate download at http://msdn.microsoft.com/downloads 12 Module 16 (Optional): Using Microsoft ADO.NET to Access Data " Accessing Data with DataSets Topic Objective To introduce the topics included in the section Using DataSets to Read Data ! Storing Multiple Tables... two ways to access data, DataSets and DataReaders In this section, you will learn how to access data by using DataSets You will also learn about DataViews and displaying data in DataGrid controls Module 16 (Optional): Using Microsoft 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 Lead-in Now that we can establish... identifies the DataTable that will be created inside the DataSet A DataSet can contain many DataTables You use the string supplied to the Fill method to reference the DataTable after it is created 14 Module 16 (Optional): Using Microsoft ADO.NET to Access Data The following code example illustrates how to create a SqlDataAdapter object that contains the query statement The Fill method then populates the . reading data. Module 16 (Optional): Using Microsoft ADO.NET to Access Data v Module Strategy Use the following strategy to present this module: ! Overview. 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 16 (Optional): Using

Ngày đăng: 22/10/2013, 16:15

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

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

Tài liệu liên quan