Module 15 (Optional): Interoperating Between Managed and Unmanaged Code |
55 |
|
|
|
Review
Topic Objective
To reinforce module objectives by reviewing key points.
Lead-in
The review questions cover some of the key concepts taught in the module.
!Integration Services
!Platform Invoke
!Calling COM Components from Managed Code
!Calling .NET Objects from COM Objects
*****************************ILLEGAL FOR NON-TRAINER USE******************************
1.List the steps for calling an API function that is implemented in a DLL.
Declare the API function with the static and extern C# keywords, attach the DllImport attribute to the function specifying the name of the DLL that exports the unmanaged function, and optionally specify marshaling information.
2.What is pinning?
Pinning is a technique in which data, in its current memory location, is temporarily locked to keep it from being relocated by the common language runtime’s garbage collector.
3.List the tasks that are performed when the Type Library Importer is run on a type library file.
The Type Library Importer converts unmanaged COM coclasses to C# classes with a constructor (that does not have parameters) and no other methods, converts unmanaged COM vtable interfaces to C# interfaces, and converts unmanaged COM structures to C# structures with public fields.
4.Which attribute must you use to suppress runtime security checks that are preformed when managed code calls unmanaged code?
SuppressUnmanagedCodeSecurityAttribute
56Module 15 (Optional): Interoperating Between Managed and Unmanaged Code
5.List some best practices when using .NET code from COM clients.
Define an explicit interface for COM clients to use rather than generating the class interface, and avoid caching dispatch identifiers (DISPIDs).
6.Which must you use to prevent the class interface from being generated?
Set the Value property of the ClassInterfaceAttribute to ClassInterfaceType.None
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
Presentation:
120 Minutes
Lab:
60 Minutes
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.
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.
viModule 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
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.
!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.
2Module 16 (Optional): Using Microsoft ADO.NET to Access Data
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.
!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.