简体   繁体   English

DataTable.Load,一行或多行包含违反非空、唯一或外键约束的值

[英]DataTable.Load, One or more rows contain values violating non-null, unique, or foreign-key constraints

I searched a lot, but could not find the solution.我搜索了很多,但找不到解决方案。

I get the error:我收到错误:

Failed to enable constraints.无法启用约束。 One or more rows contain values violating non-null, unique, or foreign-key constraints.一行或多行包含违反非空、唯一或外键约束的值。

I run DataTable.GetErrors() and see that some columns are set to Not NULL in SQL Compact Edition Database.我运行DataTable.GetErrors()并看到一些列在 SQL 精简版数据库中设置为Not NULL And these columns are used in LEFT OUTER JOIN query so they are null when the query is run.这些列在LEFT OUTER JOIN查询中使用,因此在运行查询时它们是 null。 (I can get the results when i run the query in Server Explorer in VS). (当我在 VS 的服务器资源管理器中运行查询时,我可以获得结果)。 The error occurs when trying to load the data in Datatable:尝试加载 Datatable 中的数据时发生错误:

using (SqlCeCommand Cmd = new SqlCeCommand("Query HERE", "Connection HERE"))
                {
                    C.Open();
                    using (SqlCeDataReader Rdr = Cmd.ExecuteReader())
                    {
                        DataTable DT = new DataTable();
                        DT.Load(Rdr);
                        return DT;
                    }
                }

I tried so many solutions to overcome this however i was not able to solve it.我尝试了很多解决方案来克服这个问题,但是我无法解决它。 I know about "EnforceConstraints" however as i do not use any dataset, i can not change that property.我知道“EnforceConstraints”但是因为我不使用任何数据集,所以我无法更改该属性。

I managed to solve this by getting the schema of the table, iterating through the rows of the schema table (which are columns of the actual table) and creating a column with same properties as the schema column (only difference is setting new column's AllowDBNull to true and Unique and AutoIncrement to false) and finally adding the new column to a fresh datatable which will later be filled with data of our actual table (by the help of a DataReader to get only data and not the schema). 我设法通过获取表的模式,遍历模式表的行(这是实际表的列)并创建与模式列具有相同属性的列来解决此问题(唯一的区别是将新列的AllowDBNull设置为true和Unique以及AutoIncrement为false)并最终将新列添加到新数据表中,该数据表稍后将填充我们实际表的数据(在DataReader的帮助下仅获取数据而不是模式)。

Here is the code: 这是代码:

using (SqlCeConnection C = new SqlCeConnection(DBStr))
            using (Cmd)
            {   //using SqlCeCommand
                Cmd.Connection = C;
                C.Open();
                using (SqlCeDataReader Rdr = Cmd.ExecuteReader())
                {
                    //Create datatable to hold schema and data seperately
                    //Get schema of our actual table
                    DataTable DTSchema = Rdr.GetSchemaTable();
                    DataTable DT = new DataTable();
                    if (DTSchema != null)
                        if (DTSchema.Rows.Count > 0)
                            for (int i = 0; i < DTSchema.Rows.Count; i++)
                            {
                                //Create new column for each row in schema table
                                //Set properties that are causing errors and add it to our datatable
                                //Rows in schema table are filled with information of columns in our actual table
                                DataColumn Col = new DataColumn(DTSchema.Rows[i]["ColumnName"].ToString(), (Type)DTSchema.Rows[i]["DataType"]);
                                Col.AllowDBNull = true;
                                Col.Unique = false;
                                Col.AutoIncrement = false;
                                DT.Columns.Add(Col);
                            }

                    while (Rdr.Read())
                    {
                        //Read data and fill it to our datatable
                        DataRow Row = DT.NewRow();
                        for (int i = 0; i < DT.Columns.Count; i++)
                        {
                            Row[i] = Rdr[i];
                        }
                        DT.Rows.Add(Row);
                    }
                    //This is our datatable filled with data
                    return DT;
                }
            }

In case it catches anyone else;以防它抓住其他人; in my SQLite database I had NULL entries stored as strings ( "NULL" ), simply fixing the data worked.在我的 SQLite 数据库中,我将 NULL 条目存储为字符串( "NULL" ),只需修复数据即可。

This is a SQLite specific issue as you can mix datatypes in columns!这是一个特定于 SQLite 的问题,因为您可以在列中混合数据类型!

This can also happen if you load data from a table where some strings are only different in upper and lower case.如果您从一个表中加载数据,其中某些字符串仅大写和小写不同,也会发生这种情况。 In my case it happened using SQLite (Microsoft.Data.SQLite).就我而言,它发生在使用 SQLite (Microsoft.Data.SQLite) 时。 I could fix it by setting DataTable.CaseSensitive = true .我可以通过设置DataTable.CaseSensitive = true来修复它。 Code example in C#: C#中的代码示例:

protected virtual DataTable Get(string sqlQuery)
{
    DataTable dataTable = new DataTable() { CaseSensitive = true };
    DbProviderFactory factory = SqliteFactory.Instance;
    using (DbConnection connection = GetConnection(factory))
    {
        using (DbCommand command = factory.CreateCommand())
        {
            command.Connection = connection;
            command.CommandText = sqlQuery;

            connection.Open();
            dataTable.Load(command.ExecuteReader());
        }
    }

    return dataTable;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法启用约束。 一行或多行包含违反非null,唯一或外键约束的值 - 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 这正是错误:无法启用约束。 一行或多行包含违反非空、唯一或外键约束的值 - Exactly this is the error: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints 无法启用约束。 一或多个行包含违反非null,唯一或外键约束的值。 ASP.NET C# - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. asp.net c# 无法启用约束。 一或多个行包含违反非null,唯一或外键约束的值。 MyDataset.Tables [0]。合并 - Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. MyDataset.Tables[0].Merge C#SQL:无法启用约束。 一或多个行包含违反非空,唯一或外键约束的值 - C# SQL: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints 计算数据表中不同的非空行 - Count distinct non-null rows in a datatable EF添加实体:由于一个或多个外键属性不可为空,因此无法更改关系 - EF adding entity: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架5无法更改该关系,因为一个或多个外键属性不可为空 - Entity Framework 5 The relationship could not be changed because one or more of the foreign-key properties is non-nullable 由于一个或多个外键属性不可为空,因此无法更改该关系 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM