在填充datatable时无法启用约束 [英] Failed to enable constraints when filling datatable

查看:596
本文介绍了在填充datatable时无法启用约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MSVS 2012中的C#中的XSD数据集中加载数据表。标准的 GetData()方法正常运行。但是,我的自定义查询 GetDataCustom()(仅返回数据集中的几个字段)会引发普遍的异常:

I am loading a data table from an XSD data set in C# in MSVS 2012. The standard GetData() method runs properly. However, my custom query GetDataCustom(), which returns only a few fields from the data set, throws the popular exception:


无法启用约束。一行或多行包含违反非空,唯一或外键约束的值

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

我使用了详细约束异常发布在SOV上的类似问题的回覆中,其中显示了这个细节:

I used the Detailed Constraint Exception posted in reply to a similar issue here on SOV, which showed this detail:

Error filling table
No Row Errors reported in DataTable=[datatable_Custom]

我设置 EnforceConstraints = false 在我的数据集,但异常仍然抛出。在这个问题上,我已经看过很多关于SOV的问题,但是其他的修复都没有帮助。还可以尝试什么?

I set EnforceConstraints = false on my data set, but the exceptions still were thrown. I have looked through the many questions on SOV on this issue, but none of the other fixes helped. What else can I try?

编辑添加代码块:

ds_Sample dsSample = new ds_Sample();
dsSample.Clear();
dsSample.EnforceConstraints = false;
ds_SampleTableAdapters.ta_Sample taCustom = new ds_SampleTableAdapters.ta_Custom();
ds_Sample.dt_CustomDataTable dtCustom = taCustom.GetDataCustom();//throws exception


推荐答案

自己修复:表适配器具有数据集中的所有字段,但是我的查询只有表适配器中的某些字段。我更改了表适配器以使用感兴趣的查询,并且问题已解决。

Fixed it myself: The table adapter had all the fields in the data set, but my query only had some of the fields in the table adapter. I changed the table adapter to use the query of interest, and the problem was resolved.

因此,约束违规很可能是中的空数据NOT NULL 字段。

Therefore, the constraint violation was most likely null data in NOT NULL fields.

这篇关于在填充datatable时无法启用约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆