ConstraintCollection.Add メソッド (String, DataColumn, DataColumn)
指定した名前、親列、および子列を使用して新しい ForeignKeyConstraint を作成し、この制約をコレクションに追加します。
Overloads Public Overridable Function Add( _
ByVal name As String, _ ByVal primaryKeyColumn As DataColumn, _ ByVal foreignKeyColumn As DataColumn _) As Constraint
[C#]
public virtual Constraint Add(stringname,DataColumnprimaryKeyColumn,DataColumnforeignKeyColumn);
[C++]
public: virtual Constraint* Add(String* name,DataColumn* primaryKeyColumn,DataColumn* foreignKeyColumn);
[JScript]
public function Add(
name : String,primaryKeyColumn : DataColumn,foreignKeyColumn : DataColumn) : Constraint;
パラメータ
- name
ForeignKeyConstraint の名前。 - primaryKeyColumn
主キーまたは親 DataColumn 。 - foreignKeyColumn
外部キーまたは子 DataColumn 。
戻り値
新しい ForeignKeyConstraint 。
解説
制約が正常に追加された場合は、 CollectionChanged イベントが発生します。
使用例
[Visual Basic, C#, C++] 新しい ForeignKeyConstraint を DataTable の ConstraintCollection に追加する例を次に示します。
Private Sub AddForeignConstraint(myDataSet As DataSet)
Try
Dim parentCol As DataColumn = _
myDataSet.Tables("Suppliers").Columns("SupplierID")
Dim childCol As DataColumn = _
myDataSet.Tables("Products").Columns("SupplierID")
myDataSet.Tables("Products").Constraints.Add _
("ProductsSuppliersCstr", parentCol, childCol)
Catch e As Exception
' In case the constraint already exists, catch the collision here and respond.
Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
log.Source = "My Application"
log.WriteEntry(e.ToString())
Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
End Try
End Sub
[C#]
private void AddForeignConstraint(DataSet myDataSet){
try{
DataColumn parentCol =
myDataSet.Tables["Suppliers"].Columns["SupplierID"];
DataColumn childCol =
myDataSet.Tables["Products"].Columns["SupplierID"];
myDataSet.Tables["Products"].Constraints.Add
("ProductsSuppliersCstr", parentCol, childCol);
}
catch(Exception e){
// In case the constraint already exists, catch the collision here and respond.
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = "My Application";
log.WriteEntry(e.ToString());
Console.WriteLine("Exception of type {0} occurred.", e.GetType());
}
}
[C++]
private:
void AddForeignConstraint(DataSet* myDataSet){
try{
DataColumn* parentCol =
myDataSet->Tables->Item[S"Suppliers"]->Columns->Item[S"SupplierID"];
DataColumn* childCol =
myDataSet->Tables->Item[S"Products"]->Columns->Item[S"SupplierID"];
myDataSet->Tables->Item[S"Products"]->Constraints->Add(S"ProductsSuppliersCstr", parentCol, childCol);
}
catch(Exception* e){
// In case the constraint already exists, catch the collision here and respond.
System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
log->Source = S"My Application";
log->WriteEntry(e->ToString());
Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: 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
参照
ConstraintCollection クラス | ConstraintCollection メンバ | System.Data 名前空間 | ConstraintCollection.Add オーバーロードの一覧