Export (0) Print
Expand All

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

NoteNote

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

  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.

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.

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.

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

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

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.

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.

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.

To save the project

  • On the File menu, click Save All.

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

Community Additions

ADD
Show:
© 2014 Microsoft