Breyta

Deila með


ConstraintCollection Class

Definition

Represents a collection of constraints for a DataTable.

public ref class ConstraintCollection sealed : System::Data::InternalDataCollectionBase
public ref class ConstraintCollection : System::Data::InternalDataCollectionBase
public sealed class ConstraintCollection : System.Data.InternalDataCollectionBase
[System.Serializable]
public class ConstraintCollection : System.Data.InternalDataCollectionBase
type ConstraintCollection = class
    inherit InternalDataCollectionBase
[<System.Serializable>]
type ConstraintCollection = class
    inherit InternalDataCollectionBase
Public NotInheritable Class ConstraintCollection
Inherits InternalDataCollectionBase
Public Class ConstraintCollection
Inherits InternalDataCollectionBase
Inheritance
ConstraintCollection
Attributes

Examples

The first example creates a DataTable, and adds a DataColumn (with its Unique property set to true) to the DataColumnCollection. The second example creates a DataSet, two DataTable objects, four columns, and a DataRelation. The count of constraints is then printed to show that a ForeignKeyConstraint and a UniqueConstraint are created when a DataRelation is added to the DataSet object's DataRelationCollection.

private void MakeTableWithUniqueConstraint()
{
    DataTable table = new DataTable("table");
    DataColumn column = new DataColumn("UniqueColumn");
    column.Unique=true;
    table.Columns.Add(column);

    // Print count, name, and type.
    Console.WriteLine("Constraints.Count "
        + table.Constraints.Count);
    Console.WriteLine(table.Constraints[0].ConstraintName);
    Console.WriteLine(table.Constraints[0].GetType() );

    // Add a second unique column.
    column = new DataColumn("UniqueColumn2");
    column.Unique=true;
    table.Columns.Add(column);

    // Print info again.
    Console.WriteLine("Constraints.Count "
        + table.Constraints.Count);
    Console.WriteLine(table.Constraints[1].ConstraintName);
    Console.WriteLine(table.Constraints[1].GetType() );
}

private void MakeTableWithForeignConstraint()
{
    // Create a DataSet.
    DataSet dataSet = new DataSet("dataSet");

    // Make two tables.
    DataTable customersTable= new DataTable("Customers");
    DataTable ordersTable = new DataTable("Orders");

    // Create four columns, two for each table.
    DataColumn name = new DataColumn("Name");
    DataColumn id = new DataColumn("ID");
    DataColumn orderId = new DataColumn("OrderID");
    DataColumn cDate = new DataColumn("OrderDate");

    // Add columns to tables.
    customersTable.Columns.Add(name);
    customersTable.Columns.Add(id);
    ordersTable.Columns.Add(orderId);
    ordersTable.Columns.Add(cDate);

    // Add tables to the DataSet.
    dataSet.Tables.Add(customersTable);
    dataSet.Tables.Add(ordersTable);

    // Create a DataRelation for two of the columns.
    DataRelation myRelation = new
        DataRelation("CustomersOrders",id,orderId,true);
    dataSet.Relations.Add(myRelation);

    // Print TableName, Constraints.Count,
    // ConstraintName and Type.
    foreach(DataTable t in dataSet.Tables)
    {
        Console.WriteLine(t.TableName);
        Console.WriteLine("Constraints.Count "
            + t.Constraints.Count);
        Console.WriteLine("ParentRelations.Count "
            + t.ParentRelations.Count);
        Console.WriteLine("ChildRelations.Count "
            + t.ChildRelations.Count);
        foreach(Constraint cstrnt in t.Constraints)
        {
            Console.WriteLine(cstrnt.ConstraintName);
            Console.WriteLine(cstrnt.GetType());
        }
    }
}
Private Sub MakeTableWithUniqueConstraint()

    Dim table As New DataTable("table")
    Dim column As New DataColumn("UniqueColumn")
    column.Unique = True
    table.Columns.Add(column)

    ' Print count, name, and type.
    Console.WriteLine("Constraints.Count " _
       + table.Constraints.Count.ToString())
    Console.WriteLine(table.Constraints(0).ConstraintName)
    Console.WriteLine( _
        table.Constraints(0).GetType().ToString())

    ' Add a second unique column.
    column = New DataColumn("UniqueColumn2")
    column.Unique = True
    table.Columns.Add(column)

    ' Print info again.
    Console.WriteLine("Constraints.Count " _
       + table.Constraints.Count.ToString())
    Console.WriteLine(table.Constraints(1).ConstraintName)
    Console.WriteLine( _
        table.Constraints(1).GetType().ToString())
End Sub

Private Sub MakeTableWithForeignConstraint()

    ' Create a DataSet.
    Dim dataSet As New DataSet("dataSet")

    ' Make two tables.
    Dim customersTable As New DataTable("Customers")
    Dim ordersTable As New DataTable("Orders")

    ' Create four columns, two for each table.
    Dim name As New DataColumn("Name")
    Dim id As New DataColumn("ID")
    Dim orderId As New DataColumn("OrderID")
    Dim orderDate As New DataColumn("OrderDate")
    
    ' Add columns to tables.
    customersTable.Columns.Add(name)
    customersTable.Columns.Add(id)
    ordersTable.Columns.Add(orderId)
    ordersTable.Columns.Add(orderDate)
    
    ' Add tables to the DataSet.
    dataSet.Tables.Add(customersTable)
    dataSet.Tables.Add(ordersTable)

    ' Create a DataRelation for two of the columns.
    Dim myRelation As New DataRelation _
       ("CustomersOrders", id, orderId, True)
    dataSet.Relations.Add(myRelation)

    ' Print TableName, Constraints.Count, 
    ' ConstraintName and Type.
    Dim t As DataTable
    For Each t In  dataSet.Tables
        Console.WriteLine(t.TableName)
        Console.WriteLine("Constraints.Count " _
           + t.Constraints.Count.ToString())
        Console.WriteLine("ParentRelations.Count " _
           + t.ParentRelations.Count.ToString())
        Console.WriteLine("ChildRelations.Count " _
           + t.ChildRelations.Count.ToString())
        Dim cstrnt As Constraint
        For Each cstrnt In  t.Constraints
            Console.WriteLine(cstrnt.ConstraintName)
            Console.WriteLine(cstrnt.GetType())
        Next cstrnt
    Next t
End Sub

Remarks

The ConstraintCollection is accessed through the DataTable.Constraints property.

The ConstraintCollection can contain both UniqueConstraint and ForeignKeyConstraint objects for the DataTable. A UniqueConstraint object makes sure that data in a specific column is always unique to preserve the data integrity. The ForeignKeyConstraint determines what will occur in related tables when data in the DataTable is either updated or deleted. For example, if a row is deleted, the ForeignKeyConstraint will determine whether the related rows are also deleted (a cascade), or some other course of action.

Note

When you add a DataRelation that creates a relationship between two tables to a DataSet, both a ForeignKeyConstraint and a UniqueConstraint are created automatically. The UniqueConstraint is applied to the primary key column in the parent DataTable, and the constraint is added to that table's ConstraintCollection. The ForeignKeyConstraint is applied to the primary key column and the foreign key column, and the constraint is added to the child table's ConstraintCollection.

The ConstraintCollection uses standard collection methods such as Add, Clear, and Remove. In addition, the Contains method can be used to look for the existence of a particular constraint in the collection.

A UniqueConstraint is created when a DataColumn with its Unique property set to true is added to a DataTable object's DataColumnCollection.

A ForeignKeyConstraint is created when a DataRelation is added to a DataSet object's DataRelationCollection.

Properties

Count

Gets the total number of elements in a collection.

(Inherited from InternalDataCollectionBase)
IsReadOnly

Gets a value that indicates whether the InternalDataCollectionBase is read-only.

(Inherited from InternalDataCollectionBase)
IsSynchronized

Gets a value that indicates whether the InternalDataCollectionBase is synchronized.

(Inherited from InternalDataCollectionBase)
Item[Int32]

Gets the Constraint from the collection at the specified index.

Item[String]

Gets the Constraint from the collection with the specified name.

List

Gets the items of the collection as a list.

List

Gets the items of the collection as a list.

(Inherited from InternalDataCollectionBase)
SyncRoot

Gets an object that can be used to synchronize the collection.

(Inherited from InternalDataCollectionBase)

Methods

Add(Constraint)

Adds the specified Constraint object to the collection.

Add(String, DataColumn, Boolean)

Constructs a new UniqueConstraint with the specified name, DataColumn, and value that indicates whether the column is a primary key, and adds it to the collection.

Add(String, DataColumn, DataColumn)

Constructs a new ForeignKeyConstraint with the specified name, parent column, and child column, and adds the constraint to the collection.

Add(String, DataColumn[], Boolean)

Constructs a new UniqueConstraint with the specified name, array of DataColumn objects, and value that indicates whether the column is a primary key, and adds it to the collection.

Add(String, DataColumn[], DataColumn[])

Constructs a new ForeignKeyConstraint, with the specified arrays of parent columns and child columns, and adds the constraint to the collection.

AddRange(Constraint[])

Copies the elements of the specified ConstraintCollection array to the end of the collection.

CanRemove(Constraint)

Indicates whether a Constraint can be removed.

Clear()

Clears the collection of any Constraint objects.

Contains(String)

Indicates whether the Constraint object specified by name exists in the collection.

CopyTo(Array, Int32)

Copies all the elements of the current InternalDataCollectionBase to a one-dimensional Array, starting at the specified InternalDataCollectionBase index.

(Inherited from InternalDataCollectionBase)
CopyTo(Constraint[], Int32)

Copies the collection objects to a one-dimensional Array instance starting at the specified index.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

Gets an IEnumerator for the collection.

(Inherited from InternalDataCollectionBase)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(Constraint)

Gets the index of the specified Constraint.

IndexOf(String)

Gets the index of the Constraint specified by name.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnCollectionChanged(CollectionChangeEventArgs)

Raises the CollectionChanged event.

Remove(Constraint)

Removes the specified Constraint from the collection.

Remove(String)

Removes the Constraint object specified by name from the collection.

RemoveAt(Int32)

Removes the Constraint object at the specified index from the collection.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Events

CollectionChanged

Occurs whenever the ConstraintCollection is changed because of Constraint objects being added or removed.

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

See also