Constraint 클래스

정의

하나 이상의 DataColumn 개체에 적용할 수 있는 제약 조건을 나타냅니다.Represents a constraint that can be enforced on one or more DataColumn objects.

public ref class Constraint abstract
public abstract class Constraint
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
public abstract class Constraint
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
[System.Serializable]
public abstract class Constraint
type Constraint = class
Public MustInherit Class Constraint
상속
Constraint
파생
특성

예제

다음 예에서는 DataTable에 대 한 제약 조건의 컬렉션을 검사 하 고 각 제약 조건이 UniqueConstraint 인지 ForeignKeyConstraint여부를 결정 합니다.The following example examines the collection of constraints for a DataTable and determines whether each constraint is a UniqueConstraint or a ForeignKeyConstraint. 제약 조건의 속성 표시 됩니다.The properties of the constraint are then displayed.

   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<columnArray.Length ;i++) 
       {
           Console.WriteLine("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<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데이터의 무결성을 유지 하는 데 사용 되는 규칙입니다.A constraint is a rule used to maintain the integrity of the data in the DataTable. 예를 들어 하나 이상의 관련 테이블에 사용 되는 값을 삭제 하는 경우 관련 테이블의 값도 삭제 되는지, null 값으로 설정 되거나, 기본값으로 설정 되는지 또는 동작이 발생 하지 않는지를 결정 ForeignKeyConstraint.For example, when you delete a value that is used in one or more related tables, a ForeignKeyConstraint determines whether the values in the related tables are also deleted, set to null values, set to default values, or whether no action occurs. 반면에 UniqueConstraint는 특정 테이블의 모든 값이 고유한 지를 확인할 수 있습니다.A UniqueConstraint, on the other hand, just makes sure that all values within a particular table are unique. 자세한 내용은 DataTable 제약 조건을 참조 하세요.For more information, see DataTable Constraints.

기본 Constraint 생성자는 사용 되지 않습니다.A base Constraint constructor is not used. Primary 또는 unique key 제약 조건은 UniqueConstraint 생성자를 사용 하 여 만들어지며, foreign key 제약 조건은 ForeignKeyConstraint 생성자를 사용 하 여 만듭니다.Primary or unique key constraints are created by using the UniqueConstraint constructor, and foreign key constraints are created by using the ForeignKeyConstraint constructor.

생성자

Constraint()

Constraint 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the Constraint class.

속성

_DataSet

이 제약 조건이 속한 DataSet를 가져옵니다.Gets the DataSet to which this constraint belongs.

ConstraintName

ConstraintCollection의 제약 조건 이름입니다.The name of a constraint in the ConstraintCollection.

ExtendedProperties

사용자 정의 제약 조건 속성의 컬렉션을 가져옵니다.Gets the collection of user-defined constraint properties.

Table

제약 조건이 적용될 DataTable을 가져옵니다.Gets the DataTable to which the constraint applies.

메서드

CheckStateForProperty()

이 제약 조건이 속한 DataSet를 가져옵니다.Gets the DataSet to which this constraint belongs.

Equals(Object)

지정한 개체가 현재 개체와 같은지를 확인합니다.Determines whether the specified object is equal to the current object.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(다음에서 상속됨 Object)
SetDataSet(DataSet)

제약 조건의 DataSet을 설정합니다.Sets the constraint's DataSet.

ToString()

ConstraintName이 있는 경우 문자열로 가져옵니다.Gets the ConstraintName, if there is one, as a string.

적용 대상

스레드 보안

이 유형은 다중 스레드 읽기 작업에 안전 합니다.This type is safe for multithreaded read operations. 모든 쓰기 작업을 동기화 해야 합니다.You must synchronize any write operations.

추가 정보