Constraint クラス
1 つ以上の DataColumn オブジェクトに強制的に適用できる制約を表します。
この型のすべてのメンバの一覧については、Constraint メンバ を参照してください。
System.Object
System.Data.Constraint
System.Data.ForeignKeyConstraint
System.Data.UniqueConstraint
<Serializable>
MustInherit Public Class Constraint
[C#]
[Serializable]
public abstract class Constraint
[C++]
[Serializable]
public __gc __abstract class Constraint
[JScript]
public
Serializable
abstract class Constraint
スレッドセーフ
この型は、マルチスレッド読み取り操作に対して安全です。すべての書き込み操作の同期をとる必要があります。
解説
制約は、 DataTable 内のデータの整合性を保持するための規則です。たとえば、1 つ以上の関連テーブルで使用されている値を削除する場合に、関連テーブル内のその値も削除するか、null 値に設定するか、既定値に設定するか、またはアクションを何も実行しないかを ForeignKeyConstraint によって決定します。これに対して、 UniqueConstraint は、特定のテーブル内のすべての値が一意であるようにする制約です。詳細については、「 テーブルへの制約の追加 」を参照してください。
基本 Constraint コンストラクタは使用されません。主キー制約または一意キー制約を作成するには UniqueConstraint コンストラクタを使用し、外部キー制約を作成するには ForeignKeyConstraint コンストラクタを使用します。
使用例
[Visual Basic, C#, C++] DataTable の制約コレクションをチェックし、各制約が UniqueConstraint または ForeignKeyConstraint のどちらであるかを確認する例を次に示します。制約のプロパティが次に表示されます。
Private Sub GetConstraints(myTable As DataTable)
Console.WriteLine()
' Print the table's name.
Console.WriteLine("TableName: " + myTable.TableName)
' Iterate through the collection and print each name and type value.
Dim cs As Constraint
For Each cs In myTable.Constraints
Console.WriteLine("Constraint Name: " + cs.ConstraintName)
Console.WriteLine("Type: " + cs.GetType().ToString())
' If the constraint is a UniqueConstraint, print its properties
' using a function below.
If TypeOf cs Is UniqueConstraint Then
PrintUniqueConstraintProperties(cs)
End If
' If the constraint is a ForeignKeyConstraint, print its properties
' using a function below.
If TypeOf cs Is ForeignKeyConstraint Then
PrintForeigKeyConstraintProperties(cs)
End If
Next cs
End Sub
Private Sub PrintUniqueConstraintProperties(cs As Constraint)
Dim uCS As UniqueConstraint
uCS = CType(cs, UniqueConstraint)
' Get the Columns as an array.
Dim colArray() As DataColumn
colArray = uCS.Columns
' Print each column's name.
Dim i As Integer
For i = 0 To colArray.Length - 1
Console.WriteLine("Column Name: " + colArray(i).ColumnName)
Next i
End Sub
Private Sub PrintForeigKeyConstraintProperties(cs As Constraint)
Dim fkCS As ForeignKeyConstraint
fkCS = CType(cs, ForeignKeyConstraint)
' Get the Columns as an array.
Dim colArray() As DataColumn
colArray = fkCS.Columns
' Print each column's name.
Dim i As Integer
For i = 0 To colArray.Length - 1
Console.WriteLine("Column Name: " + colArray(i).ColumnName)
Next i
Console.WriteLine()
' Get the related columns and print each columns name.
colArray = fkCS.RelatedColumns
For i = 0 To colArray.Length - 1
Console.WriteLine("Related Column Name: " + colArray(i).ColumnName)
Next i
Console.WriteLine()
End Sub
[C#]
private void GetConstraints(DataTable myTable){
Console.WriteLine();
// Print the table's name.
Console.WriteLine("TableName: " + myTable.TableName);
// Iterate through the collection and print each name and type value.
foreach(Constraint cs in myTable.Constraints ) {
Console.WriteLine("Constraint Name: " + cs.ConstraintName);
Console.WriteLine("Type: " + cs.GetType().ToString());
// If the constraint is a UniqueConstraint, print its properties using
// a function below.
if(cs is UniqueConstraint) {
PrintUniqueConstraintProperties(cs);
}
// If the constraint is a ForeignKeyConstraint, print its properties
// using a function below.
if(cs is ForeignKeyConstraint) {
PrintForeigKeyConstraintProperties(cs);
}
}
}
private void PrintUniqueConstraintProperties(Constraint cs){
UniqueConstraint uCS;
uCS = (UniqueConstraint) cs;
// Get the Columns as an array.
DataColumn[] colArray;
colArray = uCS.Columns;
// Print each column's name.
for(int i = 0;i<colArray.Length ;i++) {
Console.WriteLine("Column Name: " + colArray[i].ColumnName);
}
}
private void PrintForeigKeyConstraintProperties(Constraint cs){
ForeignKeyConstraint fkCS;
fkCS = (ForeignKeyConstraint) cs;
// Get the Columns as an array.
DataColumn[] colArray;
colArray = fkCS.Columns;
// Print each column's name.
for(int i = 0;i<colArray.Length ;i++) {
Console.WriteLine("Column Name: " + colArray[i].ColumnName);
}
Console.WriteLine();
// Get the related columns and print each columns name.
colArray = fkCS.RelatedColumns ;
for(int i = 0;i<colArray.Length ;i++) {
Console.WriteLine("Related Column Name: " + colArray[i].ColumnName);
}
Console.WriteLine();
}
[C++]
private:
void GetConstraints(DataTable* myTable){
Console::WriteLine();
// Print the table's name.
Console::WriteLine(S"TableName: {0}", myTable->TableName);
// Iterate through the collection and print each name and type value.
System::Collections::IEnumerator* myEnum = myTable->Constraints->GetEnumerator();
while (myEnum->MoveNext())
{
Constraint* cs = __try_cast<Constraint*>(myEnum->Current);
Console::WriteLine(S"Constraint Name: {0}", cs->ConstraintName);
Console::WriteLine(S"Type: {0}", cs->GetType());
// If the constraint is a UniqueConstraint, print its properties using
// a function below.
if(dynamic_cast<UniqueConstraint*>(cs)) {
PrintUniqueConstraintProperties(cs);
}
// If the constraint is a ForeignKeyConstraint, print its properties
// using a function below.
if(dynamic_cast<ForeignKeyConstraint*>(cs)) {
PrintForeigKeyConstraintProperties(cs);
}
}
}
void PrintUniqueConstraintProperties(Constraint* cs){
UniqueConstraint* uCS;
uCS = dynamic_cast<UniqueConstraint*> (cs);
// Get the Columns as an array.
DataColumn* colArray[];
colArray = uCS->Columns;
// Print each column's name.
for(int i = 0;i<colArray->Length ;i++) {
Console::WriteLine(S"Column Name: {0}", colArray[i]->ColumnName);
}
}
void PrintForeigKeyConstraintProperties(Constraint* cs){
ForeignKeyConstraint* fkCS;
fkCS = dynamic_cast<ForeignKeyConstraint*> (cs);
// Get the Columns as an array.
DataColumn* colArray[];
colArray = fkCS->Columns;
// Print each column's name.
for(int i = 0;i<colArray->Length ;i++) {
Console::WriteLine(S"Column Name: {0}", colArray[i]->ColumnName);
}
Console::WriteLine();
// Get the related columns and print each columns name.
colArray = fkCS->RelatedColumns ;
for(int i = 0;i<colArray->Length ;i++) {
Console::WriteLine(S"Related Column Name: {0}", colArray[i]->ColumnName);
}
Console::WriteLine();
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Data
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: System.Data (System.Data.dll 内)
参照
Constraint メンバ | System.Data 名前空間 | ConstraintCollection | Constraints | ForeignKeyConstraint | UniqueConstraint