Creating a DataSet with Relational Data in XML Schemas

only for RuBoard

Creating a DataSet with Relational Data in XML Schemas

Now that you have created an XML Schema, it's time to create an ADO.NET typed DataSet from an XML Schema.

To create an ADO.NET DataSet from an existing schema, all you have to do is this: With the schema file loaded in Schema view, choose Generate DataSet from the Schema menu, as shown in Figure 4.20.

Figure 4.20. Selecting the Generate DataSet option from the Schema menu.
graphics/04fig20.gif

The XML Designer generates a DataSet Customers.cs , which has the same base name as that of the .xsd file. To see the newly generated DataSet file in the Solution Explorer, you might have to choose Show All Files from the Project menu.

When we talk about representing relational data with XML Schemas, it involves the creation of individual tables with columns and creating relationships between them based on the keys defined on the columns. We will discuss how typical database constructs, such as tables, columns , primary keys, foreign keys, unique keys, and constraint rules, are created in an XML Schema and get the corresponding DataSet that's automatically generated by the XML Designer. You'll create the two tables, Customers and Order s, with the primary keys defined on them and then create a one-to-many relationship between the Customers table (one) and the Orders table (many).

Here are the steps to complete this process:

  1. Add a DataSet to the project by using the Add New Item option from the Solution Explorer (as shown in Figure 4.21). This adds a new schema file ( CustOrders.xsd ) and a corresponding DataSet class file, with the same name as that of the schema file, to your project.

    Figure 4.21. Add a new Dataset to the project.
    graphics/04fig21.gif
  2. In the last section, "Creating an XML Schema with XML Designer," you created complex elements in the schema by dragging from the XML Schema tab of the Toolbox. Using the same technique, you can create the following Customers element that represents a Customers table, and the subelements CustomerID to Zip that represent the columns of the table. After doing this, your table should look similar to Figure 4.22.

    Figure 4.22. The Customers table.
    graphics/04fig22.gif

    Notice that the primary key is added to this table. To add the primary key, right-click to the left of the row of the element that you want to designate as a key; alternatively, drag a key element from the XML Schema tab of the Toolbox and drop it on this row. The Edit Key dialog box appears, as shown in Figure 4.23.

    Figure 4.23. The Edit Key dialog box.
    graphics/04fig23.gif
  3. In the Name box, replace the default name with the name CustomersIDKey or anything that is appropriate for this key. If you need a multipart key, you can select additional elements in the Field area. To define a primary key, check Dataset Primary Key. If you do not check this option, the key becomes unique and null values will be allowed.

  4. Follow the guidelines in step 2 to create another table, Orders , as shown in Figure 4.24. Create a primary key on the OrderID field with the name OrderIDKey.

    Figure 4.24. The Orders table.
    graphics/04fig24.gif

    This completes the creation of the two tables.

  5. Now, you can create a relationship between the Customers and Orders tables by using the Relation object. Drag and drop a Relation object from the XML Schema tab of the Toolbox anywhere exactly on the Orders table. The Edit Relation dialog box appears, as shown in Figure 4.25.

    Figure 4.25. The Edit Relation dialog box.
    graphics/04fig25.gif

    Set the Parent element to Customers and the Child element to Orders . You can leave the default name CustomersOrders that appears in the Name box. Set the key to CustomersIDKey , which you defined earlier in the Customers table. In the Foreign Key Fields area, select CustomerID to match the key field in the parent Orders table. Leave the rest of the default settings as is and click OK. This creates a Relation object that appears on the designer surface, as shown in Figure 4.26.

    Figure 4.26. The Relation object.
    graphics/04fig26.gif
  6. Save the file. The XML Designer regenerates the DataSet class file to reflect the changes you made to the schema file since the time of creation.

By switching to XML view, you can see the following code added to the CustOrders.xsd file:

 <?xml version="1.0" encoding="utf-8" ?>  <xsd:schema id="CustOrders"  targetNamespace="http://tempuri.org/CustOrders.xsd"  elementFormDefault="qualified" xmlns="http://tempuri.org/CustOrders.xsd"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas- microsoft-com:xml-msdata">       <xsd:element name="CustOrders" msdata:IsDataset="true">            <xsd:complexType>                 <xsd:choice maxOccurs="unbounded">                      <xsd:element name="Orders">                           <xsd:complexType>                                <xsd:sequence>                                      <xsd:element name="OrderID" type="xsd:string" />                                      <xsd:element name="CustomerID" type="xsd:string" />                                      <xsd:element name="OrderDate" type="xsd:date" graphics/ccc.gif minOccurs="0" />                                      <xsd:element name="ShippedDate" type="xsd:date" graphics/ccc.gif minOccurs="0" />                                      <xsd:element name="ShipVia" type="xsd:string" graphics/ccc.gif minOccurs="0" />                                </xsd:sequence>                           </xsd:complexType>                      </xsd:element>                      <xsd:element name="Customers">                           <xsd:complexType>                                <xsd:sequence>                                       <xsd:element name="CustomerID" type="xsd:string" />                                       <xsd:element name="CompanyName" type="xsd:string" />                                       <xsd:element name="ContactName" type="xsd:string" graphics/ccc.gif minOccurs="0" />                                       <xsd:element name="Street" type="xsd:string" graphics/ccc.gif minOccurs="0" />                                       <xsd:element name="City" type="xsd:string" graphics/ccc.gif minOccurs="0" />                                       <xsd:element name="State" type="xsd:string" graphics/ccc.gif minOccurs="0" />                                       <xsd:element name="Zip" type="xsd:string" graphics/ccc.gif minOccurs="0" />                                </xsd:sequence>                           </xsd:complexType>                      </xsd:element>                 </xsd:choice>            </xsd:complexType>            <xsd:key name="CustomersIDKey" msdata:PrimaryKey="true">                 <xsd:selector xpath=".//Customers" />                 <xsd:field xpath="CustomerID" />            </xsd:key>            <xsd:key name="OrderIDKey" msdata:PrimaryKey="true">                 <xsd:selector xpath=".//Orders" />                 <xsd:field xpath="OrderID" />            </xsd:key>            <xsd:keyref name="CustomersOrders" refer="CustomersIDKey">                 <xsd:selector xpath=".//Orders" />                 <xsd:field xpath="CustomerID" />            </xsd:keyref>       </xsd:element>  </xsd:schema> 

This chapter doesn't discuss the generated DataSet class because you can find a detailed discussion of it in Chapter 8, "Database Access with ADO.NET and XML." Figure 4.27 shows the generated DataSet that's being used in the code editor.

Figure 4.27. Using the auto-generated typed DataSet .
graphics/04fig27.gif
only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

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