Search code examples
c#datatableconnectionsql-server-cedatareader

Datatable.Load does not work with SqlCeReader


I have this method:

public static DataTable ExecuteDataTable(IDbConnection connection, string cmdText)
{
    IDbCommand command = connection.CreateCommand();
    command.CommandText = cmdText;
    command.CommandType = CommandType.Text;
    IDataReader reader = command.ExecuteReader();
    DataTable dt = new DataTable();
    dt.Load(reader);
    return dt;
}

When i execute the query select * from information_schema.Tables against a connection of type SQLConnection it all works. However, when I try to run it against a connection of type SqlCEConnection the line dt.Load(reader) raises an exception:

System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Solution

  • Certainly a weird issue but here's an alternative:

    Read the data into a DataSet and set the EnforceConstraints to false. You could then return DataSet.Tables[0]