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

Commenti

Un vincolo è una regola utilizzata per mantenere l'integrità dei dati nell'oggetto DataTable.A constraint is a rule used to maintain the integrity of the data in the DataTable.Ad esempio, quando si elimina un valore utilizzato in una o più tabelle correlate, un oggetto ForeignKeyConstraint determina se vengono eliminati anche i valori nelle tabelle correlate, impostare su valori null, impostare i valori predefiniti o se non si verifica alcuna azione.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.Un UniqueConstraintoggetto, d'altra parte, garantisce solo che tutti i valori all'interno di una determinata tabella siano univoci.A UniqueConstraint, on the other hand, just makes sure that all values within a particular table are unique.Per altre informazioni, vedere vincoli DataTable.For more information, see DataTable Constraints.