Creating and Using Strongly Typed DataSets

   


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.

Using the Component Designer to Create a Strongly Typed DataSet

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 STEP

1.10 Creating a Strongly Typed DataSet With the Component Designer

  1. Add a new Windows form to your Visual Basic .NET project.

  2. Expand the Server Explorer treeview to show a Data Connection to the Northwind sample database. Drill into the Tables folder within this database.

  3. Drag the Employees table from Server Explorer and drop it on the form. This will add a SqlConnection object named SqlConnection1 and a SqlDataAdapter object named SqlDataAdapter1 to the component tray of the form, as shown in Figure 1.16.

    Figure 1.16. ADO.NET Objects created from Server Explorer.

  4. Select the SqlDataAdapter object. Click the Generate DataSet hyperlink at the bottom of the Properties Window, as shown in Figure 1.17.

    Figure 1.17. Generating a DataSet via the Properties Window.

  5. Clicking the hyperlink will open the Generate DataSet dialog box, as shown in Figure 1.18. Name the new DataSet object dsEmployees and select the check box to add the DataSet to the designer. Click OK.

    Figure 1.18. The Generate DataSet dialog box.

  6. You'll see a new object, dsEmployees.xsd, appear in Solution Explorer. This is a DataSet schema file that describes the new strongly typed DataSet. Click the Show All Files button on the Solution Explorer toolbar. Expand the dsEmployees.xsd node to see the dsEmployees.vb file, as shown in Figure 1.19. This is a class file that can be instantiated to produce the strongly typed DataSet.

    Figure 1.19. Strongly Typed DataSet in Solution Explorer.

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 

Creating a Strongly Typed DataSet from a DataSet Schema

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.

Using a Strongly Typed DataSet

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 STEP

1.11 Using a Strongly Typed DataSet

  1. Open the form that you created in Step By Step 1.10 in the designer. Add a new ListBox control to the form, and name the control lbEmployees .

  2. Double-click on the form to open the event handler for the form's Load event. Add this code to handle the Load event:

     Private Sub Form1_Load(_  ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load     Dim EmpRow As dsEmployees.EmployeesRow     SqlDataAdapter1.Fill(DsEmployees1, "Employees")     For Each EmpRow In DsEmployees1.Employees         lbEmployees.Items.Add(_          EmpRow.FirstName & " " & EmpRow.LastName)     Next End Sub 
  3. Notice as you type this code that the IntelliSense feature fills in the names of tables and columns for you.

  4. Set the form as the startup object for the project. Run the project. You'll see a list of employees on the form, as shown in Figure 1.20.

    Figure 1.20. Data from 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

  • A strongly typed DataSet brings the benefits of early binding to your data access code.

  • You can create a strongly typed DataSet by using the component designer with components dragged from Server Explorer or by building a DataSet schema file.

  • When you're working with a strongly typed DataSet in code, IntelliSense will show you the names of the tables and columns contained within the DataSet.


   
Top


MCAD. MCSD Training Guide (Exam 70-310. Developing XML Web Services and Server Components with Visual Basic. NET and the. NET Framework)
MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
ISBN: 0789728206
EAN: 2147483647
Year: 2002
Pages: 166

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