.SqlDataReader.ReadColumnHeader的NullReferenceException [英] .SqlDataReader.ReadColumnHeader NullReferenceException

查看:181
本文介绍了.SqlDataReader.ReadColumnHeader的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题在某种程度上与<一个href="http://stackoverflow.com/questions/18468284/sqldatareader-does-not-return-all-records-3rd-attempt">my previous 之一回答这有助于我找到了原因,为什么这个简单的迭代直通SqlDataReader的:

This question is somewhat related to my previous one answer to which helped me to find the reason why this simple iteration thru SqlDataReader:

m_aFullIDList = New Generic.List(Of Integer)

While i_oDataReader.Read
    m_aFullIDList.Add(i_oDataReader.GetInt32(0))
End While

m_iTotalNumberOfRecords = m_aFullIDList.Count

不返回的所有记录。原来当Generic.List改变其容量以容纳更多的元素(例如,从2 ^ 19到下一个2 ^ 20)在这一点上SqlDataReader的简单对等,它的读取方法返回false,如果没有更多的记录。

does not return all the records. Turned out when Generic.List changes its Capacity to accommodate more elements (e.g from 2^19 to next one 2^20) at this point SqlDataReader simple quits, its Read method returns False as if there's no more records.

它的大部分悄然退出时间,没有异常被抛出任何。但每一个现在,然后我会得到:

Most of the time it quits quietly, no exception is thrown whatsoever. But every now and then I'd get:

的NullReferenceException {对象未设置为一个实例   对象。}

NullReferenceException {"Object reference not set to an instance of an object."}

在System.Data.SqlClient.SqlDataReader.ReadColumnHeader(的Int32我)
  在System.Data.SqlClient.SqlDataReader.ReadColumn(我的Int32,布尔   的setTimeout)在System.Data.SqlClient.SqlDataReader.GetInt32(的Int32我)

at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout) at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)

我知道一个事实,即使用的阅读器(它是一个单柱)存储过程返回的所有记录都是整数值。如果我删除行 m_aFullIDList.Add ,而是简单的读值转换成整数变量还是我pre-分配泛型列表容量为知大批 - 这个问题不会发生。显然,这只是发生在名单重新分配的能力 - 这会影响读者

I know for a fact that all records returned by Stored Procedure used by the Reader (it's a single column) are integer values. If I remove line m_aFullIDList.Add and instead simple read value into an integer variable OR if I pre-allocate Generic List capacity to a know large number - this issue is not happening. Apparently it only happens when List reallocates Capacity - this affects the reader.

我还试图使用其他结构(ArrayList中,甚至阵列,使用Array.Resize)只要该结构的能力被重新分配超过特定点 - 这打破SqlDataReader的

I also attempted to use other structures (ArrayList, even Array, using Array.Resize) as soon as capacity of this structure is reallocated beyond certain point - this breaks the SqlDataReader.

这ASP.NET项目是有点复杂,所以当我试图在仅由执行读写器和阅读列表进入一个独立的简单的项目重建的问题 - 这个问题是不会发生。任何想法是怎么回事,如何这可以解决吗?

This ASP.NET project is kinda complex, so when I tried to recreate the issue in a standalone simple project consisting only of executing reader and reading into List - the issue is not happening. Any idea what is going on and how this could be fixed?

推荐答案

我想,我终于想通了。

I think I finally figured it out.

在我的code的逻辑有两个步骤 - 调用返回SqlDataReader的功能,然后用该阅读器在另一个函数来填充列表:

The logic in my code had 2 steps - call the function that returns SqlDataReader and then use that reader in another function to fill the list:

Dim oReader as SqlDataReader = GetTheReader()

FillTheList(oReader)

功能GetTheReader()看起来是这样的:

Function GetTheReader() looked something like this:

Function GetTheReader() as SqlDataReader
   Dim oConn As New SqlConnection("Connection String") : oConn.Open()
   Dim oComm As New SqlCommand("Stored Procedure", oConn)

   Dim oReader As SqlDataReader = oComm.ExecuteReader(CommandBehavior.CloseConnection)

   Return oReader
End Function

它打开的连接作为的本地的其中又以超出范围时,该函数返回给调用者的变量。当泛型列表中被填充,陆续容量分配的垃圾收集摧毁过时的变量(和关闭的连接)声称内存。我留下了一个SqlDataReader没有一个有效的连接。

It opened connection as a local variable which went out of scope when the function returned back to the caller. And when Generic List was being populated, after another Capacity allocation Garbage Collection claimed that memory by destroying outdated variables (and closing that connection). I was left with a SqlDataReader without a valid connection.

我目前的解决方案是创建 GetTheReader 函数外连接,并把它作为参数之一。

My current solution is to create connection outside GetTheReader function and pass it as one of the parameters.

这篇关于.SqlDataReader.ReadColumnHeader的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆