Share via


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 Customizing Development Settings in Visual Studio.

Creating a New Windows Application

To create a new Windows Application project

  1. From the File menu, create a new project.

  2. Select a programming language in the Project Types pane and create a Windows Application.

  3. 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

  1. From the Project menu, choose Add New Item.

    The Add New Item Dialog Box appears.

  2. From the Templates area select DataSet.

  3. 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

  1. Drag a DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.

    A table named DataTable1 is added to the dataset.

  2. Click the title bar of DataTable1 and rename it Customers.

  3. Drag a second DataTable from the DataSet tab of the Toolbox onto the Dataset Designer.

    A table named DataTable1 is added to the dataset.

  4. Click the title bar of DataTable1 and rename it Orders.

Adding Columns to the Customers DataTable

To add columns to the Customers table

  1. Right-click the Customers table.

  2. Point to Add, and then click Column.

  3. Name the column CustomerID.

  4. In the Properties window, set the DataType property to Int16.

  5. Repeat this process to add the following columns:

    Column

    DataType Property

    CompanyName

    String

    Contact

    String

    ContactTitle

    String

Adding Columns to the Orders DataTable

To add columns to the Orders table

  1. Right-click the Orders table.

  2. Point to Add, and then click Column.

  3. Name the column OrderID.

  4. In the Properties window, set the DataType property to Int16.

  5. Repeat this process to add the following columns:

    Column

    DataType Property

    CustomerID

    Int16

    OrderDate

    DateTime

    ShippedDate

    DateTime

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

  1. Click the Customers table to select it.

  2. 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

  1. Click the Orders table to select it.

  2. 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

  1. Drag a Relation object from the DataSet tab of the Toolbox onto the Orders table.

    The Relation dialog box opens.

  2. In the Parent Table box, select Customers.

  3. In the Child Table box, select Orders.

  4. In the Columns box, set Key Columns to CustomerID.

  5. In the Columns box, set Foreign Key Columns to CustomerID.

  6. Click OK to create the relationship; a relation line appears on the designer between the two tables.

  7. 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

Adding Data to a Table

View data in a table

Viewing Data in a Table

Edit data

Editing Data in a Table

Delete a row from a table

Deleting a Row from a Table

See Also

Reference

GetChildRows

GetParentRow

Concepts

Preparing Your Application to Receive Data

Editing Data in Your Application

Validating Data

Saving Data

Other Resources

Creating and Using DataTables

Connecting to Data in Visual Studio

Data Walkthroughs