Export (0) Print
Expand All

Walkthrough: Adding Untyped Datasets to a Form or Component 

An untyped dataset is an instance of the DataSet class that you create in a form or component. Unlike a typed dataset, an untyped dataset is not an instance of a class generated using an XML Schema (.xsd) file. As a consequence, an untyped dataset has no inherent structure. You create the tables, columns, constraints, and DataRelation objects yourself at design time in the Properties window, at run time in code, or by allowing a data adapter's mappings to do so when you use the data adapter to fill the dataset.

NoteNote

Although an untyped dataset is not based on a class that incorporates a schema, after building the untyped dataset, you can export its structure to a schema file. For more information, see the WriteXmlSchema method.

Creating the Dataset

The instructions below describe how to create an untyped dataset using the Component Designer while editing a form or component. For more information about how to create an untyped dataset at run time in code, see Creating a DataSet and Adding a DataTable to a DataSet.

NoteNote

When you deploy an application that includes Visual Studio data-access components, you must make sure that the user installing the application has version 2.7 or later of the Microsoft Data Access Components (MDAC). For more information, see How to: Add a Launch Condition for Microsoft Data Access Components.

To create an untyped dataset at design time

  1. From the Data tab of the Toolbox, drag a DataSet onto the form or component you are working with.

    The Add Dataset dialog box is displayed.

  2. Select Untyped Dataset and click OK.

    A dataset is added to the form or component.

  3. If you want to give the dataset a new name, select it, open the Properties window, and edit the following two properties.

    • (Name)   This property sets the object name by which you refer to the dataset in code.

    • DataSetName   If you export the dataset to an XML document, this property sets the name of the dataset in the document (that is, the name of the topmost element). Setting this property can help avoid problems with name duplication or case sensitivity.

Adding Tables and Columns

You can use the Properties window to create the structure of the untyped dataset. The dataset includes a Tables property that is a collection of the DataTable objects defined for the dataset. Each table in turn supports a Columns property that is a collection of DataColumn objects. To create tables and columns, therefore, you use the collection-editor feature of the Properties window.

NoteNote

You cannot create records in an untyped dataset at design time; you can populate a dataset only at run time. The DataRowCollection collection of a dataset is therefore not available in the Properties window.

To add tables and columns to the dataset

  1. In the form or component, select the dataset and open the Properties window.

  2. In the Tables property, click the button to open the Tables Collection Editor dialog box.

  3. Click Add to create a new table. If you want to rename the table, select it and in the right pane of the properties grid, change the value of the (Name) property.

    NoteNote

    The names of all tables in all datasets in the form or component share the same namespace. Therefore, you cannot use a table name in one dataset if that name is in use in another dataset.

  4. To add columns to the table, do the following:

    1. Make sure the table you are working with is selected in the Members list, and then in the Properties grid on the right, click the button in the Columns property to display the Columns Collection Editor dialog box.

    2. Click Add to create a new column, and then set the new column's properties in the grid on the right. Typical properties to set are listed in the following table.

      Property Description

      AllowDBNull

      A Boolean value indicating whether the column can be left blank in a given record.

      ColumnName

      The name of the column in the table's DataTableCollection collection, which is how the column can be referenced in code.

      DataType

      A .NET Framework data type to be enforced for the column. If you import or export an XML document to or from this dataset, data types are automatically converted to schema types (for example, System.String is converted to xs:string and vice versa). Select a data type from the drop-down list.

      ReadOnly

      A Boolean value indicating whether a column can be changed.

      Unique

      A Boolean value indicating a unique constraint on the column. You always set this to true for a primary key column, but it can be set for any column.

    3. Repeat Step b for each column you want to add.

    4. When you are finished adding columns for the current table, click Close to close the Columns Collection Editor dialog box.

  5. To set a primary key for the table, use the list in the PrimaryKey property and select the check box next to each column that makes up the primary key.

  6. Repeat Steps 3 through 5 for each table and set of columns that you want to add to the dataset.

Adding Constraints

You can define two types of constraints in a dataset:

  • A unique constraint that checks that the new values in a column are unique in the table.

  • A foreign-key constraint that defines rules for how related child records should be updated when a record in a master table is updated or deleted.

To add constraints to an untyped dataset at design time

  1. In the form or component, select the dataset and open the Properties window.

  2. Click the button in the Tables property to open the Tables Collection Editor.

  3. Select the table to add a constraint to, and then click the button in the Constraints property to open the Constraints Collection Editor.

  4. Click Add and then select Unique Constraint or Foreign Key Constraint from the drop-down list.

    The Unique Constraint or Foreign Key Constraint dialog box is displayed. For help on filling in these dialog boxes, press F1.

Adding Relations

You can also create relationships (DataRelation objects) between tables in the dataset at design time. For more information about DataRelation objects, see Introduction to DataRelation Objects.

To add a DataRelation object to an untyped dataset at design time

  1. In the form or component, select the dataset and open the Properties window.

  2. In the Relations property, click the button to open the Relations Collection Editor dialog box.

  3. Click Add to create a new data relation.

    The Relation dialog box opens with default values for the new relationship.

  4. In the dialog box, set values according to the following table.

    Value Description

    Name

    The name by which you can refer to this DataRelation object in code. A typical name includes the names of both tables being related (for example, CustomersOrders).

    Parent table

    The table containing the parent records. Select a table from the drop-down list.

    Child table

    The table containing child records. Select a table from the drop-down list.

    Key Columns

    The names of the columns that make up the primary keys in the parent table. You can select column names from the drop-down list or type in a column name (for example, if you know the name of a column that will be defined at run time).

    Foreign Key Columns

    The names of the columns that make up the foreign keys in the child table.

    Update rule

    Delete rule

    Accept/reject rule

    The nature of the cascaded change that should be made in the child table when changes occur in the parent table. For more information about the settings for these values, see Introduction to DataRelation Objects.

  5. Click OK to save the changes to this relationship.

  6. Repeat Steps 3 through 5 for each relationship you want to create.

See Also

Community Additions

Show:
© 2014 Microsoft