Walkthrough: Creating a Relationship between Data Tables
This walkthrough explains how to create two data tables without TableAdapters using the Dataset Designer and creating a relationship between them. For information on creating data tables that include TableAdapters, see How to: Create TableAdapters. For more information on TableAdapters, see TableAdapter Overview.
Tasks illustrated in this walkthrough include:
Creating a new Windows application
Adding a new dataset to the application
Adding two new data tables to the dataset
Adding columns to the data tables
Setting the primary key for the tables
Creating a relationship between the tables
Note: |
|---|
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings. |
To create a new Windows Application project
-
From the File menu, create a new project.
-
Select a programming language in the Project Types pane and create a Windows Application.
-
Name the project RelationshipWalkthrough, and then click OK.
Visual Studio will add the project to Solution Explorer and display Form1 in the Windows Forms Designer.
To add a new Dataset item to the project
-
From the Project menu, choose Add New Item.
The Add New Item Dialog Box appears.
-
From the Templates area select DataSet.
-
Click Add.
Visual Studio will add a file called Dataset1.xsd to the project and open it in the Dataset Designer.
To add a new data table to the dataset
-
Drag a DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.
A table named DataTable1 is added to the dataset.
-
Click the title bar of DataTable1 and rename it Customers.
-
Drag a second DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.
A table named DataTable1 is added to the dataset.
-
Click the title bar of DataTable1 and rename it Orders.
The relationship is created between the common column from each table — in this case, the CustomerID column.
To configure a new relationship between the Customers and Orders tables
-
Drag a Relation object from the DataSet tab of the Toolbox onto the Orders table.
The Relation Dialog Box opens.
-
In the Parent Table box, select Customers.
-
In the Child Table box, select Orders.
-
In the Columns box, set Key Columns to CustomerID.
-
In the Columns box, set Foreign Key Columns to CustomerID.
-
Click OK to create the relationship; a relation line appears on the designer between the two tables.
-
On the Data menu, choose Show Relation Labels.
Note: