MSDN Documentation

Microsoft Developer Network

Data Constraints in ADO.NET

Data constraints are rules that enforce data integrity within a dataset. In ADO.NET, these constraints are represented by classes that derive from the abstract Constraint class. The primary types of constraints you'll encounter are UniqueConstraint and ForeignKeyConstraint.

UniqueConstraint

A UniqueConstraint ensures that the values in one or more columns of a DataTable are unique. This is analogous to a unique index or primary key constraint in a relational database.

Key Properties:

Example:


DataTable customersTable = new DataTable("Customers");
DataColumn idColumn = customersTable.Columns.Add("CustomerID", typeof(int));
customersTable.Columns.Add("Name", typeof(string));

// Set CustomerID as the primary key
customersTable.Constraints.Add(new UniqueConstraint(idColumn, true));

// Add another unique constraint on Name (if needed)
// customersTable.Constraints.Add(new UniqueConstraint(new DataColumn[] { customersTable.Columns["Name"] }));
            

ForeignKeyConstraint

A ForeignKeyConstraint enforces referential integrity between two tables. It specifies that values in the foreign key column(s) of a child table must exist in the unique (or primary key) column(s) of a parent table. This prevents "orphan" records.

Key Properties:

Common Rule Actions:

Example:


DataTable ordersTable = new DataTable("Orders");
DataColumn orderIDColumn = ordersTable.Columns.Add("OrderID", typeof(int));
ordersTable.Columns.Add("CustomerID", typeof(int));
ordersTable.Columns.Add("OrderDate", typeof(DateTime));

// Assume customersTable and its UniqueConstraint for CustomerID already exist

// Add ForeignKeyConstraint
ForeignKeyConstraint fkConstraint = new ForeignKeyConstraint(
    customersTable.Columns["CustomerID"], // Parent column(s)
    ordersTable.Columns["CustomerID"]     // Child column(s)
);
fkConstraint.AcceptRejectRule = AcceptRejectRule.None;
fkConstraint.DeleteRule = Rule.Cascade; // If a customer is deleted, delete their orders
fkConstraint.UpdateRule = Rule.Cascade; // If a customer ID changes, update it in orders

ordersTable.Constraints.Add(fkConstraint);

// You would typically create a DataRelation to represent this relationship visually and programmatically.
DataRelation relation = new DataRelation("CustOrders",
    customersTable.Columns["CustomerID"],
    ordersTable.Columns["CustomerID"],
    false // This parameter controls whether to cascade deletes/updates; the ForeignKeyConstraint handles this more granularly.
);
// dataset.Relations.Add(relation); // If using a DataSet
            

Creating Constraints

Constraints are added to the Constraints collection of a DataTable. You can add them during table creation or later. It's generally recommended to define primary keys and foreign key relationships before populating the tables with data to ensure integrity from the start.

Benefits of Using Constraints

"Constraints are fundamental to maintaining the quality and reliability of your data within an ADO.NET DataTable."

By effectively using UniqueConstraint and ForeignKeyConstraint, you can build more robust and trustworthy data-driven applications.