[ Team LiB ] Previous Section Next Section

Chapter 13. Strongly Typed DataSets

Strongly typed DataSets are a collection of classes that inherit from the DataSet, DataTable, and DataRow classes, and provide additional properties, methods, and events based on the DataSet schema. These methods, properties, and events allow, among other things, named properties and methods to be used to retrieve column values, access parent and child records, find rows, and handle null column values.

It is important to remember that strongly typed DataSets are in fact just a collection of classes. Because the strongly typed DataSet classes inherit from DataSet, DataTable, and DataRow, all the functionality present in those classes can be used just as it is for the untyped classes. The development environment just provides a way to automatically generate consistent and efficient strongly typed DataSet classes.

Using a strongly typed DataSet has a number of advantages over using untyped DataSet:

  • Schema information is contained within the strongly typed DataSet. This results in an improvement in performance over retrieving schema information at runtime. Of course, the schema of an untyped DataSet can also be defined programmatically.

  • Programming is more intuitive, and code is easier to maintain. Table and field names are accessed through properties rather than indexer arguments. The Visual Studio .NET IDE provides autocomplete functionality for these names. As an example, the following code demonstrates accessing data using both an untyped and strongly typed Northwind DataSet:

    // untyped
    String categoryName =
       (String)dsNorthwind.Tables["Categories"].Rows[0]["CategoryName"];
    
    // strongly typed
    String categoryName = dsNorthwind.Categories[0].CategoryName;
  • Type mismatch errors and errors resulting from either misspelled or out-of-bounds indexer arguments to retrieve tables and columns can be detected during compilation rather than at runtime.

There are some drawbacks to using strongly typed DataSet objects:

  • Using typed classes adds some overhead to code execution. If the strongly typed functionality isn't required, application performance can be improved slightly using an untyped DataSet.

  • The strongly typed DataSet will need to be regenerated when the data structure changes. Applications using the strongly typed DataSet will need to be rebuilt using a reference to the new strongly typed DataSet. This can be especially significant in a multitier application or distributed where any clients that use the strongly typed DataSets will have to be rebuilt using a reference to the updated version, even those that would not otherwise have been affected by the change if an untyped DataSet was used.

This chapter discusses how to create strongly typed DataSets and describes some of their added functionality. Examples that demonstrate how to use the extended functionality are presented.

    [ Team LiB ] Previous Section Next Section