我写了一个方法,使用反射来在.NET应用程序中加载多个强类型数据表。如果我按照下面的方法运行,一切都正常工作,包括没有抛出任何异常。但是,如果我改为使用注释部分(其余内容相同),那么我会得到“启用约束失败”错误,详细描述请参见这里:进入链接描述。
如果我查看错误数组中的内容,总是以下内容:
如果我查看错误数组中的内容,总是以下内容:
"Column 'AEDelegateName' does not allow DBNull.Value."
对于错误,ItemArray看起来像这样:
[0] = {}
[1] = "Some Value"
这让我感到惊讶,因为我只期望一个选择1列而不是像上面所示的2列的脚本。 我还想象这接近问题,因为其中一个似乎为空。
我的脚本不会返回空值,并且我可以快速,直观地确认它,以及在我使用的查询中说诸如NOT NULL之类的话。
private void GetData(string query, Component tableAdapter)
{
OracleCommand command = new OracleCommand();
command.Connection = conn;
command.CommandText = query;
command.CommandType = CommandType.Text;
command.CommandTimeout = 3000;
OracleDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
MethodInfo[] methods = tableAdapter.GetType().GetMethods();
MethodInfo getDataMethod = tableAdapter.GetType().GetMethod("GetData");
DataTable table = (DataTable)getDataMethod.Invoke(tableAdapter, null);
Type[] paramTypes = new Type[] { table.GetType() };
MethodInfo updateMethod = tableAdapter.GetType().GetMethod("Update", paramTypes);
foreach (DataRow row in table.Rows)
{
row.Delete();
}
//try
//{
// if (reader.HasRows)
// {
// table.Load(reader, LoadOption.OverwriteChanges, FillErrorHandler);
// }
//}
//catch (Exception e)
//{
// DataRow[] errors = table.GetErrors();
//}
while (reader.Read())
{
try
{
List<object> newRow = new List<object>();
for (int i = 0; i < reader.FieldCount; ++i)
{
object currentValue = reader.GetValue(i);
Debug.WriteLine("Value: "+currentValue);
newRow.Add(currentValue);
}
table.Rows.Add(newRow.ToArray());
}
catch (ConstraintException e)
{
DataRow[] errors = table.GetErrors();
}
}
updateMethod.Invoke(tableAdapter, new object[]{table});
reader.Close();
}
DataTable table
和通过OracleDataReader
运行的查询之间的关系吗?我感觉两者的模式不同,而DataTable.Load()
会覆盖定义,从而触发约束。 - Caramirielcatch
块正在捕获异常? - radarbobAcceptChanges()
。 - radarbob