Constraint Constraint Constraint Constraint Class

Definición

Representa una restricción que se puede forzar en uno o más objetos DataColumn.Represents a constraint that can be enforced on one or more DataColumn objects.

public ref class Constraint abstract
[System.ComponentModel.TypeConverter(typeof(System.Data.ConstraintConverter))]
[System.Serializable]
public abstract class Constraint
type Constraint = class
Public MustInherit Class Constraint
Herencia
ConstraintConstraintConstraintConstraint
Derivado
Atributos

Ejemplos

En el ejemplo siguiente DataTable UniqueConstraint se examina la colección de restricciones de un y se determina si cada restricción es o ForeignKeyConstraint.The following example examines the collection of constraints for a DataTable and determines whether each constraint is a UniqueConstraint or a ForeignKeyConstraint. A continuación, se muestran las propiedades de la restricción.The properties of the constraint are then displayed.

private void GetConstraints(DataTable dataTable)
{
    Console.WriteLine();

    // Print the table's name.
    Console.WriteLine("TableName: " + dataTable.TableName);

    // Iterate through the collection and 
    // print each name and type value.
    foreach(Constraint 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(constraint is UniqueConstraint) 
        {
            PrintUniqueConstraintProperties(constraint);
        }
        // If the constraint is a ForeignKeyConstraint, 
        // print its properties using a function below.
        if(constraint is ForeignKeyConstraint) 
        {
            PrintForeigKeyConstraintProperties(constraint);
        }
    }
}

private void PrintUniqueConstraintProperties(
    Constraint constraint)
{
    UniqueConstraint uniqueConstraint;
    uniqueConstraint = (UniqueConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = uniqueConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++) 
    {
        Console.WriteLine("Column Name: " 
            + columnArray[i].ColumnName);
    }
}

private void PrintForeigKeyConstraintProperties(
    Constraint constraint)
{
    ForeignKeyConstraint fkConstraint;
    fkConstraint = (ForeignKeyConstraint) constraint;

    // Get the Columns as an array.
    DataColumn[] columnArray;
    columnArray = fkConstraint.Columns;

    // Print each column's name.
    for(int i = 0;i<columnArray.Length ;i++) 
    {
        Console.WriteLine("Column Name: " 
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();

    // Get the related columns and print each columns name.
    columnArray = fkConstraint.RelatedColumns ;
    for(int i = 0;i<columnArray.Length ;i++) 
    {
        Console.WriteLine("Related Column Name: " 
            + columnArray[i].ColumnName);
    }
    Console.WriteLine();
}
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

Comentarios

Una restricción es una regla que se usa para mantener la integridad de los datos DataTableen.A constraint is a rule used to maintain the integrity of the data in the DataTable. Por ejemplo, cuando se elimina un valor que se usa en una o varias tablas relacionadas, un ForeignKeyConstraint determina si los valores de las tablas relacionadas también se eliminan, se establecen en valores NULL, se establecen valores predeterminados o si no se produce ninguna acción.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. A UniqueConstraint, por otro lado, solo se asegura de que todos los valores de una tabla determinada son únicos.A UniqueConstraint, on the other hand, just makes sure that all values within a particular table are unique. Para obtener más información, vea restricciones de DataTable.For more information, see DataTable Constraints.

No se Constraint utiliza un constructor base.A base Constraint constructor is not used. Las restricciones PRIMARY KEY o Unique se crean mediante el UniqueConstraint constructor, y las restricciones Foreign Key se crean mediante el ForeignKeyConstraint constructor.Primary or unique key constraints are created by using the UniqueConstraint constructor, and foreign key constraints are created by using the ForeignKeyConstraint constructor.

Constructores

Constraint() Constraint() Constraint() Constraint()

Inicializa una nueva instancia de la clase Constraint.Initializes a new instance of the Constraint class.

Propiedades

ConstraintName ConstraintName ConstraintName ConstraintName

Nombre de una restricción en el ConstraintCollection.The name of a constraint in the ConstraintCollection.

ExtendedProperties ExtendedProperties ExtendedProperties ExtendedProperties

Obtiene la colección de propiedades de restricción definidas por el usuario.Gets the collection of user-defined constraint properties.

Table Table Table Table

Obtiene el DataTable al que se aplica la restricción.Gets the DataTable to which the constraint applies.

_DataSet _DataSet _DataSet _DataSet

Obtiene el objeto DataSet al que pertenece esta restricción.Gets the DataSet to which this constraint belongs.

Métodos

CheckStateForProperty() CheckStateForProperty() CheckStateForProperty() CheckStateForProperty()

Obtiene el objeto DataSet al que pertenece esta restricción.Gets the DataSet to which this constraint belongs.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina si el objeto especificado es igual al objeto actual.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Sirve como la función hash predeterminada.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Obtiene el Type de la instancia actual.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficial del objeto Object actual.Creates a shallow copy of the current Object.

(Inherited from Object)
SetDataSet(DataSet) SetDataSet(DataSet) SetDataSet(DataSet) SetDataSet(DataSet)

Establece el objeto DataSet de la restricción.Sets the constraint's DataSet.

ToString() ToString() ToString() ToString()

Obtiene el ConstraintName, si existe, como una cadena.Gets the ConstraintName, if there is one, as a string.

Se aplica a

Seguridad para subprocesos

Este tipo es seguro para las operaciones de lectura multiproceso.This type is safe for multithreaded read operations. Debe sincronizar cualquier operación de escritura.You must synchronize any write operations.

Consulte también: