I have the notorious "failed to enable constraints" exception, but none of the answers from this question are applicable because I'm not using a DataSet
or TableAdatper
:
DataTable tblTypes = connVeekun.ExecuteDataTable("SELECT id, damage_class_id, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 1) AS name_ja, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 9) AS name_en, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 5) AS name_fr, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 8) AS name_it, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 6) AS name_de, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 7) AS name_es, " +
"(SELECT name FROM type_names WHERE type_names.type_id = types.id AND local_language_id = 3) AS name_ko " +
"FROM types ORDER BY id");
Helper method:
/// <summary>
/// Runs a command and returns a DataTable containing its results.
/// </summary>
/// <param name="db">Open data connection</param>
/// <param name="sqlstr">SQL string</param>
/// <param name="_params">List of parameters to use with the SQL</param>
public static DataTable ExecuteDataTable(this DbConnection conn, String sqlstr, params IDataParameter[] _params)
{
DbCommand cmd = conn.CreateCommand();
cmd.CommandText = sqlstr;
cmd.Parameters.AddRange(_params);
DbDataReader reader = cmd.ExecuteReader();
DataTable result = new DataTable();
result.Load(reader);
return result;
}
The failed constraint is that there's a null value in the name_ko
column. Since I'm not using a DataAdapter
, I have no way to change the (inferred) schema or disable constraints. It's important in my application that I preserve the nullness of the name_ko
column so modifying the SQL to remove nulls is not an option.
The data is from Eevee's Pokédex.