Hack 49. Generate Strongly Typed DataSets![]() Studio and the XSD tool to generate typed DataSets. ADO .NET's DataSet objects provide powerful functionality when working with data. They allow you to easily scroll, filter, search, and sort data, as well as work with hierarchical data and its relationships. However, your typical DataSet object is untyped, meaning it doesn't really know and understand what it is storing. It's just a smart container for your data. You can make a DataSet object even smarter by turning it into a strongly typed DataSet object.When working with an untyped DataSet, accessing the value of a row looks something like this: String artistName =When working with a typed DataSet, you will have IntelliSense for the list of tables as well as the column names, and best of all, each property is stored in its correct type. This means you don't need to call ToString() or cast the type when pulling it out of the DataSet. Performing the same operation with a typed DataSet would look like this: string artistName = ds.Artists[0].Artist;Creating a strongly typed DataSet object is done one of three ways: by writing the code yourself, by letting Visual Studio help you, or by using the xsd.exe tool. Hand-writing the code to make strongly typed DataSet objects can be long and tedious, so it's best to use one of the other two methods to do the heavy lifting for you. 6.7.1. Let Visual Studio HelpVisual Studio provides a nice shortcut to creating your own strongly typed DataSet objects. Follow these simple steps:Open your Visual Studio Project (or create a new one).Select the project in the Solution Explorer and click the Show All Files button.Right-click on the project in the Solution Explorer, click Add, and select Add New Item.In the Add New Item dialog box, select the DataSet template. In the Name field, type the name of your DataSet (e.g., Customers.xsd) and click Open.You will see that the XSD file was added to your project. There are two things to note here. First, right-click on the XSD file and select Properties. This is where you can specify a custom tool to generate your strongly typed DataSet objects. By default, the custom tool is named MSDataSetGenerator, which is provided out of the box by Visual Studio (see Figure 6-8). Figure 6-8. Properties of DataSet XSD file![]() DataSet object was autogenerated for you. You can see it by turning on Show All Files and expanding the XSD file. Figure 6-9 shows that the Customers.cs class file was autogenerated to create a DataSet object of type Customers. Figure 6-9. Autogenerated DataSet class file![]() 6.7.2. Using the xsd.exe ToolThe .NET Framework SDK includes a tool namedxsd.exe that can be used for a few different reasons; one of the handiest is for creating strongly typed DataSet objects. The best and easiest way to accomplish this is by first creating your own XML schema definition file (*.xsd). Because a DataSet object natively supports XML (as with everything else in .NET), using XSD files as the basis for your data and its structure makes the creation of strongly typed DataSet objects rather simple.Assuming your XSD file is already created, using the xsd.exe tool will save you a significant amount of time because it generates your classes automatically based on the information contained in your XML schema definition.To get started, let's first take a quick look at a sample XSD, which is based on the Customers table in the Northwind database: <?xml version="1.0" standalone="yes"?>Notice that the XSD is rather simple, but does contain some database schema information. For instance, by studying the Customers.xsd, you can see that the CustomerID and CompanyName fields are required but all other fields can be null and that the CustomerID field is the primary key.To create a strongly typed DataSet object of type Customers, feed the Customers.xsd file into the xsd.exe tool and have it generate the Customers class for you. To do this, perform the following:Open the Visual Studio .NET command prompt and browse to the directory where your XSD file exists.Type the following at the prompt: xsd.exe SchemaName.xsd /DataSet /language:CS, where SchemaName.xsd is the name of your XSD file. In this example, the command line looks like this: xsd.exe Customers.xsd /DataSet /language:CS. For Visual Basic .NET, use /language:VB.Look for your generated class file. In this sample, a file named Customers.cs will be created.There are several options and flags for the xsd.exe tool, but the important flag for creating your strongly typed DataSet object is the /DataSet flag. This tells the tool to parse through the XSD file and autogenerate a strongly typed DataSet object for you. A view of the command line looks like this: C:\>xsd.exe Customers.xsd /DataSet /language:CSAgain, you can see the class file that was generated in this example was named Customers.cs. Taking a look at this class, you can see that the xsd.exe tool created a class named Customers that inherits from the DataSet class, thus creating a strongly typed DataSet object: [Serializable( )]Including all of the code here would be excessivethe generated class is more than 600 lines long, but it is important to note that the Customers class is not the only class contained in this file. The following classes were also generated:CustomerDataTableCustomerRowCustomerRowChangeEventCustomerRowChangeEventHandler I would encourage you to examine the generated classes further to better understand how strongly typed DataSets work. Dave Donaldson |