• Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.


    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 = 0;
    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 = 0; 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)
  • 相关阅读:
    TCP/IP详解学习笔记
    python 中颜色的表示
    token工作原理
    sql注入相关链接记一次SQL注入实战
    python读写不同编码txt文件
    selenium+python 优化测试报告
    一个非常好的Linux学习网站
    jmeter 正则表达式 练习
    jmeter 实战项目总结2——微信端
    jmeter 实战项目总结1
  • 原文地址:https://www.cnblogs.com/vipsoft/p/3573853.html
Copyright © 2020-2023  润新知