无法在没有数据适配器的情况下启用约束
本文关键字:情况下 启用 约束 适配器 数据 | 更新日期: 2023-09-27 18:32:12
我有臭名昭著的"未能启用约束"异常,但这个问题的答案都不适用,因为我没有使用DataSet
或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");
帮助程序方法:
/// <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;
}
失败的约束是name_ko
列中存在 null 值。由于我没有使用DataAdapter
,因此我无法更改(推断的)模式或禁用约束。在我的应用程序中,保留name_ko
列的空度很重要,因此修改 SQL 以删除空值不是一种选择。
数据来自Eevee的Pokédex。
从文档中:
该方法还从结果集中检索约束信息 对于所有添加的列。除主键约束的情况外, 仅当当前数据表执行 在加载操作开始时不包含任何列。
看起来,如果您为表设置架构(通过创建所有列)然后加载它,您将绕过约束问题,因为他的方法不会推断约束。