zoukankan      html  css  js  c++  java
  • Failed to enable constraints. One or more rows contain values violating nonnull, unique, or foreignkey 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

  • 相关阅读:
    UnitTest测试套件及运行器
    DDT实现数据驱动
    MySQL练习题部分答案(未完待续)
    day58自我回顾版
    Linux 下安装pip
    wget用法汇总
    Linux基础操作整理
    pip安装django失败
    利用"SQL"语句自动生成序号的两种方式
    Python2.*与Python3.*共存问题
  • 原文地址:https://www.cnblogs.com/emanlee/p/2686064.html
Copyright © 2011-2022 走看看