private void SetConstraint ( DataSet myDataSet, string table1,
string table2, string column1, string column2 ) {
// declare parent column and child column variables.
DataColumn parentColumn;
DataColumn childColumn;
ForeignKeyConstraint myFKeyConst;
// set parent and child column variables.
parentColumn = myDataSet.Tables [ Table1 ].Columns [ Column1 ];
childColumn = myDataSet.Tables [ Table2 ].Columns [ Column2 ];
myFKeyConst = new ForeignKeyConstraint
( "SupplierFKeyConst", parentColumn, childColumn );
// set null values when a value is deleted.
myFKeyConst.DeleteRule = Rule.SetNull;
myFKeyConst.UpdateRule = Rule.Cascade;
myFKeyConst.AcceptRejectRule = AcceptRejectRule.Cascade;
// add the constraint, and set EnforceConstraints to true.
myDataSet.Tables [ Table1 ].Constraints.Add ( myFKeyConst );
myDataSet.EnforceConstraints = true;
}
Private Sub SetConstraint ( myDataSet As DataSet, table1 As String, _
table2 As String, column1 As String, column2 As String )
' declare parent column and child column variables.
Dim parentColumn As DataColumn
Dim childColumn As DataColumn
Dim myFKeyConst As ForeignKeyConstraint
' set parent and child column variables.
parentColumn = myDataSet.Tables ( table1 ).Columns ( column1 )
childColumn = myDataSet.Tables ( table2 ).Columns ( column2 )
myFKeyConst = New ForeignKeyConstraint _
( "SupplierFKeyConst", parentColumn, childColumn )
' set null values when a value is deleted.
myFKeyConst.DeleteRule = Rule.SetNull
myFKeyConst.UpdateRule = Rule.Cascade
myFKeyConst.AcceptRejectRule = AcceptRejectRule.Cascade
' add the constraint, and set EnforceConstraints to true.
myDataSet.Tables ( table1 ).Constraints.Add ( myFKeyConst )
myDataSet.EnforceConstraints = True
End Sub |