C# 加载数据集时触发 ConstraintException 的原因是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/140161/
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
What triggers ConstraintException when loading DataSet?
提问by Dala
How can I find out which column and value is violating the constraint? The exception message isn't helpful at all:
如何找出违反约束的列和值?异常消息根本没有帮助:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
未能启用约束。一行或多行包含违反非空、唯一或外键约束的值。
采纳答案by Nikki9696
There is a property called RowError you can check.
您可以检查一个名为 RowError 的属性。
See http://dotnetdebug.net/2006/07/16/constraintexception-a-helpful-tip/
见http://dotnetdebug.net/2006/07/16/constrainexception-a-helpful-tip/
Edited to add this link showing iteration of rows to see which had errors.
编辑添加此链接显示行的迭代以查看哪些有错误。
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.adonet/topic58812.aspx
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.adonet/topic58812.aspx
回答by Joe
Like many people, I have my own standard data access components, which include methods to return a DataSet. Of course, if a ConstraintException is thrown, the DataSet isn't returned to the caller, so the caller can't check for row errors.
像许多人一样,我有自己的标准数据访问组件,其中包括返回 DataSet 的方法。当然,如果抛出 ConstraintException,DataSet 不会返回给调用者,因此调用者无法检查行错误。
What I've done is catch and rethrow ConstraintException in such methods, logging row error details, as in the following example (which uses Log4Net for logging):
我所做的是在此类方法中捕获并重新抛出 ConstraintException,记录行错误详细信息,如下例所示(使用 Log4Net 进行记录):
...
try
{
adapter.Fill(dataTable); // or dataSet
}
catch (ConstraintException)
{
LogErrors(dataTable);
throw;
}
...
private static void LogErrors(DataSet dataSet)
{
foreach (DataTable dataTable in dataSet.Tables)
{
LogErrors(dataTable);
}
}
private static void LogErrors(DataTable dataTable)
{
if (!dataTable.HasErrors) return;
StringBuilder sb = new StringBuilder();
sb.AppendFormat(
CultureInfo.CurrentCulture,
"ConstraintException while filling {0}",
dataTable.TableName);
DataRow[] errorRows = dataTable.GetErrors();
for (int i = 0; (i < MAX_ERRORS_TO_LOG) && (i < errorRows.Length); i++)
{
sb.AppendLine();
sb.Append(errorRows[i].RowError);
}
_logger.Error(sb.ToString());
}
回答by Jonathan Webb
回答by doekman
When you use a strong typed dataset and used the visual designer (xsd): to access tbl.Rows[0].RowErrorinformation, you need to create the Fillmethod.
当您使用强类型数据集并使用可视化设计器 (xsd): 访问tbl.Rows[0].RowError信息时,您需要创建Fill方法。
You can't use the Getmethod, since the DataTable is instanced within generated code.
您不能使用 Get方法,因为 DataTable 是在生成的代码中实例化的。
回答by Olivier de Rivoyre
For googlers who want a snippet to get more details on the ConstraintException:
对于想要片段以获取有关 ConstraintException 的更多详细信息的谷歌员工:
try
{
ds.EnforceConstraints = true;
}
catch (ConstraintException ex)
{
string details = string.Join("",
ds.Tables.Cast<DataTable>()
.Where(t => t.HasErrors)
.SelectMany(t => t.GetErrors())
.Take(50)
.Select(r => "\n - " + r.Table.TableName + "[" + string.Join(", ", r.Table.PrimaryKey.Select(c => r[c])) + "]: " + r.RowError));
throw new ConstraintException(ex.Message + details);
}