You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Load()方法加载OleDbDataReader到DataTable时触发NullReferenceException

加载IBM DB2视图数据到DataTable时触发NullReferenceException

我开发的应用需要通过DataTable.Load(IDataReader reader)方法,将IBM DB2数据库视图中的数据加载到System.Data.DataTable中。但加载数千条数据时,OleDbDataReader会触发NullReferenceException。

  • 该问题仅在操作数据库视图时出现,加载普通数据库表的数据则无异常。
  • 我认为可空列应仅在DataTable的列中标记为可空,不应引发该异常。
  • 连接及命令的超时时间已设置为较大值,可排除读取器超时的可能性。

连接字符串

private const string connectionString = "Provider=IBMDA400;Data Source=...;User ID=...;Password=...;Force Translate=37;Connect Timeout=600";

OleDbCommand创建代码

new OleDbCommand(query, Connection) { CommandType = CommandType.Text, CommandTimeout = 500 }.ExecuteReader();

核心业务代码

var dataTable = new DataTable();
var reader = Oledb.executeQuery($"SELECT * FROM ... WHERE ...");
dataTable.Load(reader);

Oledb.executeQuery方法实现

return new OleDbCommand(query, Connection) { CommandType = CommandType.Text, CommandTimeout = CommandTimeout }.ExecuteReader();

异常堆栈跟踪

>System.Data.dll!System.Data.OleDb.OleDbDataReader.GetRowDataFromHandle()
System.Data.dll!System.Data.OleDb.OleDbDataReader.GetValueBinding(System.Data.OleDb.MetaData info)
System.Data.dll!System.Data.OleDb.OleDbDataReader.GetValues(object[] values)
System.Data.dll!System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(object[] values)
System.Data.dll!System.Data.ProviderBase.SchemaMapping.LoadDataRow()
System.Data.dll!System.Data.Common.DataAdapter.FillLoadDataRow(System.Data.ProviderBase.SchemaMapping mapping)
System.Data.dll!System.Data.Common.DataAdapter.FillFromReader(System.Data.DataSet dataset, System.Data.DataTable datatable, string srcTable, System.Data.ProviderBase.DataReaderContainer dataReader, int startRecord, int maxRecords, System.Data.DataColumn parentChapterColumn, object parentChapterValue)
System.Data.dll!System.Data.Common.DataAdapter.Fill(System.Data.DataTable[] dataTables, System.Data.IDataReader dataReader, int startRecord, int maxRecords)
System.Data.dll!System.Data.Common.LoadAdapter.FillFromReader(System.Data.DataTable[] dataTables, System.Data.IDataReader dataReader, int startRecord, int maxRecords)
System.Data.dll!System.Data.DataTable.Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler errorHandler)
System.Data.dll!System.Data.DataTable.Load(System.Data.IDataReader reader)
>ProductionRecords.dll!BbtExtensions.ProductionRecords.GetIsnrsForPon.AnonymousMethod__0() Line 205
mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.Task<System.Collections.Generic.List<BbtExtensions.ProductionRecords.IsnrDto>>>.InnerInvoke()
mscorlib.dll!System.Threading.Tasks.Task.Execute()
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution)
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

内容的提问来源于stack exchange,提问作者Jakob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 04:15:43