Create and Manipulate DataSets: Create a strongly typed DataSet.
The best way to understand strongly typed DataSet objects is to see what you can do with one syntactically. Suppose that you have a normal DataSet, and you've extracted a DataTable object named dt from this DataSet. Then you can refer to a value in the DataTable with any of these syntaxes:
dt.Rows(0).Item(0) dt.Rows(0)(0) dt.Rows(0).Item("ColumnName") dt.Rows(0)("ColumnName") 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.
One way to create a strongly typed DataSet is to derive it directly from a table or other data- bearing object in a database. Step By Step 1.10 demonstrates this technique.
STEP BY STEP1.10 Creating a Strongly Typed DataSet With the Component Designer
|
In addition to the new objects in Solution Explorer, this example will also add a new component, DsEmployees1, to the component tray underneath the form. This is an instance of the class defined in dsEmployees.vb. If you inspect the code behind the form, you'll find the declaration for the new component:
Friend WithEvents DsEmployees1 As _310C01.dsEmployees
You can also create a strongly typed DataSet from a DataSet schema filemore precisely, if you've created a DataSet schema in your Visual Basic .NET project. Visual Basic .NET will automatically create a strongly typed DataSet class that matches the structure defined in the schema. As you edit the schema, Visual Basic .NET will keep the DataSet class synchronized with your edits.
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 Customers.xsd DataSet schema file:
Customers.vb This file is the class definition for a strongly typed DataSet based on the DataSet schema that you've been working with.
Customers.xsx This file contains information on the layout of objects within the DataSet schema designer window.
Now that you've built a strongly typed DataSet, what can you do with it? Step By Step 1.11 demonstrates the syntax for using such a DataSet.
STEP BY STEP1.11 Using a Strongly Typed DataSet
|
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.
REVIEW BREAK
|
Top |