Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
遇到这个错误 Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
请参考msdn文章来debug找出错误的地方: http://msdn.microsoft.com/en-us/library/system.data.datatable.geterrors(VS.71).aspx
它检查DataSet中的所有DataTable的HasErrors属性,然后对有错误的DataTable使用GetErrors方法。
GetErrors返回一组DataRows,检查返回的所有DataRow的RowError属性来获得确切的错误信息。
解决方法:
DataTable dataTable=new DataTable();
try
{
SqlDataAdapter.Fill(dataTable); //这行会出错,先catch住
}
catch
{
//remove datatable errors
DataRow[] rowsInError;
if (dataTable.HasErrors)
{
// Get an array of all rows with errors.
rowsInError = dataTable.GetErrors();
// Print the error of each column in each row.
StringBuilder sbError = new StringBuilder();
for (int i = 0; i < rowsInError.Length; i++)
{
foreach (DataColumn myCol in dataTable.Columns)
{
sbError.Append(myCol.ColumnName + " " + rowsInError[i].GetColumnError(myCol));
}
// Clear the row errors
rowsInError[i].ClearErrors(); //设置断点,然后运行时观察其中错误
}
}
}
http://www.cnblogs.com/liuzhendong/archive/2010/03/04/1678503.html
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 分享4款.NET开源、免费、实用的商城系统
2009-09-15 英语听力的技巧