Bagikan melalui


Constraint Kelas

Definisi

Mewakili batasan yang dapat diberlakukan pada satu atau beberapa DataColumn objek.

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
Warisan
Constraint
Turunan
Atribut

Contoh

Contoh berikut memeriksa kumpulan batasan untuk dan DataTable menentukan apakah setiap batasan adalah UniqueConstraint atau ForeignKeyConstraint. Properti batasan kemudian ditampilkan.

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

Keterangan

Batasan adalah aturan yang digunakan untuk mempertahankan integritas data di DataTable. Misalnya, saat Anda menghapus nilai yang digunakan dalam satu atau beberapa tabel terkait, ForeignKeyConstraint menentukan apakah nilai dalam tabel terkait juga dihapus, diatur ke nilai null, diatur ke nilai default, atau apakah tidak ada tindakan yang terjadi. A UniqueConstraint, di sisi lain, hanya memastikan bahwa semua nilai dalam tabel tertentu unik. Untuk informasi selengkapnya, lihat Batasan DataTable.

Konstruktor dasar Constraint tidak digunakan. Batasan kunci primer atau unik dibuat dengan menggunakan UniqueConstraint konstruktor, dan batasan kunci asing dibuat dengan menggunakan ForeignKeyConstraint konstruktor.

Konstruktor

Constraint()

Menginisialisasi instans baru kelas Constraint.

Properti

_DataSet

Mendapatkan tempat DataSet batasan ini berada.

ConstraintName

Nama batasan di ConstraintCollection.

ExtendedProperties

Mendapatkan kumpulan properti batasan yang ditentukan pengguna.

Table

Mendapatkan batasan DataTable yang berlaku.

Metode

CheckStateForProperty()

Mendapatkan tempat DataSet batasan ini berada.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
SetDataSet(DataSet)

Mengatur batasan DataSet.

ToString()

ConstraintNameMendapatkan , jika ada, sebagai string.

Berlaku untuk

Keamanan Thread

Jenis ini aman untuk operasi baca multithreaded. Anda harus menyinkronkan operasi tulis apa pun.

Lihat juga