Module 16 (Optional): Using Microsoft ADO.NET to Access Data |
53 |
|
|
|
Reading XML Data into a DataSet
Topic Objective
To explain how to read XML data into a DataSet.
Lead-in
Because XML is an important part of ASP.NET, there are various easy ways to bind to XML data.
! Setup the XML file for reading
FileStream fs = File.OpenRead("myFile.xml"); FileStream fs = File.OpenRead("myFile.xml"); StreamReader myStreamReader = new StreamReader(fs); StreamReader myStreamReader = new StreamReader(fs);
! Read the contents of the File into a DataSet
DataSet myDataSet = new DataSet(); DataSet myDataSet = new DataSet(); myDataSet.ReadXml(myStreamReader); myDataSet.ReadXml(myStreamReader);
! Bind a DataGrid to the DataSet
dataGrid1.DataSource = myDataSet.Tables[0]; dataGrid1.DataSource = myDataSet.Tables[0];
*****************************ILLEGAL FOR NON-TRAINER USE******************************
DataSets in ADO.NET are designed to extract data in a way that is independent of its data source. Therefore, reading data from an XML source is similar to reading data from a database. To read XML data, you import the System.IO namespace in your ASP.NET page.
Note You cannot read XML data into a DataReader. You can read it only into a DataSet.
For XML data, the DataSet supports a ReadXml method that takes a FileStream as its parameter. The DataSet expects data to be in the following format:
<DocumentElement>
<TableName>
<ColumnName1>column value</ColumnName1> <ColumnName2>column value</ColumnName2> <ColumnName3>column value</ColumnName3>
</TableName>
<TableName>
<ColumnName1>column value</ColumnName1> <ColumnName2>column value</ColumnName2> <ColumnName3>column value</ColumnName3>
</TableName>
</DocumentElement>
Each TableName section corresponds to a single row in the table.
54 |
Module 16 (Optional): Using Microsoft ADO.NET to Access Data |
The following example shows how to read the schema and data from an XML file by using the ReadXml method, the FileStream object, and the StreamReader object. Note that after the data is read into the DataSet, it is indistinguishable from SQL data—the DataGrid binds to it in the same way.
You can perform the following steps to read a file named myFile.xml that contains XML data and displays it in a DataGrid.
First open the XML file and create a StreamReader:
FileStream fs = File.OpenRead("myFile.xml"); StreamReader myStreamReader = new StreamReader(fs);
Next, read the XML data into the DataSet:
DataSet myDataSet = new DataSet(); myDataSet.ReadXml(myStreamReader);
Finally, bind a DataGrid to the DataSet:
dataGrid1.DataSource = myDataSet.Tables[0];
After the data has been read into a DataSet, the repeated elements in the XML become the columns in the DataSet and can be bound to any control to be displayed on the client.
The Windows Forms code, including try/catch/finally blocks, that is used in Demonstration: Reading XML Data into a DataSet, is as follows:
StreamReader myStreamReader = null; statusBar1.Text = "";
try
{
FileStream fs = File.OpenRead(textBox1.Text); myStreamReader = new StreamReader(fs);
DataSet myDataSet = new DataSet(); myDataSet.ReadXml(myStreamReader); dataGrid1.DataSource = myDataSet.Tables[0]; statusBar1.Text = "Read file: " + textBox1.Text;
}
catch(Exception exception)
{
statusBar1.Text = exception.ToString();
}
finally
{
if (myStreamReader != null) myStreamReader.Close();
}
Module 16 (Optional): Using Microsoft ADO.NET to Access Data |
55 |
|
|
|
Demonstration: Reading XML Data into a DataSet
Topic Objective
To demonstrate how to display information from an XML file.
Lead-in
In this demonstration, you will see how to read information from an XML file and display it in a DataGrid control.
*****************************ILLEGAL FOR NON-TRAINER USE******************************
In this demonstration, you will see how to read information from an XML file and display it in a DataGrid control.
Run the Visual Studio .NET XMLData project from the following location:
<install folder>\Democode\Mod16\Demo16.5
Enter the default filename books1.xml and click the Load Data button. The file books1.xml is read into a DataSet and displayed in a DataGrid.
56 |
Module 16 (Optional): Using Microsoft ADO.NET to Access Data |
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.
!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******************************
1.What are some of the new objects in the ADO.NET object model?
Some of the new objects in the ADO.NET object model are: DataSet, DataAdapter, DataView, DataReader, and DataTable.
2.What is the difference between a DataSet and a DataView?
DataSet is a collection of DataTables and a DataView is a custom view of the DataTables in a DataSet.
3.What is the difference between a DataSet and a DataReader?
The DataReader is designed to produce a read-only, forward-only stream returned from the database.
The DataSet is designed to handle the actual data from a data store. It represents a cache of data, with database-like behavior. It can contain tables, columns, relationships, constraints, and data.
Module 16 (Optional): Using Microsoft ADO.NET to Access Data |
57 |
|
|
|
4.What is the purpose of the DataAdapter object?
A DataAdapter object is a tool that is used to create and initialize various tables. It allows for the retrieval and saving of data between a DataSet object and the source data store. It is responsible for pulling out data from the physical store and pushing it into data tables and relations.
5.Which method is used to populate a DataSet with results of a query?
The method that is used to populate the DataSet with results of a query is the Fill method.
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 17 (Optional): Attributes |
iii |
|
|
|
Instructor Notes
Presentation:
60 Minutes
Lab:
45 Minutes
Teach this module if time permits. Module 17 is a stand-alone module that is not dependent upon any other module.
This module provides students with the details about how to use attributes in code. It describes the predefined attributes that are provided by the Microsoft®
.NET Framework and provides some simple examples of how to use some common attributes. The concept of custom attributes is introduced. This introduction is followed by a detailed explanation of how to define and use custom attributes. The process used to compile code that has custom attributes is also explained. Finally, the module describes how to retrieve attribute values during run time by using reflection. The procedure that is used to retrieve attribute information into an array and query the array to obtain the required values is explained.
After completing this module, students will be able to:
!Use common predefined attributes.
!Create simple custom attributes.
!Query attribute information at run time.
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 Microsoft PowerPoint® file 2349B_17.ppt.
Preparation Tasks
To prepare for this module, you should:
!Read all of the materials for this module.
!Complete the lab.
!Read the instructor notes and margin notes for the module.
!Practice using the Microsoft Intermediate Language disassembler to examine the metadata of an assembly.
iv |
Module 17 (Optional): Attributes |
Demonstration
This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes.
Use the debugger to step through the code while you point out features and ask students what they think will happen next.
Custom Attributes
In this demonstration, you will show students how to declare and apply custom attributes and how to use reflection to retrieve custom attribute metadata.
The code for this demonstration is contained in one project and is located in <install folder>\Democode\Mod17.
Module Strategy
Use the following strategy to present this module:
!Overview of Attributes
Begin by explaining that attributes are only annotations to classes and perform no function themselves. Before attributes can cause an action, certain code must be implemented. This code is in the runtime for the predefined attributes and is written by the developer for custom attributes. Explain the syntax used to apply an attribute, and, after covering the lists of predefined attributes briefly, discuss the three attributes—Conditional,
DllImport, and Transaction—using examples.
!Defining Custom Attributes
Introduce the need for creating custom attributes, and explain the procedures involved in defining a custom attribute. Explain the use of AttributeUsage and how to create an attribute class. Then explain the details of the procedure used to compile code that uses custom attributes. Finish the section by explaining how to use multiple attributes in code.
!Retrieving Attribute Values
In this section, introduce the concept of retrieving attribute values at run time by using reflection. Explain how to use the MemberInfo class and the typeof operator to obtain attribute values. Finally, discuss how to iterate through the stored attribute values in an array to retrieve the required values. To end the discussion about attributes, use the review slide to recapitulate the main concepts covered in the module.