Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
2012-09-15 10:50
363 查看
遇到这个错误 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(); //设置断点,然后运行时观察其中错误
}
}
}
/article/4878503.html
相关文章推荐
- 源码调试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.
- 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.
- Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
- DataTable - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
- DataSet hell - "Failed to enable constraints. One or more rows contain values...."
- dell装机bios问题 os install mode enable failed to detect one or more drives during post
- bug笔记 - Spring --- Tomcat 报错: SEVERE: One or more listeners failed to start.
- android的imageView出现failed to instantiate one or more classes
- One or more listeners failed to start
- One or more Filters failed to start. Full details will be found in the appropriate container log fil
- 【报错】项目启动,仅仅报错 One or more listeners failed to start. Full details will be found in the appropriate container log file
- Cannot change version of project facet Dynamic Web Module to 2.5. 和 One or more constraints have not
- 关于One or more listeners failed to start的问题排查
- arcgis的One or more layers failed to draw: 问题
- Failed to instantiate one or more classes
- Geoserver fails One or more listeners failed to start
- Validation failed for one or more entities while saving changes to SQL Server Database
- 关于Struts2报错严重: One or more Filters failed to start. Full details will be found in the appropriate co