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
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.
STEP BY STEP1.1 Creating a New DataSet Schema
|
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.
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.
STEP BY STEP1.2 Adding Elements to a DataSet Schema
|
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.
STEP BY STEP1.3 Adding Attributes to a DataSet Schema
|
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.
EXAM TIP
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.
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.
STEP BY STEP1.4 Adding a Simple Type to a DataSet Schema
|
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.
Facet Name | Description |
---|---|
enumeration | Set of allowable values. |
fractionDigits | Maximum number of digits in the fractional part of a number. |
length | Fixed length of the data. |
maxExclusive | Maximum allowed value must be less than this number. |
maxInclusive | Maximum allowed value must be less than or equal to this number. |
maxLength | Maximum length of a variable length field. |
minExclusive | Minimum allowed value must be greater than this number. |
minInclusive | Minimum allowed value must be greater than or equal to this number. |
minLength | Minimum length of a variable length field. |
pattern | Regular expression that specifies a pattern that the data must conform to. |
totalDigits | Maximum number of total decimal digits in a number. |
whiteSpace | 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). |
NOTE
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.
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.
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.
Resource Type | Represents |
---|---|
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 |
Services | 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.
STEP BY STEP1.5 Adding a Data Connection from Server Explorer
|
EXAM TIP
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.
STEP BY STEP1.6 Using a Server Explorer Table in the DataSet Schema Designer
|
EXAM TIP
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.
REVIEW BREAK
|
Top |