如何从 .NET 中的数据表中删除约束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8814840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove constraints from datatable in .NET?
提问by Ravi
I cannot set any constraints in my datatable if i merge two datatables dt1.Merge(dt2);It shows error like this Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.By default i view in visualizer it shows constraints count as 2. How to remove constraints from datatable and solve this error?
如果我合并两个数据表,我无法在我的数据表中设置任何约束dt1.Merge(dt2);它显示这样的错误Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.默认情况下,我在可视化器中查看它显示约束计数为 2。如何从数据表中删除约束并解决此错误?
回答by Peter Meinl
To remove constraints from a DataTable you can use the Clear method of the Constrains property:
要从 DataTable 中删除约束,您可以使用 Constrains 属性的 Clear 方法:
myDataTable.Constraints.Clear();
回答by Shoaib Shaikh
try iterating the constraints of datatable and remove them using
尝试迭代数据表的约束并使用删除它们
private void RemoveConstraint(DataTable table,
Constraint constraint)
{
if(table.Constraints.Contains(constraint.ConstraintName))
if(table.Constraints.CanRemove(constraint))
table.Constraints.Remove(constraint);
}
回答by Yemoku
Alternatively, you can also do
或者,你也可以做
New DataView(dt).ToTable
to create a copy of the table without constraints
无约束地创建表的副本
回答by VS1
EDIT:
编辑:
Ok this is more clear now: You can't remove constraints, they are disable during merge operation and then automatically enable after the merge if they can be enabled.
好的,现在更清楚了:您不能删除约束,它们在合并操作期间被禁用,如果可以启用,则在合并后自动启用。
As the error is like after the merge operation is completed, the Constraints can't be enabled due to some of the invalid values in target datatable. So, I think you would need to explicitly update the target datatable dt2 values before the merge operation starts. This can be done by watching the constraints in datatable dt1 and resolving those contraints in datatable dt2 with code (by updating the null or non-unique or foriegn key data in target datatable dt2); and then start the merge operation.
由于错误类似于合并操作完成后,由于目标数据表中的某些无效值,无法启用约束。因此,我认为您需要在合并操作开始之前显式更新目标数据表 dt2 值。这可以通过观察数据表 dt1 中的约束并用代码解决数据表 dt2 中的约束来完成(通过更新目标数据表 dt2 中的空或非唯一或外来键数据);然后开始合并操作。
The following excerpt and link from MSDN explains this while doing the Merge operation:
以下摘自 MSDN 的摘录和链接在执行合并操作时解释了这一点:
During a merge, constraints are disabled. If any constraints cannot be enabled at the end of merge, a ConstraintException is generated and the merged data is retained while the constraints are disabled. In this case, the EnforceConstraints property is set to false, and all rows that are invalid are marked in error. The errors must be resolved before attempting to reset the EnforceConstraints property to true.
在合并期间,约束被禁用。如果在合并结束时无法启用任何约束,则会生成 ConstraintException 并在禁用约束时保留合并的数据。在这种情况下,EnforceConstraints 属性设置为 false,所有无效行都标记为错误。在尝试将 EnforceConstraints 属性重置为 true 之前,必须解决这些错误。

