Creating aUniqueConstraintObject
In this section, you'll learn how to create aUniqueConstraint object. The
UniqueConstraint constructor is overloaded as follows:
UniqueConstraint(DataColumn myDataColumn)
UniqueConstraint(DataColumn[] myDataColumns)
UniqueConstraint(DataColumn myDataColumn, bool isPrimaryKey)
UniqueConstraint(DataColumn[] myDataColumns, bool isPrimaryKey)
UniqueConstraint(string constraintName, DataColumn myDataColumn)
UniqueConstraint(string constraintName, DataColumn[] myDataColumns)
UniqueConstraint(string constraintName, DataColumn myDataColumn, bool
isPrimaryKey)
UniqueConstraint(string constraintName, DataColumn[] myDataColumns,
bool isPrimaryKey)
UniqueConstraint(string constraintName, string[] columnNames,
bool isPrimaryKey)
where
• myDataColumn and myDataColumns are the DataColumn objects that you want
to ensure are unique.
• constraintName is the name you want to assign to the ConstraintName property of
your UniqueConstraint.
• isPrimaryKey indicates whether your UniqueConstraint is for a primary key.
Before creating and adding aUniqueConstraint to a DataTable, you first need a
DataTable. The following example creates and populates two DataTable objects named
customersDT and ordersDT (the ordersDT object will be used later in the section
"Creating a ForeignKeyConstraint Object
"):
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT CustomerID, CompanyName " +
"FROM Customers " +
"WHERE CustomerID = 'ALFKI'" +
"SELECT OrderID, CustomerID " +
"FROM Orders " +
"WHERE CustomerID = 'ALFKI';";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
mySqlDataAdapter.Fill(myDataSet);
mySqlConnection.Close();
myDataSet.Tables["Table"].TableName = "Customers";
myDataSet.Tables["Table1"].TableName = "Orders";
DataTable customersDT = myDataSet.Tables["Customers"];
DataTable ordersDT = myDataSet.Tables["Orders"];
The following example creates aUniqueConstraintobject on the CustomerID
DataColumn of the customersDT DataTable, and then adds this UniqueConstraint to
customersDT; notice that the third parameter to the UniqueConstraint constructor is set to
true, indicating that the constraint is for a primary key:
UniqueConstraint myUC =
new UniqueConstraint(
"UniqueConstraintCustomerID",
customersDT.Columns["CustomerID"],
true
);
customersDT.Constraints.Add(myUC);
N
ote To successfully add aUniqueConstraint to the DataColumn of a DataTable, the
D
ataColumn value in each DataRow object in the DataTable must be unique.
The final example retrieves the constraint just added to customersDT and displays its
properties:
myUC =
(UniqueConstraint) customersDT.Constraints["UniqueConstraintCustomerID"];
Console.WriteLine("Columns:");
foreach (DataColumn myDataColumn in myUC.Columns)
{
Console.WriteLine("" + myDataColumn);
}
Console.WriteLine("myUC.ConstraintName = " + myUC.ConstraintName);
Console.WriteLine("myUC.IsPrimaryKey = " + myUC.IsPrimaryKey);
Console.WriteLine("myUC.Table = " + myUC.Table);
This example displays the following output:
Columns:
CustomerID
myUC.ConstraintName = UniqueConstraintCustomerID
myUC.IsPrimaryKey = True
myUC.Table = Customers
The IsPrimaryKey property is true because the constraint is for a primary key; this was
specified in the constructor when the constraint was created earlier.
. 'ALFKI';";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();.
Before creating and adding a UniqueConstraint to a DataTable, you first need a
DataTable. The following example creates and populates two DataTable objects