ITPub博客

首页 > Linux操作系统 > Linux操作系统 > System.Data 的Constraint 类介绍

System.Data 的Constraint 类介绍

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-01-22 18:35:00 0 删除 编辑

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

命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)

Visual Basic(声明)
Public MustInherit Class Constraint
Visual Basic(用法)
Dim instance As Constraint
C#
public abstract class Constraint
C++
public ref class Constraint abstract
J#
public abstract class Constraint
JScript
public abstract class Constraint

约束是用于维护 DataTable 中的数据的完整性的规则。例如,当删除在一个或多个相关表中使用的值时,ForeignKeyConstraint 确定如何处理相关表中的值:同样删除、设置为空值、设置为默认值或者不执行任何操作。另一方面,UniqueConstraint 只是确保特定的表内的所有值都是唯一的。有关更多信息,请参见 将约束添加到表

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

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

Visual Basic
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
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) 
        {
            PrintForeigKeyConstraintProperties(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"Column Name: " 
            + columnArray[i].ColumnName);
    }
}
 
private void PrintForeigKeyConstraintProperties(
    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"Column Name: " 
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();
 
    // Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns ;
    for(int i = 0;i"Related Column Name: " 
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();
}

该类型对于多线程读操作是安全的。您必须使任何写操作同步。

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-157086/,如需转载,请注明出处,否则将追究法律责任。

上一篇: XmlComment 类介绍
请登录后发表评论 登录
全部评论

注册时间:2008-01-04

最新文章