Manipulating a DataSet Schema

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 a DataSet Schema

A DataSet schema is an XML representation of the metadata that defines the structure of a DataSet. Visual Studio .NET provides two ways to create a DataSet schema:

  • Drag and drop a table from the Visual Studio .NET Server Explorer to the form. Right-click on the SqlDataAdapter object and select Generate DataSet. A .xsd file for the DataSet will be automatically created.

  • Select Project, Add New Item, and select the Data Set template for the new file. This file has an .xsd extension. Visual Studio .NET supports visual editing of .xsd files.

Creating a DataSet Schema

Take the following steps to create a DataSet schema "from scratch," without reference to an existing DataSet or to a table stored on a database server:

  1. Create a new Visual C# .NET Web Application project (Example2_5).

  2. Add a new item (dsCustomers.xsd) based on the DataSet template to the project.

  3. Select the Element tool in the toolbox. Drag the element from the toolbox and drop it on the design surface. This creates an element named element1. Rename the element Customers by typing over the name.

  4. Drag another element and drop it on top of the first element. This creates a nested element. Name the new element CustomerID. The designer assumes that the new element will be a complex element that is, one that contains other elements. To change the CustomerID element to a simple element, change its data type to string by selecting a data type from the drop-down list to the right of the element.

  5. Create a CompanyName element by dragging another element from the toolbox, renaming it, and changing its data type to string.

You can also use the XML designer to add attributes to a DataSet schema. Attributes provide an alternative way to represent columns within a DataSet. Take the following steps to add attributes to a DataSet schema:

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

  2. Select the Attribute tool in the toolbox. Drag the attribute from the toolbox and drop it on top of the complex element named Customers. This creates an attribute named attribute1. Rename the attribute ContactName by typing over the name. You'll see that the attribute is assigned the string data type by default. Attributes cannot be used for complex data types.

  3. Drag a second attribute to the Customers element. Rename the attribute ContactTitle.

Given that you can represent columns by either elements or attributes within a DataSet schema file, how do you decide which representation to use? Here are some points to consider:

  • If you need to exchange the schema file with other applications, you must be sure that it is compatible with those applications. Microsoft Access, for example, can import a schema defined using elements but not one defined using attributes.

  • If your DataSet requires columns that are defined using a custom data type, you must use elements for those columns. Attributes are restricted to the built-in data types defined by the World Wide Web Consortium (W3C).

  • To represent a child table in a hierarchy, you must use an element.

Using Simple Types

When you define a column with an element or an attribute, there are no restrictions on that column beyond those imposed by the data type that you choose for the element or attribute. For example, you might want to require that the CustomerID column be a string consisting of between 5 and 10 characters. Take the following steps to add a simple type to a DataSet schema:

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

  2. Select the SimpleType tool in the toolbox. Drag the simple type from the toolbox and drop it in a blank area of the XML designer. This creates a simple type named simpleType1. Rename the simple type CustomerIDType by typing over the name. You'll see that the simple type is assigned the string data type by default.

  3. Click in the second row of the simple type, directly under the ST icon. This produces a drop-down arrow. Click the arrow and select Facet from the list (it is the only item in the list). In the next column select minLength. In the last column, type the value 5.

  4. Add another facet to the simple type. Select the maxLength facet and set the maximum length to 10.

  5. Click the data type drop-down for the CustomerID element. You'll find that this list now contains the CustomerIDType data type. Set the element to use this data type.

In this example, minLength and maxLength are data type facets. Facets help define the acceptable range of data for an element.

graphics/tip_icon.gif

You can also create schema elements by dragging a view, a stored procedure, a function, or a set of columns from Server Explorer to the DataSet schema designer.



    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