DataSet.EnforceConstraints Propiedad
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene o establece un valor que indica si se siguen las reglas de restricción al intentar realizar cualquier operación de actualización.
public:
property bool EnforceConstraints { bool get(); void set(bool value); };
public bool EnforceConstraints { get; set; }
[System.Data.DataSysDescription("DataSetEnforceConstraintsDescr")]
public bool EnforceConstraints { get; set; }
member this.EnforceConstraints : bool with get, set
[<System.Data.DataSysDescription("DataSetEnforceConstraintsDescr")>]
member this.EnforceConstraints : bool with get, set
Public Property EnforceConstraints As Boolean
true
si se aplican reglas; de lo contrario, false
. De manera predeterminada, es true
.
- Atributos
No se pueden forzar una o más restricciones.
En el ejemplo siguiente se crea un DataSet con una tabla, una columna, cinco filas y una UniqueConstraint. La EnforceConstraints propiedad se establece false
en y los valores de cada fila se establecen en el mismo valor. Cuando la EnforceConstraints propiedad se restablece a true
, se genera .ConstraintException
private void DemonstrateEnforceConstraints()
{
// Create a DataSet with one table, one column and
// a UniqueConstraint.
DataSet dataSet= new DataSet("dataSet");
DataTable table = new DataTable("table");
DataColumn column = new DataColumn("col1");
// A UniqueConstraint is added when the Unique
// property is true.
column.Unique=true;
table.Columns.Add(column);
dataSet.Tables.Add(table);
Console.WriteLine("constraints.count: " +
table.Constraints.Count);
// add five rows.
DataRow row ;
for(int i=0;i<5;i++)
{
row = table.NewRow();
row["col1"] = i;
table.Rows.Add(row);
}
table.AcceptChanges();
dataSet.EnforceConstraints=false;
// Change the values of all rows to 1.
foreach(DataRow thisRow in table.Rows)
{
thisRow["col1"]=1;
//Console.WriteLine("\table" + thisRow[0]);
}
try
{
dataSet.EnforceConstraints=true;
}
catch(System.Data.ConstraintException e)
{
// Process exception and return.
Console.WriteLine("Exception of type {0} occurred.",
e.GetType());
}
}
Private Sub DemonstrateEnforceConstraints()
' Create a DataSet with one table, one column and
' a UniqueConstraint.
Dim dataSet As New DataSet("dataSet")
Dim table As New DataTable("table")
Dim column As New DataColumn("col1")
column.Unique = True
table.Columns.Add(column)
dataSet.Tables.Add(table)
Console.WriteLine("constraints.count: " _
& table.Constraints.Count)
' add five rows.
Dim row As DataRow
Dim i As Integer
For i = 0 To 4
row = table.NewRow()
row("col1") = i
table.Rows.Add(row)
Next
table.AcceptChanges()
dataSet.EnforceConstraints = False
' Change the values of all rows to 1.
Dim thisRow As DataRow
For Each thisRow In table.rows
thisRow("col1") = 1
Next
Try
dataSet.EnforceConstraints = True
Catch e As System.Data.ConstraintException
' Process exception and return.
Console.WriteLine("Exception of type {0} occurred.", _
e.GetType().ToString())
End Try
End Sub
Las restricciones se establecen en el DataTable nivel (Constraints propiedad ). Para obtener más información sobre cómo crear restricciones, vea Restricciones de DataTable.
Producto | Versiones |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Comentarios de .NET
.NET es un proyecto de código abierto. Seleccione un vínculo para proporcionar comentarios: