System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
at System.Data.DataTable.EnableConstraints()
at System.Data.DataTable.set_EnforceConstraints(Boolean value)
at System.Data.DataTable.EndLoadData()

遇到这个错误 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()
int returnValue = ;
try
{
returnValue = this.Adapter.Fill(dataTable);
}
catch (System.Data.DataException e)
{
System.Data.DataRow[] rowsInError;
System.Text.StringBuilder sbError = new System.Text.StringBuilder();
// Test if the table has errors. If not, skip it.
if (dataTable.HasErrors)
{
// Get an array of all rows with errors.
rowsInError = dataTable.GetErrors();
// Print the error of each column in each row.
for (int i = ; i < rowsInError.Length; i++)
{
foreach (System.Data.DataColumn column in dataTable.Columns)
{
sbError.Append(column.ColumnName + " " + rowsInError[i].GetColumnError(column));
}
// Clear the row errors
rowsInError[i].ClearErrors();
}
}
string time = System.DateTime.Now.ToString();
var fileName = "Error.log";
string filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
System.IO.FileStream fst = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
System.IO.StreamWriter swt = new System.IO.StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
swt.WriteLine("======================");
swt.WriteLine(time);
swt.WriteLine("----------------------");
swt.WriteLine(sbError.ToString());
swt.WriteLine("----------------------");
swt.WriteLine(e.ToString());
swt.Close();
fst.Close();
}
return returnValue;

具体错误将显示在日志中

======================
2/27/2014 11:47:54 PM
----------------------
CustomerID ParentCustomerID CustomerNumber Column 'CustomerNumber' exceeds the MaxLength limit.Name StatementName Inactive OnHold EMailAddress TimeStamp ShippingMethodID PriceLevel TradeDiscount TaxExempt ClassID UserDefine1 UserDefine2 CreditLimitType TimeStamp1 ActivationCode
----------------------
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
at System.Data.DataTable.EnableConstraints()
at System.Data.DataTable.set_EnforceConstraints(Boolean value)
at System.Data.DataTable.EndLoadData()
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at Nodus.Applications.EPay.Data.EntitiesTableAdapters.CustomerTableAdapter.FillByKeyword(CustomerDataTable dataTable, String Keyword, Nullable`1 MaxResults)

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.的更多相关文章

  1. InnoDB和Foreign KEY Constraints

    InnoDB表中中Foreign Key定义 1. InnoDB允许a foreign key引用一个索引列或者索引组列. 2. InnoDB现在并不支持用户定义的分区表有foreign keys,这 ...

  2. 解决Windows8下Cisco Systems VPN Client的Reason 442: Failed to Enable Virtual Adapter错误

    Windows8下使用Cisco Systems VPN Client创建的Cisco IPSec VPN无法连接,提示Reason 442: Failed to Enable Virtual Ada ...

  3. WIN8 下Cisco VPN连接 出现vpn 422 failed to enable virtual adapter错误

    今天在家用VPN软件连接,出现了“vpn 422 failed to enable virtual adapter”的错误,系统安装的是Win8专业版32位,百度了半天又很多方法解决不了,后来发现了一 ...

  4. 解决Cisco VPN Client:Reason 442: Failed to Enable Virtual Adapter VPN连接问题

    大公司里肯定涉及不同地点的办公问题,这样VPN的使用就频繁了,今天遇到一个VPN连接问题,分享给大家,看一眼,以后不在这问题上耗费太多功夫. 在win7上连接vpn时抛出“failed to enab ...

  5. Windows 8 – Reason 442: Failed to enable Virtual Adapter

    Cisco VPN on Windows 8.1 – Reason 442: Failed to enable Virtual Adapter https://supertekboy.com/2013 ...

  6. Composer\Downloader\TransportException ... Failed to enable crypto,failed to open stream: operation failed

    failed to open stream: operation failed 错误详细信息: [Composer\Downloader\TransportException] The "h ...

  7. Foreign key (referential) constraints on DB2 LUW v105

    oreign key constraints (also known as referential constraints or referential integrity constraints) ...

  8. Azure CosmosDB (9) Unique Key Constraints

    <Windows Azure Platform 系列文章目录> 在Azure Cosmos DB中,还支持Unique Key Constraints(唯一键约束). 我们可以在Azure ...

  9. Sqlite: FOREIGN KEY constraint failed on remove的错误原因。

    sqlite外键约束中.restrict约束:如果要删除父表,则子表需空. 如果没有定义约束.会报错,需设置一个约束.

随机推荐

  1. 树莓派进阶之路 (019) - 树莓派通过filezilla,samba与PC文件共享(转)

    虽然我们可以很方便的通过ssh譬如putty或者vnc连接操控树莓派,但是毕竟树莓派资源没那么高,在上面编程,调试要吃力的多.所以还是想在pc上编程上传到树莓派或者最好,文件共享,可以直接读写共同的文 ...

  2. 在Linux CentOS 6.6上安装RedisLive

    Real time dashboard for redis 安装必须软件 1.安装pip到指定的python版本下面: curl -O https://bootstrap.pypa.io/get-pi ...

  3. Swift 类

    1.类概念 类是一种抽象的概念,它实际上并不存在于现实中的时间和空间里. 在定义一个类时,我们就会在类中定义该类事物的特性和行为,从术语上讲,特性叫类的属性,行为叫类的方法. 类是一个静态的概念,类本 ...

  4. Linux系统排查——CPU负载篇

    本随笔介绍CPU负载的排查手段. 查看系统负载的工具:uptime,w,都能查看系统负载,系统平均负载是处于运行或不可打扰状态的进程的平均数, 可运行:运行态,占用CPU,或就绪态,等待CPU调度. ...

  5. AndroidStudio编译错误:Error: null value in entry: blameLogFolder=null

    今天写项目的时候,电脑开了个WiFi热点,然后这个热点和window驱动不兼容,有时候会导致电脑重启,重启之后AndroidStudio编译就报错了, Error: null value in ent ...

  6. Java Nashorn--Part 2

    在命令行中执行 JavaScript 现在我们有一个 my_script.js 文件,想在 Nashorn 下执行,则需要在命令行下执行该命令: jrunscript my_script.js jru ...

  7. Springboot使用junit

    1. 首先要下载配套的spring-boot-starter-test包,注意版本要对应. compile("org.springframework.boot:spring-boot-sta ...

  8. Web用户控件开发--星型评分控件

    本文中分享一个实现简单,使用方便的星型评分控件. 一:贴几张测试图片先: 二.星型评分控件的实现: RatingBar.ascx: <%@ Control Language="C#&q ...

  9. Android Developers:从一个Activity获取结果

    启动其它Activity不是单向的.你也能启动其它Activity并获取一个返回结果.为了获取一个结果,调用startActivityForResult()方法(替代startActivity()方法 ...

  10. MySql(十一):MySQL性能调优——常用存储引擎优化

    一.前言 MySQL 提供的非常丰富的存储引擎种类供大家选择,有多种选择固然是好事,但是需要我们理解掌握的知识也会增加很多.本章将介绍最为常用的两种存储引擎进行针对性的优化建议. 二.MyISAM存储 ...