•
constraintName is the name you want to assign to your constraint.
•
isPrimaryKey indicates whether the constraint is a primary key constraint or just a
regular unique constraint.
The following example uses the Add() method to add a primary key constraint to the
Products DataTable:
myDataSet.Tables["Orders"].Constraints.Add(
"Primary key constraint",
myDataSet.Tables["Orders"].Columns["OrderID"],
true
);
This example does the same thing as the previous example that added the primary key
constraint using the PrimaryKey property. Notice the last parameter to the Add() method
is set to true, which indicates the constraint is for a primary key.
Just as an aside, if you have a column that isn't a primary key but is unique, you can add a
UniqueConstraint object to the ConstraintsCollection. For example:
UniqueConstraint myUC =
new UniqueConstraint(myDataTable.Columns["myColumn"]);
myDataTable.Constraints.Add(myUC);
Adding a Primary Key to the OrderDetails DataTable
Let's consider an example of setting the PrimaryKey property for the Order Details
DataTable. The primary for the Order Details table is made up of the OrderID and
ProductID columns, and the following example sets the PrimaryKey property of the
Order Details DataTable to these two columns:
myDataSet.Tables["Order Details"].PrimaryKey =
new DataColumn[]
{
myDataSet.Tables["Order Details"].Columns["OrderID"],
myDataSet.Tables["Order Details"].Columns["ProductID"]
};
see how to add foreign key constraints.
Adding Foreign Key Constraints to the Order Details DataTable
In this section, you'll see how to add a foreign key constraint to the Order Details
DataTable. To do this, you use the Add() method through the Constraints property of the
DataTable.
The following example adds a foreign key constraint from the OrderID DataColumn of
the Order Details DataTable to the OrderID DataColumn of the Orders DataTable:
ForeignKeyConstraint myFKC = new ForeignKeyConstraint(
myDataSet.Tables["Orders"].Columns["OrderID"],
myDataSet.Tables["Order Details"].Columns["OrderID"]
);
myDataSet.Tables["Order Details"].Constraints.Add(myFKC); Note Notice that the parent DataColumn (OrderID of Orders) is specified before the
child DataColumn (OrderID of Order Details).
The next example adds a foreign key constraint from the ProductID DataColumn of the
Order Details DataTable to the ProductID DataColumn of the Products DataTable:
myDataSet.Tables["Order Details"].Constraints.Add(
"Foreign key constraint to ProductID DataColumn of the " +
"Products DataTable",
myDataSet.Tables["Order Details"].Columns["ProductID"],
myDataSet.Tables["Products"].Columns["ProductID"]
);
That wraps up adding constraints to the DataTable objects. Next, you'll see how to add
restrictions to DataColumn objects.
Adding Restrictions to DataColumn Objects
In this section, you'll see how to add restrictions to the DataColumn objects stored in a
DataTable. Specifically, you'll see how to set the AllowDBNull, AutoIncrement,
AutoIncrementSeed, AutoIncrementStep, ReadOnly, and Unique properties of the
Listing 11.1: ADDRESTRICTIONS.CS
/*
AddRestrictions.cs illustrates how to add constraints to
DataTable objects and add restrictions to DataColumn objects
*/
using System;
using System.Data;
using System.Data.SqlClient;
class AddRestrictions
{
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT ProductID, ProductName " +
"FROM Products;" +
"SELECT OrderID " +
"FROM Orders;" +
"SELECT OrderID, ProductID, UnitPrice " +
"FROM [Order Details];";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
new DataColumn[]
{
myDataSet.Tables["Order Details"].Columns["OrderID"],
myDataSet.Tables["Order Details"].Columns["ProductID"]
},
true
);