2.9. Creating a Foreign Key Constraint

Problem

You need to create a foreign key constraint on one or more columns in a table to implement referential integrity between parent and child tables.

Solution

Use the System.Data.ForeignConstraint class.

The solution creates a DataSet containing two tables and adds a foreign key constraint. Rows are added to both parent and child tables, including a row that violates the constraint. The results are output to the console.

The C# code in Program.cs in the project CreateForeignKeyConstraint is shown in Example 2-9.

Example 2-9. File: Program.cs forCreateForeignKeyConstraint solution

using System;
using System.Data;

namespace CreateForeignKeyConstraint
{
    class Program
    {
        static void
...