14.3 Generating an XML Schema for an Untyped DataSet

 <  Day Day Up  >  

14.3 Generating an XML Schema for an Untyped DataSet

You want to generate a schema for an untyped DataSet that can then be used to create a typed DataSet .


Technique

A typed DataSet is a class derived from a DataSet containing specific methods and properties designed to work with certain tables in a database. Creating a typed DataSet involves creating an XML schema for an untyped DataSet by using the tools within Visual Studio .NET. This example creates a typed DataSet from the Products table in the Northwind database.

Assuming you have a project created, choose Project, Add New Item. Select the Data Set template and give it a name. The name that you place within the Name field will be the name of the generated class. When the item is created, you will be placed in a design view. Open Server Explorer and locate the data connection you want to use. For this example, use the MachineName.Northwind.dbo data connection. Expand the data connection until you locate the Products table within the Tables node, and drag and drop the table onto the designer. When you save the XML schema file, Visual Studio .NET will generate the underlying DataSet class. By default, the C# source file is hidden from view within Solution Explorer. To view the generated class, either click on the Show All Files toolbar button located at the top of Solution Explorer or view the class using Class View.

Comments

A DataSet presents data hierarchically by utilizing properties and collections extensively, each returning other ADO.NET objects. For instance, accessing the Tables property of the DataSet class returns a DataTable object, and using the Relations collection returns a DataRelation object. Within the DataTable is a collection of DataColumn and DataRow objects as well as any relational links using the ParentRelations and ChildRelations collection, each of which is implemented as a DataRelation collection. When using an untyped DataSet , you work directly with these classes to access the data contained within the DataSet . However, the APIs for these classes are generic in that no custom methods and properties can be created and added to these objects at compile or runtime. A typed DataSet solves this problem.

The "Technique" section demonstrated a method to create a typed DataSet . The result of this procedure generated an XML schema representing the data format and a generated C# class containing custom classes, methods, and properties specifically designed for the table or tables it represents. The example generated four classes. The first is the typed DataSet , which in our project is named ProductsDS . This class is derived from the DataSet class, allowing you to use it wherever a DataSet class is used to represent the Products table. Within the class is a single property named Products , which you might have guessed represents the Products table for the database whose data type is a class derived from DataTable named ProductsDataTable . Once you start investigating this class, you can start to see where the customized methods and properties come into play. Some of the methods within the ProductsDataTable include AddProductsRow ; NewProductsRow ; FindByProductID , which was created because the ProductID field in the database is a primary key; and RemoveProductsRow . The rows themselves are another custom class derived from the DataRow class, which is named ProductsRow . The ProductsRow class contains several methods to check whether a certain field is null and a property that you can access corresponding to each field within the row. Furthermore, because the XML schema that was generated contained type information gathered from the SQL database, each property has a related .NET data type. The last class that is created is an EventArg derived class used as arguments passed to a delegate in response to a row within the Products table being changed.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net