Strongly Typed DataSet Objects

Team-Fly    

Developing XML Web Services and Server Components with Visual C#™ .NET and the .NET Framework, Exam Cram™ 2 (Exam 70-320)
By Amit Kalani, Priti Kalani

Table of Contents
Chapter 2.  Consuming and Manipulating DataSets


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. 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 object. By contrast, in a strongly typed DataSet object, the columns actually become properties of the row. With a strongly typed DataSet object, an early bound version of the data-retrieval code becomes available:

 dt.Rows[0].ColumnName 

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.

Take the following steps to learn how to create a strongly typed DataSet:

  1. Add a new Visual C# ASP.NET Web Application project (Example2_4) to the existing solution.

  2. Expand the Server Explorer tree view to show the (local)\NetSDK SQL Server. Drill into the Tables folder within the Northwind sample database.

  3. Drag the Employees table from Server Explorer and drop it on the form. This adds a SqlConnection object named sqlConnection1 and a SqlDataAdapter object named sqlDataAdapter1 to the component tray of the form.

  4. Right-click the SqlDataAdapter object in Design view and select the Generate DataSet option from the shortcut menu. This action opens the Generate Dataset dialog box. Name the new DataSet object dsEmployees, and select the check box to add the DataSet to the DataSet schema designer. Click OK.

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

In addition to the new objects in Solution Explorer, this example 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 Example2_4.dsEmployees dsEmployees1; 

Now that you've built a strongly typed DataSet object, take the following steps to learn how to use it:

  1. Open the project Example2_4. Add a ListBox control (lbEmployees) and a Button control (btnGetEmployees) to the form.

  2. Double-click on the Button control and add the following code to handle the Click event:

     private void btnGetEmployees_Click(object sender, System.EventArgs e) {     sqlDataAdapter1.Fill(dsEmployees1, "Employees");     foreach(dsEmployees.EmployeesRow EmpRow in dsEmployees1.Employees)        lbEmployees.Items.Add(EmpRow.FirstName + " " + EmpRow.LastName); } 

    Notice as you type this code that the IntelliSense feature fills in the names of tables and columns for you.

  3. Set the project Example2_4 as the startup project and run the application. Click on the Get Employees button to see a list of employees.

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.


    Team-Fly    
    Top


    MCAD Developing XML Web Services and Server Components with Visual C#. NET and the. NET Framework Exam Cram 2 (Exam Cram 70-320)
    Managing Globally with Information Technology
    ISBN: 789728974
    EAN: 2147483647
    Year: 2002
    Pages: 179

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