DataTable.Constraints Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene la raccolta di vincoli gestiti da questa tabella.
public:
property System::Data::ConstraintCollection ^ Constraints { System::Data::ConstraintCollection ^ get(); };
[System.Data.DataSysDescription("DataTableConstraintsDescr")]
public System.Data.ConstraintCollection Constraints { get; }
public System.Data.ConstraintCollection Constraints { get; }
[<System.Data.DataSysDescription("DataTableConstraintsDescr")>]
member this.Constraints : System.Data.ConstraintCollection
member this.Constraints : System.Data.ConstraintCollection
Public ReadOnly Property Constraints As ConstraintCollection
Valore della proprietà
Oggetto ConstraintCollection contenente l'insieme di Constraint oggetti per la tabella. Se non Constraint esistono oggetti, viene restituita una raccolta vuota.
- Attributi
Esempio
Nell'esempio seguente viene aggiunto un oggetto ForeignKeyConstraint alla raccolta di vincoli.
private void CreateConstraint(DataSet dataSet,
string table1, string table2, string column1, string column2)
{
ForeignKeyConstraint idKeyRestraint = new
ForeignKeyConstraint(dataSet.Tables[table1].Columns[column1],
dataSet.Tables[table2].Columns[column2]);
// Set null values when a value is deleted.
idKeyRestraint.DeleteRule = Rule.SetNull;
idKeyRestraint.UpdateRule = Rule.Cascade;
// Set AcceptRejectRule to cascade changes.
idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade;
dataSet.Tables[table1].Constraints.Add(idKeyRestraint);
dataSet.EnforceConstraints = true;
}
Private Sub CreateConstraint(dataSet As DataSet, _
table1 As String, table2 As String, _
column1 As String, column2 As String)
Dim idKeyRestraint As ForeignKeyConstraint = _
New ForeignKeyConstraint _
(dataSet.Tables(table1).Columns(column1), _
dataSet.Tables(table2).Columns(column2))
' Set null values when a value is deleted.
idKeyRestraint.DeleteRule = Rule.SetNull
idKeyRestraint.UpdateRule = Rule.Cascade
' Set AcceptRejectRule to cascade changes.
idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade
dataSet.Tables(table1).Constraints.Add(idKeyRestraint)
dataSet.EnforceConstraints = True
End Sub
Commenti
Un ForeignKeyConstraint oggetto limita l'azione eseguita quando un valore in una colonna (o colonne) viene eliminato o aggiornato. Tale vincolo deve essere usato con le colonne chiave primaria. In una relazione padre/figlio tra due tabelle, l'eliminazione di un valore dalla tabella padre può influire sulle righe figlio in uno dei modi seguenti.
Le righe figlio possono essere eliminate anche (un'azione a catena).
I valori nella colonna figlio (o colonne) possono essere impostati su valori Null.
I valori nella colonna figlio (o colonne) possono essere impostati sui valori predefiniti.
È possibile generare un'eccezione.
Un oggetto UniqueConstraint diventa attivo quando si tenta di impostare un valore in una chiave primaria su un valore non univoco.