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)

 

你可能感兴趣的:(constraints)