[ Team LiB ]
Recipe 2.3 CreatingaStronglyTypedDataSet
Problem
You want to create astronglytyped object wrapper around a DataSet.
Solution
Use one of the three techniques shown in the discussion to create astronglytyped
DataSet using either the Visual Studio .NET IDE or a command line approach.
Discussion
A stronglytypedDataSet 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 stronglytyped classes inherit in the same way as with untyped classes.
A strongly typedDataSet 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 TableNameDataTable for each table in the stronglytyped DataSet.
It has the base class DataTable. Table 2-1
lists commonly used methods of this class
specific to the stronglytyped DataSet.
Table 2-1. TableNameDataTable methods
Method Description
AddTableNameRow( )
Adds a row to the table. The method has two
overloads: one takes a TableNameRow object as the
argument, while the other takes a set of arguments
containing the column values.
FindByPrimaryKeyField1
PrimaryKeyFieldN( )
Takes N arguments which are the values of the
primary key fields of the row to find. Returns a
TableNameRow object, if found.
NewTableNameRow( ) Takes no arguments and returns a new
TableNameRow 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 TableNameRow for each table in the stronglytyped 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 stronglytyped
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.
IsColumnNameNull( )
Returns a Boolean value indicating whether the field
contains a null value.
SetColumnNameNull( ) Sets the value of the underlying field to a null value.
GetChildTableNameRows(
)
Returns the rows for the table as an array of
ChildTableNameRow objects.
ParentTableNameRow( )
Returns the parent row as an object of type
ParentTableNameRow.
There is a class named TableNameRowChangeEvent for each table in the stronglytyped
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 TableNameRow object for which the event was raised.
A stronglytypedDataSet has some advantages over using an untyped DataSet:
• The schema information is contained within the stronglytypedDataSet resulting
in a performance over retrieving schema information at runtime. The schema of an
untyped DataSet can also be defined programmatically, as discussed in Recipe 2.1
,
resulting in similar performance.
• Programming is more intuitive and code is easier to maintain. Table, column, and
other object names are accessed through properties having names based on the
underlying data source object names rather than by using index or delimited string
arguments. The Visual Studio .NET IDE provides autocomplete functionality for
strongly typedDataSet names.
• Type mismatch errors and errors resulting from misspelled or out of bounds
arguments used with DataSet objects can be detected during compilation, rather
than at runtime.
The disadvantages of astronglytypedDataSet object include:
• Additional overhead when executing. If stronglytyped functionality is not
required, performance is better with an untyped DataSet rather than with atyped
DataSet.
• AstronglytypedDataSet must be regenerated when the structure of the underlying
data source changes. Applications using these stronglytypedDataSet objects will
need to be rebuilt with a reference to the new stronglytyped DataSet. With an
untyped DataSet, as long as the new schema is a superset of the old schema,
existing clients can simply ignore any new data and do not need to be recompiled.
Four ways to generate atypedDataSet class are described in the following subsections.
Using the Visual Studio .NET IDE to generate atypedDataSet
The first and easiest method uses Visual Studio .NET following these steps:
1. Drop a DataAdapter object from the Data tab in the Visual Studio .NET Toolbox
onto a design surface such as a form or a component. The Data Adapter
Configuration Wizard will appear.
2. Press Next to continue to the Choose Your Data Connection dialog.
3. Select an existing connection or create a new one as required. Press Next to
continue to the Choose a Query Type dialog.
4. Select the Use SQL statements radio button and press Next to continue to the
Generate the SQL Statements dialog.
5. Press the Advanced Options button.
6. Uncheck all three check boxes on the Advanced SQL Generation Options dialog
and press OK.
7. Press the Query Builder button.
8. Select only one table on the Add Table dialog and press Add. Press Close.
9. Check the columns from the table to include or (All Columns) and press OK.
10. Press Next to continue to the View Wizard Results dialog.
11. Press Finish to complete the wizard.
12. Repeat steps 1-11 for the other tables that you want to have included in the
strongly typed DataSet.
13. Right-click on the design surface and select Generate DataSet.
14. Provide a name for the stronglytyped DataSet, select the tables to be included, and
press OK to generate the new stronglytyped DataSet.
15. To relate tables in the new stronglytyped DataSet, open the XSD file for the new
DataSet in the Solution Explorer window.
16. Right-click on the child table in XSD schema designer, select Add New
Relation . . . from the shortcut menu, and complete the dialog. Repeat this step to
create all required relationships.
17. Instances of the strongly typedDataSet can now be created programmatically
either by using the new keyword in C# or the New keyword in Visual Basic .NET
or by dragging the DataSet object from the Data tab in the Visual Studio .NET
Toolbox onto a design surface such as a component or form.
Using the TypedDataSetGenerator class to generate atypedDataSet
The second technique is to derive a class from the TypedDataSetGenerator class. The
static Generate( ) method of the TypedDataSetGenerator class is used to create astrongly
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 atypedDataSet
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 astronglytypedDataSet from the XSD
schema file using Visual Studio .NET or using the XML Schema Definition Tool.
To create astronglytypedDataSet from the XSD schema using Visual Studio .NET,
follow these steps:
1. Right-click on the project in the Solution Explorer window, choose Add/Existing
Item from the shortcut menu, and select the XSD file to add it to the project.
2. Select the XSD schema file from the Solution Explorer window and open it in the
designer window.
3. Right-click on the designer window and select Generate DataSet.
4. Instances of the strongly typedDataSet can now be created programmatically by
using the new keyword in C# or the New keyword in Visual Basic .NET or by
dragging the DataSet object from the Data tab in the Visual Studio .NET Toolbox
onto a design surface such as a component or form.
The second way to create astronglytypedDataSet 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 typedDataSet 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 astronglytypedDataSet 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 stronglytypedDataSet 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 typedDataSet class can now be
added to a project.
If the stronglytypedDataSet file is not visible as a child node of the
XSD Schema in the Solution Explorer window, select Show All Files
from the Project menu.
[ Team LiB ]
. in the
strongly typed DataSet.
There is a class named TableNameRow for each table in the strongly typed DataSet. It
has the base class DataRow and represents. properties.
There is a class named TableNameDataTable for each table in the strongly typed DataSet.
It has the base class DataTable. Table 2-1
lists commonly