Manipulating span class

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


Manipulating DataSet Relationships

In this section, you see how to add keys to a DataSet schema, and then how to create relationships among multiple tables that are part of the same DataSet schema.

Adding Keys to a DataSet Schema

The DataSet schema designer enables you to create two types of keys. You can identify a field or a set of fields as the primary key for a table, or you can identify a field or a set of fields as making up a unique key. Take the following steps to create keys in the DataSet Designer:

  1. Ensure that the DataSet schema file that you edited in Example2_5 is open in the DataSet schema designer.

  2. Select the key tool in the toolbox. Drag the key from the toolbox and drop it on the Customers table. This opens the Edit Key dialog box.

  3. Name the key CustomersPrimaryKey. Select CustomerID in the Fields list. Check the DataSet Primary Key check box and click OK to create the key. A key icon appears next to the CustomerID element.

  4. Select the key tool in the toolbox. Drag the key from the toolbox and drop it on the Customers table. This opens the Edit Key dialog box.

  5. Name the key CompanyNameUniqueKey. Select CompanyName in the Fields list. A key icon appears next to the CompanyName element.

You can create one primary key and as many unique keys as you like for each table in the DataSet schema designer. The primary key identifies the field (or combination of fields) that uniquely specifies an individual row in the table. Unique keys identify other fields or combinations of fields that cannot be repeated in different records of the same table.

One-to-Many Relationships

A one-to-many relationship is the most common type of relationship in a relational database. In a one-to-many relationship, a record in TableA can have more than one matching record in TableB, but a record in TableB has, at most, one matching record in TableA.

One way to relate two tables in a DataSet schema is to identify common key columns and then use those columns to create a relationship between the tables. This is similar to the way that relational database products handle relationships. Take the following steps to create one-to-many relationships in the DataSet schema designer:

  1. Ensure that the DataSet schema file that you edited in Example2_5 is open in the DataSet schema designer.

  2. Open the Server Explorer window and navigate to the Tables node of the Northwind database. Drag and drop the Orders table onto the schema designer.

  3. Select the Relation tool in the toolbox. Drag the Relation from the toolbox and drop it on the Orders table. This opens the Edit Relation dialog box.

  4. Name the relation CustomersOrders. Select Customers as the parent element and Orders as the child element. Select CustomersPrimaryKey as the key to use. Select CustomerID as both the key field and the foreign key field. Click OK to create the relation. The DataSet schema designer draws a relation object and connecting lines between the two tables, as shown in Figure 2.3.

    Figure 2.3. The Customers table has a one-to-many relationship with the Orders tables.

    graphics/02fig03.gif

Nested Relationships

Although one-to-many relationships are the only kind you'll find in a typical relational database, the DataSet schema designer also supports nested relationships. In a nested relationship, the child table is stored as a complex data type within the parent table. Follow these steps to create a nested relationship:

  1. Ensure that the DataSet schema file that you edited in Example2_5 is open in the DataSet schema designer.

  2. Open Server Explorer Window and navigate to the Tables node of the Northwind database. Drag and drop the Order Details table on top of the Orders table. This creates a relationship between the Orders table and the Order Details table, as shown in Figure 2.4.

    Figure 2.4. The Orders table has a nested relationship with the Order Details table.

    graphics/02fig04.gif

  3. Note that, in this case, there's no explicit relationship object; the line between the two tables does not have a diamond on it. Instead, the Order Details table has been added as a complex type to the Orders table.

Because there are two different ways to relate tables in a DataSet schema, you have to choose between the two. Here are some points to help you decide whether to use one-to-many or nested relationships in your DataSet schemas:

  • One-to-many relationships more directly represent the way that data is stored in a relational database. If your data is primarily stored in a database, this provides the most natural mapping.

  • Nested relationships are more natural to represent in XML. If your data is not stored in a relational database, nested relationships provide a cleaner and more succinct XML representation of the connections between tables.

  • If you require interoperability with other XML applications, nested relationships are more likely to be correctly interpreted.

Creating a Strongly Typed DataSet from a DataSet Schema

If you've created a DataSet schema, 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.

In the project Example2_5, if you click the Show All Files toolbar button, you'll find two files as children of the dsCustomers.xsd DataSet schema file:

  • dsCustomers.cs This file is the class definition for a strongly typed DataSet based on the DataSet schema that you've been working with.

  • dsCustomers.xsx This file contains information on the layout of objects within the DataSet schema designer window.


    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