Problem
You want to create a strongly typed object wrapper around a DataSet .
Solution
Use one of the three techniques shown in the discussion to create a strongly typed DataSet using either the Visual Studio .NET IDE or a command line approach.
Discussion
A strongly typed DataSet is a collection of classes that inherit from and extend the DataSet , DataTable , and DataRow classes, and provide additional properties, methods , and events based on the DataSet schema. You can use all of the functionality in classes from which the strongly typed classes inherit in the same way as with untyped classes.
A strongly typed DataSet class contains, in addition to a single class extending the DataSet class, three classes for each table in the DataSet extending each of the DataTable , DataRow , and DataRowChangeEvent classes. This recipe describes these classes and discusses their commonly used methods and properties.
There is a class named TableName DataTable for each table in the strongly typed DataSet . It has the base class DataTable . Table 2-1 lists commonly used methods of this class specific to the strongly typed DataSet .
Table 2-1. TableNameDataTable methods
Method |
Description |
---|---|
Add TableName Row( ) |
Adds a row to the table. The method has two overloads: one takes a TableName Row object as the argument, while the other takes a set of arguments containing the column values. |
FindBy PrimaryKeyField1 ... PrimaryKeyFieldN ( ) |
Takes N arguments which are the values of the primary key fields of the row to find. Returns a TableName Row object, if found. |
New TableName Row( ) |
Takes no arguments and returns a new TableName Row object with the same schema as the table to be used for adding new rows to the table in the strongly typed DataSet . |
There is a class named TableName Row for each table in the strongly typed DataSet . It has the base class DataRow and represents a row of data in the table. Table 2-2 lists commonly used properties and methods of this class specific to the strongly typed DataSet .
Table 2-2. TableNameRow class properties and methods
Property/method |
Description |
---|---|
Typed Accessor |
Sets and get the value of a column. The typed accessor is exposed as a property having the same name as the underlying data column. |
Is ColumnName Null( ) |
Returns a Boolean value indicating whether the field contains a null value. |
Set ColumnName Null( ) |
Sets the value of the underlying field to a null value. |
Get ChildTableName Rows( ) |
Returns the rows for the table as an array of ChildTableName Row objects. |
ParentTableName Row( ) |
Returns the parent row as an object of type ParentTableName Row . |
There is a class named TableNameRow ChangeEvent for each table in the strongly typed DataSet . It has the base class EventArgs . Table 2-3 describes the properties of this class.
Table 2-3. TableNameRowChangeEvent properties
Property |
Description |
---|---|
Action |
A value from the System.Data.DataRowAction enumeration that describes the action performed on a row that caused the event to be raised. |
Row |
The TableName Row object for which the event was raised. |
A strongly typed DataSet has some advantages over using an untyped DataSet :
The disadvantages of a strongly typed DataSet object include:
Four ways to generate a typed DataSet class are described in the following subsections.
Using the Visual Studio .NET IDE to generate a typed DataSet
The first and easiest method uses Visual Studio .NET following these steps:
Using the TypedDataSetGenerator class to generate a typed DataSet
The second technique is to derive a class from the TypedDataSetGenerator class. The static Generate( ) method of the TypedDataSetGenerator class is used to create a strongly typed DataSet . The prototype of the method is:
public static void Generate(DataSet dataSet, CodeNamespace codeNamespace, ICodeGenerator codeGenerator);
The arguments of the method are:
DataSet
The DataSet used to specify the schema for the typed DataSet .
codeNamespace
The target namespace for the typed DataSet .
codeGenerator
A class capable of dynamically rendering source code in a specific language and used to create the typed DataSet .
Using an XSD schema file to generate a typed DataSet
The other two methods require an XSD schema file. You can generate this file in a number of ways: using the Visual Studio .NET tools, third-party tools, or the DataSet WriteXmlSchema( ) method. You can create a strongly typed DataSet from the XSD schema file using Visual Studio .NET or using the XML Schema Definition Tool.
To create a strongly typed DataSet from the XSD schema using Visual Studio .NET, follow these steps:
The second way to create a strongly typed DataSet from an XSD schema is to use the XML Schema Definition Tool ( XSD.EXE ) found in the .NET Framework SDK bin directory. Follow these steps:
Generate the strongly typed DataSet class file from the XSD schema file by issuing the following command from the command prompt:
xsd mySchemaFile.xsd /d /l:CS
The /d switch specifies that source code for a strongly typed DataSet should be created.
The /l:CS switch specifies that the utility should use the C# language, which is the default if not specified. For VB.NET, use the switch /l:VB .
The XML Schema Definition Tool offers other options. For more information, see the .NET Framework SDK documentation or the MSDN Library.
The class file that is generated for the strongly typed DataSet is named using the DataSet name in the XSD schema and has an extension corresponding to the programming language: .cs for C# and .vb for VB.NET. The strongly typed DataSet class can now be added to a project.
|
Connecting to Data
Retrieving and Managing Data
Searching and Analyzing Data
Adding and Modifying Data
Copying and Transferring Data
Maintaining Database Integrity
Binding Data to .NET User Interfaces
Working with XML
Optimizing .NET Data Access
Enumerating and Maintaining Database Objects
Appendix A. Converting from C# to VB Syntax