Feb. 1, 2007, 11:33 p.m.
posted by fractal
Creating and Using Strongly Typed DataSet Objects
The best way to understand strongly typed DataSet objects is to see what you can do with one syntactically. Suppose you have a normal DataSet object, and you've extracted a DataTable object named dt from this DataSet object. Then you can refer to a value in the DataTable object with any of these syntaxes: dt.Rows[0][0] dt.Rows[0]["ColumnName"] All these syntaxes have one thing in common: They're all late-bound. That is, .NET doesn't know until runtime that ColumnName is a valid column name in the DataTable. By contrast, in a strongly typed DataSet, the columns actually become properties of the row. With a strongly typed DataSet, an early-bound version of the data-retrieval code becomes available: dt.Rows[0].ColumnName In addition to being faster than the late-bound syntax, this syntax also has the benefit that column and table names show up in the applicable IntelliSense lists. In this section, you'll learn two different ways to create strongly typed DataSet objects. After that, I'll show you how to use a strongly typed DataSet in code. Using the Component Designer to Create a Strongly Typed DataSet ObjectOne way to create a strongly typed DataSet object is to derive it directly from a table or other data-bearing object in a database. Step-by-Step 1.10 demonstrates this technique.
In addition to the new objects in Solution Explorer, this example also adds a new component, dsEmployees1, to the component tray underneath the form. This is an instance of the class defined in dsEmployees.cs. If you inspect the code behind the form, you can find the declaration for the new component: private _320C01.dsEmployees dsEmployees1; Creating a Strongly Typed DataSet from a DataSet SchemaYou can also create a strongly typed DataSet from a DataSet schema file. More precisely, if you've created a DataSet schema in your Visual C# .NET project, Visual C# .NET automatically creates a strongly typed DataSet class that matches the structure defined in the schema. As you edit the schema, Visual C# .NET keeps the DataSet class synchronized with your edits. WARNING Naming a Strongly Typed DataSet Object You should not name the DataSet schema file dsCustomers.xsd with the name of its enclosing tables—that is, Customers.xsd or Orders.xsd. The corresponding code file (that is, the .cs file) created for this DataSet object contains properties to provide access to their enclosing members—that is, Customers or Orders. If you named a class with the same name as its enclosing table, you would get the error Member names cannot be the same as their enclosing type. If you've been following along with the Step-by-Steps in this chapter, take a look at the files in Solution Explorer. If you click the Show All Files toolbar button, you'll find two files as children of the dsCustomers.xsd DataSet schema file:
Using a Strongly Typed DataSet ObjectNow that you've built a strongly typed DataSet object, what can you do with it? Step-by-Step 1.11 demonstrates the syntax for using such a DataSet object.
A strongly typed DataSet class inherits from the base DataSet class, so it has all the methods and properties of the DataSet. The strong typing gives you the benefits of design-time IntelliSense and type checking. It also makes your code easier to read. Given the ease with which Visual Studio .NET can create strongly typed DataSet classes, you should plan to use them whenever possible.
|
- Comment




