Constraint 类

定义

表示可在一个或多个 DataColumn 对象上强制执行的约束。

public ref class Constraint abstract
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
public abstract class Constraint
public abstract class Constraint
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
[System.Serializable]
public abstract class Constraint
[<System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))>]
type Constraint = class
type Constraint = class
[<System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))>]
[<System.Serializable>]
type Constraint = class
Public MustInherit Class Constraint
继承
Constraint
派生
属性

示例

以下示例检查 的约束 DataTable 集合,并确定每个约束是 还是 UniqueConstraintForeignKeyConstraint。 然后显示约束的属性。

private void GetConstraints(DataTable dataTable)
{
    Console.WriteLine();

    // Print the table's name.
    Console.WriteLine("TableName: " + dataTable.TableName);

    // Iterate through the collection and
    // print each name and type value.
    foreach(Constraint constraint in dataTable.Constraints )
    {
        Console.WriteLine("Constraint Name: "
            + constraint.ConstraintName);
        Console.WriteLine("Type: "
            + constraint.GetType().ToString());

        // If the constraint is a UniqueConstraint,
        // print its properties using a function below.
        if(constraint is UniqueConstraint)
        {
            PrintUniqueConstraintProperties(constraint);
        }
        // If the constraint is a ForeignKeyConstraint,
        // print its properties using a function below.
        if(constraint is ForeignKeyConstraint)
        {
            PrintForeignKeyConstraintProperties(constraint);
        }
    }
}

private void PrintUniqueConstraintProperties(
    Constraint constraint)
{
    UniqueConstraint uniqueConstraint;
    uniqueConstraint = (UniqueConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = uniqueConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++)
    {
        Console.WriteLine("Column Name: "
            + columnArray[i].ColumnName);
    }
}

private void PrintForeignKeyConstraintProperties(
    Constraint constraint)
{
    ForeignKeyConstraint fkConstraint;
    fkConstraint = (ForeignKeyConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = fkConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++)
    {
        Console.WriteLine("Column Name: "
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();

    // Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns ;
    for(int i = 0;i<columnArray.Length ;i++)
    {
        Console.WriteLine("Related Column Name: "
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();
}
Private Sub GetConstraints(dataTable As DataTable)
    Console.WriteLine()

    ' Print the table's name.
    Console.WriteLine("TableName: " & dataTable.TableName)

    ' Iterate through the collection and print 
    ' each name and type value.
    Dim constraint As Constraint
    For Each constraint In  dataTable.Constraints
        Console.WriteLine("Constraint Name: " _
            & constraint.ConstraintName)
        Console.WriteLine("Type: " _
            & constraint.GetType().ToString())

        ' If the constraint is a UniqueConstraint, 
        ' print its properties using a function below.
        If TypeOf constraint Is UniqueConstraint Then
            PrintUniqueConstraintProperties(constraint)
        End If

        ' If the constraint is a ForeignKeyConstraint, 
        ' print its properties using a function below.
        If TypeOf constraint Is ForeignKeyConstraint Then
            PrintForeigKeyConstraintProperties(constraint)
        End If
    Next constraint
End Sub

Private Sub PrintUniqueConstraintProperties( _
    constraint As Constraint)

    Dim uniqueConstraint As UniqueConstraint
    uniqueConstraint = CType(constraint, UniqueConstraint)

    ' Get the Columns as an array.
    Dim columnArray() As DataColumn
    columnArray = uniqueConstraint.Columns

    ' Print each column's name.
    Dim i As Integer
    For i = 0 To columnArray.Length - 1
        Console.WriteLine("Column Name: " _
            & columnArray(i).ColumnName)
    Next i
End Sub

Private Sub PrintForeigKeyConstraintProperties( _
    constraint As Constraint)

    Dim fkConstraint As ForeignKeyConstraint
    fkConstraint = CType(constraint, ForeignKeyConstraint)
    
    ' Get the Columns as an array.
    Dim columnArray() As DataColumn
    columnArray = fkConstraint.Columns
    
    ' Print each column's name.
    Dim i As Integer
    For i = 0 To columnArray.Length - 1
        Console.WriteLine("Column Name: " _
            & columnArray(i).ColumnName)
    Next i
    Console.WriteLine()
    
    ' Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns
    For i = 0 To columnArray.Length - 1
        Console.WriteLine("Related Column Name: " _
            & columnArray(i).ColumnName)
    Next i
    Console.WriteLine()
End Sub

注解

约束是用于维护 中数据完整性的规则 DataTable。 例如,删除在一个或多个相关表中使用的值时, ForeignKeyConstraint 将确定相关表中的值是否也被删除、设置为 null 值、设置为默认值,或者是否不执行任何操作。 另一 UniqueConstraint方面,只需确保特定表中的所有值都是唯一的。 有关详细信息,请参阅DataTable 约束

不使用基 Constraint 构造函数。 主键或唯一键约束是使用 UniqueConstraint 构造函数创建的,外键约束是使用 ForeignKeyConstraint 构造函数创建的。

构造函数

Constraint()

初始化 Constraint 类的新实例。

属性

_DataSet

获取此约束所属的 DataSet

ConstraintName

ConstraintCollection 中的约束的名称。

ExtendedProperties

获取用户定义的约束属性的集合。

Table

获取向其应用约束的 DataTable

方法

CheckStateForProperty()

获取此约束所属的 DataSet

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
SetDataSet(DataSet)

设置约束的 DataSet

ToString()

获取 ConstraintName(如果存在)作为字符串。

适用于

线程安全性

此类型对于多线程读取操作是安全的。 必须同步所有写入操作。

另请参阅