RAD Studio for Microsoft .NET
ContentsIndex
PreviousUpNext
Using Standard DataSets

The standard DataSet provides an in-memory representation of one or more tables or views retrieved from a connected data source. Because of the level of indirection used in coding the underlying data structure, you are only able to see the column names from your data source at runtime. When you generate a DataSet, it retrieves everything you specified in your Select statement in the Data Adapter Configuration dialog. You can limit your columns by changing the Select statement and creating a new DataSet.

To use DataSets

  1. Generate a DataSet.
  2. Add multiple tables to a DataSet.
  3. Define primary keys for DataTables in the DataSet.
  4. Define column properties for your DataSet columns.
  5. Define constraints for your columns.
  6. Define relationships between tables in your DataSet.

To generate a DataSet

  1. From the Data Explorer, select a data source.
  2. Drill down in the tree, then drag and drop the name of a table onto your Windows Form or Web Form. This creates the BdpDataAdapter and BdpConnection for that data source and displays icons for those objects in the Component Tray.
    Note: You can also drag a data source only onto the form, rather than a table, but in that case, RAD Studio creates only a connection object for you. You must still create and configure the BdpDataAdapter object explicitly.
  3. Click the BdpDataAdapter icon (named bdpDataAdapter1, by default) to select it.
  4. Click the Configure Data Adapter designer verb in the Designer Verb area at the bottom of the Object Inspector. This displays the Data Adapter Configuration dialog.
  5. If the SQL statement that is pre-filled on the dialog is acceptable, click the DataSet tab, otherwise, modify the SQL statement, then click the DataSet tab.
  6. Select the New DataSet radio button.
    Tip: You can accept the default name or change the name of the DataSet.
  7. Click OK to generate the DataSet. A DataSet icon appears in the Component Tray indicating that your DataSet has been created.
    Note: By reviewing the code for the DataSet in the Code Editor, you can see that the columns are defined as generic dataColumns, whose columnName properties are assigned the value of the column name from the database table. This differs from how a typed DataSet is constructed, wherein the object name is constructed from the actual database column name, rather than assigned as a property value.

To add multiple tables to one DataSet

  1. From the Data Explorer, select a data source.
  2. Drill down in the tree, then drag and drop the names of multiple tables, one at a time, onto your Windows Form or Web Form. This creates the BdpDataAdapter for each table and one BdpConnection for that data source and displays icons for those objects in the Component Tray.
  3. Click the BdpDataAdapter icon (named bdpDataAdapter1, by default) to select it.
  4. Click the Configure Data Adapter designer verb in the Designer Verb area at the bottom of the Object Inspector. This displays the Data Adapter Configuration dialog.
  5. If the SQL statement that is pre-filled on the dialog is acceptable, click the DataSet tab, otherwise, modify the SQL statement, then click the DataSet tab.
  6. Select the New DataSet radio button.
    Tip: You can accept the default name or change the name of the DataSet.
  7. Click OK to generate the DataSet. A DataSet icon appears in the Component Tray indicating that your DataSet has been created.
  8. Repeat the Data Adapter configuration for each of the other data adapters, but select Existing Data Set on the DataSet tab when generating the DataSets for all data adapters except the first one you configure. This generates a DataTable for each data adapter and stores them all in one DataSet.
    Note: It is also possible to generate multiple DataSets, either one for each data adapter, or combinations of DataTables.

To define primary keys for each DataTable in the DataSet

  1. Select each data adapter in turn and set the Active property under Live Data in the Object Inspector to True.
  2. Select the DataSet in the Component Tray.
  3. In the Object Inspector, in the Tables property, click the ellipsis button. This displays the Tables Collection Editor. If you have set all of the data adapters' Active properties to True, the Tables Collection Editor will contain one member for each DataTable stored in the corresponding DataSet.
  4. Select a table from the members list.
  5. In the Primary Key field in the Table Properties, click on the DataColumn[] entry to display a pop-up list of column names.
  6. Click the gray check box next to the column name of the column or columns that comprise the Primary Key. The number 1 appears in the gray check box when selected.
  7. Define Column properties and Constraints for your Primary Key columns.

To define column properties for your DataSet columns

  1. In the Tables Collection Editor, click the (Collections) entry next to Columns in the Table Properties pane. This displays the Columns Collection Editor for the selected column.
  2. Set the property values for the individual columns.
  3. Repeat the process for each column.

To define constraints for your columns

  1. In the Tables Collection Editor, click the (Collections) entry next to Constraints in the Table Properties pane. This displays the Constraints Collection Editor for the selected column.
  2. Click Add to add either a Unique Constraint or a Primary Key Constraint.
  3. If you selected Unique Constraint, the Unique Constraint dialog appears. Select one or more of the displayed column names. You can also select the Primary Key check box if you want to set the column as a primary key. By setting the Unique Constraint on a column, you are enforcing the rule that all values in the column must be unique. This is useful for columns that contain identification numbers, such as employee numbers, social security numbers, part numbers, and so on.
    Note: If you have already defined a primary-foreign key relationship between two tables, you may not be able to set a column as a primary key, based on the fact that it may already be set as the primary key, or based on a conflict with another relationship.
  4. If you selected Foreign Key Constraint, the Foreign Key Constraint dialog appears. Select the tables you want to relate by choosing them from the Parent table and Child table drop down lists.
  5. Click Key Columns to select the primary key column from the list.
  6. Click Foreign Key Columns to select the foreign key column from the list.
    Warning: The primary key and foreign key columns must have the same data type and must contain unique values. Columns that can contain duplicates are not good choices for primary or foreign keys. It is common to choose the same column name from each table for your primary-foreign key relationship.

To define relationships between tables in the DataSet

  1. Once you have defined primary keys for each DataTable, select the DataSet in the Component Tray if it is not already selected.
  2. Click the ellipsis button next to the Relations property in the Object Inspector. This displays the blank Relations Collection Editor dialog.
  3. Click Add. This displays the Relation editor dialog
  4. From the Parent table and Child table dropdown lists, choose the tables you want to relate.
  5. Click the Key Columns field to choose a Primary Key column from the list of column names from the parent table.
  6. Click the Foreign Key Columns field to choose a Foreign Key column from the list of column names from the child table.
    Note: If you have already performed this procedure while setting constraints for your DataTables, you may find that all of the appropriate values are already established.
    Warning: The primary key and foreign key columns must have the same data type and must contain unique values. Columns that can contain duplicates are not good choices for primary or foreign keys. It is common to choose the same column name from each table for your primary-foreign key relationship.
  7. Click OK.
  8. Repeat the process to define additional relations between the same DataTables.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!