Chapter 6: Working with Objects

Working with DataSet Objects in Visual Studio .NET

You now know a great deal about the structure of DataSet objects and how to create them in code. But, man, that's a lot of code to write! Let's look at some features of the Visual Studio .NET development environment that you can use to create DataSet objects with a lot less effort.

Generating a DataSet from DataAdapter Objects

Early in the chapter, I explained that the structure of your DataSet generally depends on the structure of the data your DataAdapter objects will return and that you can use DataAdapter objects to create the structure for your DataSet. Visual Studio .NET also lets you create DataSet objects based on DataAdapter objects.

When you're working with a designer—such as a Windows form or a Web form—that contains DataAdapter objects, you can choose to generate a DataSet based on the data returned by those DataAdapter objects. Choose Generate Dataset from the Data menu, click on the Generate Dataset link in the Properties window, or right-click on the designer and choose Generate Dataset from the shortcut menu to open the Generate Dataset dialog box. The latter approach is shown in Figure 6-1. Figure 6-2 shows the Generate Dataset dialog box.

Figure 6-1

Launching the Generate Dataset dialog box

Figure 6-2

The Generate Dataset dialog box

In the dialog box, you can choose to create a new DataSet or modify the structure of an existing DataSet. The dialog box also allows you to select the DataAdapter objects to use in generating your new DataSet. As you can see in Figure 6-2, the dialog box shows both the DataAdapter name and the name of the table it references. Having both pieces of information can make it easier for you to determine which DataAdapter objects you want to use to build your DataSet. The dialog box also has a check box that you can select to add an instance of your new DataSet to the designer.

If you choose to add an instance of your new DataSet, itwill appear in the designer's components tray. Figure 6-3 shows an example in which we've created a new DataSet and added an instance to the designer. When you select the instance of your DataSet, you'll see the DataSet object's properties listed in the Properties window.

You'll also see a new file in the Solution Explorer window. The new file will have the same name as the name you specified for your new DataSet and will have an .xsd extension. Where does that come from, you ask? Visual Studio .NET creates a new DataSet object, calls the FillSchema method on the DataAdapter objects you referenced in the Generate Dataset dialog box, and then calls the WriteXmlSchema method on that DataSet to save its schema information to a file.

Figure 6-3

The new DataSet

note

This feature of the Visual Studio .NET development environment highlights how useful a feature such as DataAdapter.FillSchema can be when used properly.

Let's take a look at the .xsd file we created using the Generate Dataset dialog box. Double-click on the file in the Solution Explorer window to invoke the Visual Studio .NET XML schema designer, as shown in Figure 6-4. The .xsd file simply contains an XML schema file with a few added DataSet-specific attributes. The DataTable objects appear as elements within the DataSet. Similarly, DataColumn objects appear as elements within the DataTable objects. You can select an object—a DataColumn, a DataTable, or the design surface that represents the DataSet—and the Properties window will display properties for that object.

Figure 6-4

Examining the contents of the new DataSet schema file

Creating a New DataSet from Scratch

What if you want to create a new DataSet but you don't have DataAdapter objects to define its structure? You can add a new DataSet .xsd file to your project using the Add New Item dialog box. (See Figure 6-5.)

Figure 6-5

Adding a new DataSet to your project

Be sure to select the DataSet template in the dialog box rather than XML Schema. Selecting either item will add a new .xsd file to your project and launch the XML schema designer. However, DataSet schema files have an additional attribute set in their schema that Visual Studio .NET looks for to determine whether to treat the file as a standard XML schema or as a DataSet.

Once you've added a new DataSet to your project, you'll see the same designer that you saw when we generated a DataSet based on DataAdapter objects, except that your new DataSet will be empty. Let's add a new DataTable to the DataSet. Figure 6-6 shows the shortcut (context) menu that appears when you right-click on the designer. Choose Add and then New Element from the shortcut menu to add a new DataTable to the DataSet. You can also choose the same command from the Schema menu.

Figure 6-6

Adding a new DataTable to your new DataSet

note

ADO.NET can store DataSet schema information in XML schema format, so using an XML schema designer as a way to examine and modify the structure of your DataSet objects might seem like a natural fit. Unfortunately, this design choice has some shortcomings. XML-savvy developers will quickly pick up on concepts such as the fact that a DataTable in the DataSet corresponds to an element within the schema file, but developers with less XML experience might be confused by this interface. You might see a more data-specific DataSet designer in a future release of Visual Studio .NET. In the meantime, I'll cover the basic features of constructing a new DataSet using this interface.

Once you have a new DataTable, you'll want to add some DataColumn objects. Click on the leftmost cell in the first row in the box that represents your new DataTable in the XML schema designer. This should make an arrow appear just to the right of that cell. Click on the arrow and you'll see a list, similar to the one shown in Figure 6-7, of available items you can add to the DataTable. To add a new DataColumn, select Element from the list and then specify the name for your new DataColumn. The text you enter will be stored in the DataColumn object's ColumnName property.

Figure 6-7

Adding a new DataColumn to a DataTable

To set other properties on a DataColumn, select the desired column in the XML schema designer. Figure 6-8 shows part of the list of available properties that appears in the Properties window when you select a column in the XML schema designer. You'll notice that some of the properties are specific to the DataColumn and others are more applicable to XML elements. Some properties that were added to the ADO.NET object model late in the development cycle, such as the DataColumn object's MaxLength property, are not available through this interface.

Once you've created your new DataSet with the appropriate structure, save your changes and close the XML schema designer. You've now created a DataSet schema file in your project. To add an instance of your DataSet to a designer, select DataSet on the Data tab of the Visual Studio .NET Toolbox just as you would if you wanted to add another component, such as a button, to a designer. Double-click on the Toolbox item, or drag-and-drop the item onto the designer or its components tray. Either of these actions will launch the Add Dataset dialog box, shown in Figure 6-9.

Figure 6-8

Setting properties on the new DataColumn

Figure 6-9

Adding an instance of the new DataSet

The Add Dataset dialog box lets you select any of the DataSet schema files in your project. Simply select a DataSet from the list and click OK, and you'll add a new instance of the DataSet to your component.

If you want to add an instance of the new DataSet in code, you'll find that the DataSet is available as a new class defined within your project. Figure 6-10 shows an example of adding an instance of the new DataSet in code in a Visual Basic .NET code module.

Figure 6-10

Adding an instance of the new DataSet in code

Creating an Untyped DataSet

I've mentioned that Visual Studio .NET builds strongly typed DataSet objects, a topic I'll cover in more depth in Chapter 9. But what if you don't want to use strongly typed DataSet objects? Or what if you want to use an instance of a simple untyped DataSet but you want to add DataTable objects and DataColumn objects to your untyped DataSet at design time?

The Add Dataset dialog box shown in Figure 6-9 also includes an option button that you can select if you want an untyped DataSet. Let's see how you can use that feature to create an untyped DataSet.

First, use the DataSet item on the Data tab of the Visual Studio .NET Toolbox to add another DataSet to your designer. This time, when the Add Dataset dialog box appears, select the Untyped Dataset option and then click OK. You'll see your new DataSet in your designer's components tray.

Select the DataSet in the components tray, and you'll see its properties listed in the Properties window. To add DataTable objects to the DataSet, select the Tables property in the Properties window and then click on the ellipsis button to the right. Doing so will launch the Visual Studio .NET Collection Editor. Many components within Visual Studio .NET use the Collection Editor. Figure 6-11 shows how the Collection Editor appears when it is used to modify the DataTable objects within a DataSet.

Figure 6-11

Adding a new DataTable to an untyped DataSet

You'll also see the same editor when you add DataColumn objects to your new DataTable. To add DataColumn objects, select the DataTable to which you want to add columns in the Collection Editor. Then select the Columns collection in the window on the right and click the ellipsis button. You'll launch a new Collection Editor, shown in Figure 6-12, which is used for building DataColumn objects.

Figure 6-12

Adding a new DataColumn to a DataTable

Once you've added your DataColumn objects, you might want to specify a primary key for your DataTable. Select your DataTable in the Collection Editor, and you'll see PrimaryKey listed as a property in the window on the right. Select this property, and then click on the arrow to the right. You'll see the list of columns that appear in the DataTable, as shown in Figure 6-13. Select the DataColumn object you want to use for the DataTable object's primary key, and then dismiss the drop-down list by clicking elsewhere in the Collection Editor.

Figure 6-13

Specifying the primary key for a DataTable

You can also add items to the Constraints collection of your DataTable objects. Click on the Constraints property for your DataTable in the Collection Editor, and then click on the ellipsis button to launch another Collection Editor. This new Collection Editor, shown in Figure 6-14, lets you modify the contents of your DataTable object's Constraints collection. If you already have a primary key defined on your DataTable, you'll see that there's already an item in the Constraints collection.

Figure 6-14

Adding constraints to a DataTable

You can use the Collection Editor to add unique or foreign key constraints. Click the Add button, and you'll see a shortcut menu asking which type of constraint you want to add. Figure 6-15 shows the user interface for adding new unique constraints. The interface is pretty straightforward. You simply select the DataColumn object you want to include in the new unique key. You can also specify a name for the key and indicate whether this unique key should also be the DataTable object's primary key.

Figure 6-15

Adding a new UniqueConstraint to a DataTable

Figure 6-16 shows the user interface for adding new foreign key constraints. This interface should need no explanation because it's nearly identical to the one we used when we worked with the Data Form Wizard in Chapter 2 to build the relationship between the Customers and Orders tables in the DataSet.

Figure 6-16

Adding a new ForeignKeyConstraint to a DataTable



Microsoft ADO. NET Core Reference
Microsoft ADO.NET (Core Reference) (PRO-Developer)
ISBN: 0735614237
EAN: 2147483647
Year: 2002
Pages: 104
Authors: David Sceppa

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