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
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.
Creating a New Windows Application
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.
Adding a New Dataset to the Application
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.
Adding Two New DataTables to the Dataset
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.
Adding Columns to the Customers DataTable
To add columns to the Customers table
Right-click the Customers table.
Point to Add, and then click Column.
Name the column CustomerID.
In the Properties window, set the DataType property to Int16.
Repeat this process to add the following columns:
Column
DataType Property
CompanyName
Contact
ContactTitle
Adding Columns to the Orders DataTable
To add columns to the Orders table
Right-click the Orders table.
Point to Add, and then click Column.
Name the column OrderID.
In the Properties window, set the DataType property to Int16.
Repeat this process to add the following columns:
Column
DataType Property
CustomerID
OrderDate
ShippedDate
Setting the Primary Key for the Customers Table
The unique identifying column for the Customers table is the CustomerID column.
To set the primary key of the Customers table
Click the Customers table to select it.
Right-click the CustomerID column, and then click Set Primary Key on the shortcut menu.
Setting the Primary Key for the Orders Table
The unique identifying column for the Orders table is the OrderID column.
To set the primary key of the Orders table
Click the Orders table to select it.
Right-click the OrderID column, and then click Set Primary Key on the shortcut menu.
Creating the Relationship Between the Tables
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.
Saving Your Project
To save the project
- On the File menu, click Save All.
Next Steps
Now that you have created the related tables, you might want to perform one of the following actions:
To |
See |
---|---|
Add data to the table |
|
View data in a table |
|
Edit data |
|
Delete a row from a table |
See Also
Reference
Concepts
Preparing Your Application to Receive Data
Editing Data in Your Application