Manipulating a DataSet Schema


Create and Manipulate DataSets: Manipulate a DataSet Schema.

A DataSet schema is an XML file that describes the structure of a DataSet. You can work with such files as raw XML, but if you have Visual Studio .NET, there's a better way. Visual Studio .NET includes a specialized XML designer that provides a visual representation of DataSet schema files. In this section, you'll learn how to use this designer. You'll see that you can create a DataSet schema and edit it easily with the same sort of drag-and-drop operations that are used to create Windows Forms or Web Forms.

The XML designer is also integrated with the database support in Server Explorer. You can add items to a DataSet schema file by dragging and dropping from Server Explorer.

To use the XML designer in Visual Studio .NET with a DataSet schema file, you'll need to create a new Visual Studio .NET project to host the file. After you've done that, you can perform many design tasks. In this section, you'll learn how to perform these tasks on a DataSet schema:

  • Create a new schema

  • Add items such as elements, attributes, types, and facets to the schema

  • Use items from Server Explorer in a schema

  • Create element groups and attribute groups

Creating a DataSet Schema

Visual Studio .NET allows you to create a DataSet schema "from scratch" without reference to an existing DataSet or to a table stored on a database server. Step By Step 1.1 demonstrates how you can do this.


1.1 Creating a New DataSet Schema

  1. Create a new Visual Basic .NET Windows application. Name the application 310C01.

  2. Right-click on the project node in Solution Explorer and select Add, Add New Item.

  3. Select the Local Project Items node in the Categories treeview. Select the DataSet template. Name the new DataSet Customers.xsd , as shown in Figure 1.1, and click Open .

    Figure 1.1. Creating a new DataSet schema file.

  4. The new DataSet schema file will be opened in the XML Designer, as shown in Figure 1.2. The Toolbox will display tools appropriate for creating new content in the schema file.

    Figure 1.2. Empty DataSet schema file in the XML designer.

An empty DataSet schema is not very interesting or useful. In the next section of the chapter, you'll learn how to add information to the schema to help dictate the layout of the DataSet that it defines.

Elements and Attributes

Like other XML files, DataSet Schema files are made up of XML elements. You can think of an element as a unit of information. For example, if you're designing a DataSet to hold data from a database table named Customers with columns named CustomerID and CompanyName, Customers, CustomerID, and CompanyName would all be represented by XML elements. Step By Step 1.2 shows you how to add elements to a DataSet schema file.


1.2 Adding Elements to a DataSet Schema

  1. Ensure that the DataSet schema file that you created in Step By Step 1.1 is open in the designer.

  2. Select the Element tool in the Toolbox. Drag the element from the Toolbox and drop it on the design surface. This will create an element named element1 . Rename the element Customers by typing over the name, as shown in Figure 1.3.

    Figure 1.3. Top-level element represents a table.

  3. Drag another element and drop it on top of the first element. Name the new element CustomerID . This will create a nested element, as shown in Figure 1.4.

    Figure 1.4. An element with a nested element.

  4. The designer assumes that the new element will be a complex elementthat is, one that contains other elements. To change the CustomerID element to a simple element, select a data type from the drop-down list to the right of the element. Figure 1.5 shows the result of defining the CustomerID element to be a string.

    Figure 1.5. An element with a simple nested element.

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

  6. Switch to XML view of the schema file (using the tab at the bottom of the designer) and inspect the generated XML.

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.


1.3 Adding Attributes to a DataSet Schema

  1. Ensure that the DataSet schema file that you edited in Step By Step 1.2 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 will create 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 . Figure 1.6 shows the state of the DataSet schema after this change.

    Figure 1.6. A complex element containing both elements and attributes.

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 W3C.

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


Visual Studio .NET Default When you create a DataSet schema from a database using Visual Studio .NET's built-in tools, it will always use elements to represent the database columns.

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 somewhere between 5 and 10 characters . To do this, you can define a simple type in the XML designer, using the technique from Step By Step 1.4.


1.4 Adding a Simple Type to a DataSet Schema

  1. Ensure that the DataSet schema file that you edited in Step By Step 1.3 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 will create 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 will show a drop-down arrow. Click the arrow and select Facet from the list. (It will be 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. Figure 1.7 shows the simple type at this point.

    Figure 1.7. Creating a simple type in the XML schema designer.

  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, as shown in Figure 1.8.

    Figure 1.8. Using a simple type for an XML element.

In this Step By Step, minLength and maxLength are data type facets. Facets help define the acceptable range of data for an element. Table 1.1 shows the data type facets that are available in the DataSet schema designer. This table gives you a sense of what restrictions you can place on DataSet elements.

Table 1.1. Data Type Facets

Facet Name



Set of allowable values.


Maximum number of digits in the fractional part of a number.


Fixed length of the data.


Maximum allowed value must be less than this number.


Maximum allowed value must be less than or equal to this number.


Maximum length of a variable length field.


Minimum allowed value must be greater than this number.


Minimum allowed value must be greater than or equal to this number.


Minimum length of a variable length field.


Regular expression that specifies a pattern that the data must conform to.


Maximum number of total decimal digits in a number.


Can be set to preserve (to leave whitespace unchanged), replace (to replace tabs, line feeds, and carriage returns with spaces) or collapse (to replace all contiguous whitespace characters with a single space character).


Facets in the Designer At any time, the XML designer will only show you the facets that are applicable to the current data type. For example, if you're basing a simple data type on the string data type, you can only set the enumeration , length , minLength , maxLength , pattern , and whiteSpace facets.

Using Server Explorer with the XML Designer

You can also create DataSet schemas quickly by using the Server Explorer, which allows you to interact directly with SQL Server or other databases. You might not have used Server Explorer in the past, so before I show you this particular use, I'll give you a quick overview.

By default, the Server Explorer window in Visual Studio .NET is displayed as a small vertical tab to the left of the Toolbox. When you hover the mouse over this tab, the Server Explorer will slide out to cover the Toolbox. Figure 1.9 shows the two states of the Server Explorer window.

Figure 1.9. The Server Explorer is normally displayed as a small vertical tab.

Although we're going to use the Server Explorer to work with databases, it's really a general-purpose tool for managing server resources of many types. Table 1.2 lists the resources that you can manage with Server Explorer.

Table 1.2. Resources That You Can Manage with Server Explorer

Resource Type


Data Connection

A connection to a particular database

Crystal Services

Options for Crystal Reports

Event Logs

Windows event logs

Message Queues

Windows message queues

Performance Counters

Windows performance counters


Windows services

SQL Servers

Microsoft SQL Servers

To work with DataSet schema files, you'll use the Data Connection node in Server Explorer and its children. To start working with those objects, you'll need to add a Data Connection to the Server Explorer tree. Step by Step 1.5 shows you how to do this.


1.5 Adding a Data Connection from Server Explorer

  1. Open Server Explorer.

  2. Right-click the Data Connections node and then select Add Connection. This will open the Data Link Properties dialog box.

  3. Fill in the connection information for your data source. The dialog box will default to using the Microsoft OLE DB Provider for SQL Server, but you can change that on the Provider tab if you prefer. As an example, for a SQL Server Data Connection (the only kind I'll be using in this book), you must select the server name from the drop-down list, fill in your username and password, and then select the database from the second drop-down list. You can use the Test Connection button to make sure that you've successfully completed these steps.

  4. Click OK to create the Data Connection. For ease in completing the other Step By Steps in this chapter, you should create at least one Data Connection based on an instance of the SQL Server Northwind sample database.


Supported Connection Types You've probably noticed that the Data Link Properties dialog box gives you a great many choices on the Provider tab. In addition to SQL Server, connections using the Oracle or Jet providers are also fully supported by .NET. Other providers might work, but there's no guarantee, and you should test your application carefully if you decide to use another provider.

Visual Studio .NET remembers your Data Connections across sessions and projects. Any Data Connection that you've created will appear in Server Explorer in all of your projects unless you right-click the Data Connection and choose Delete.

After you've created a Data Connection, you can use objects from that Data Connection in DataSet Schema design. Step By Step 1.6 demonstrates this process.


1.6 Using a Server Explorer Table in the DataSet Schema Designer

  1. With the DataSet schema from Step By Step 1.4 open in the designer, open Server Explorer.

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

  3. Drag the Orders table from Server Explorer and drop it on the DataSet schema designer. This will create a new element with all the necessary included elements to represent the Orders table.

  4. Repeat the process to bring the Order Details table into the DataSet schema designer. Figure 1.10 shows the designer with three tables and one simple type on the design surface.

    Figure 1.10. Three tables inside the DataSet schema designer.


Alternative Objects 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.

If you're an experienced database developer, you might have noticed that there's no sign of any relationship between the three tables in the DataSet schema. I'll show you how to create such a relationship in the next section of the chapter.


  • DataSet schema files represent the metadata that describes the allowable content of a DataSet.

  • You can create a DataSet schema file from scratch by dragging and dropping elements and attributes within the DataSet schema designer.

  • Simple types allow you to apply constraints to the data that will be allowed in a DataSet.

  • You can quickly create a DataSet schema to represent an existing table by dragging and dropping the table from Server Explorer to the DataSet schema designer.


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 © 2008-2017.
If you may any questions please contact us: