且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-04 10:52:58

这个问题通常是由以下之一引起的

This problem is usually caused by one of the following


  • 未设置为AllowDBNull的列返回的空值

  • 使用相同的主键返回的行重复。

  • 数据库和数据集之间的列定义不匹配(例如,字符字段的大小)

您的查询本地并查看结果,如果结果集不是太大。如果你已经消除了空值,那么我的猜测是主键列被重复。

Try running your query natively and look at the results, if the resultset is not too large. If you've eliminated null values, then my guess is that the primary key columns is being duplicated.

或者,要查看确切的错误,您可以手动添加一个Try / Catch块生成的代码,这样,然后打破时,异常生成:

Or, to see the exact error, you can manually add a Try/Catch block to the generated code like so and then breaking when the exception is raised:

然后在命令窗口中,调用 GetErrors 方法在表上得到错误。

对于C#,命令将是? dataTable.GetErrors()

对于VB,命令是? dataTable.GetErrors

Then within the command window, call GetErrors method on the table getting the error.
For C#, the command would be ? dataTable.GetErrors()
For VB, the command is ? dataTable.GetErrors

这将显示所有出现错误的数据行。你可以得到然后看看 RowError 为每一个,应该告诉你的列是无效的问题。所以,为了看到错误的第一个数据行错误的命令是:

? dataTable.GetErrors(0).RowError

或在C#中它将是? dataTable.GetErrors()[0] .RowError

This will show you all datarows which have an error. You can get then look at the RowError for each of these, which should tell you the column that's invalid along with the problem. So, to see the error of the first datarow in error the command is:
? dataTable.GetErrors(0).RowError
or in C# it would be ? dataTable.GetErrors()[0].RowError