DataTableReader对当前DataTable“TempTable”无效

时间:2022-01-30 23:21:53

I'm getting the following error whenever my code creates a DataTableReader from a valid DataTable Object:

每当我的代码从有效的DataTable对象创建DataTableReader时,我都会收到以下错误:

"DataTableReader is invalid for current DataTable 'TempTable'."

“DataTableReader对于当前的DataTable'TempTable'无效。”

The thing is, if I reboot my machine, it works fine for an undetermined amount of time, then dies with the above. The code that throws this error could have been working fine for hours and then: bang. you get this error. It's not limited to one line either; it's every single location that a DataTableReader is used. Also, this error does NOT occur on the production web server - ever.

问题是,如果我重新启动我的机器,它可以在不确定的时间内正常工作,然后用上述方法消失。抛出此错误的代码可能已经工作了好几个小时然后:爆炸。你得到这个错误。它也不仅限于一条线;它是使用DataTableReader的每个位置。此外,此错误不会发生在生产Web服务器上 - 永远。

This has been driving me nuts for the best part of a week, and I've failed to find anything on Google that could help (as I'm pretty positive this isn't a coding issue).

这让我疯狂了一周的最佳时间,而且我没有在谷歌上找到任何可以帮助的东西(因为我非常肯定这不是编码问题)。

Some technical info:

一些技术信息:

DEV Box: Vista 32bit (with all current windows updates) Visual Studio 2008 v9.0.30729.1 SP dotNet Framework 3.5 SP1

DEV Box:Vista 32bit(所有当前Windows更新)Visual Studio 2008 v9.0.30729.1 SP dotNet Framework 3.5 SP1

SQL Server: Microsoft SQL Server 2005 Standard Edition- 9.00.4035.00 (X64) Windows 2003 64bit (with all current windows updates)

SQL Server:Microsoft SQL Server 2005标准版 - 9.00.4035.00(X64)Windows 2003 64位(包含所有当前Windows更新)

Web Server: Windows 2003 64bit (with all current windows updates)

Web服务器:Windows 2003 64位(所有当前Windows更新)

Any help, ideas, or advice would be greatly appreciated!

任何帮助,想法或建议将不胜感激!

UPDATE 1:

Ok - Have tried the following now with no success:

好的 - 现在尝试了以下但没有成功:

1: Rebooted 2: SFC / ScanNow 3: Changed SQL Servers 4: Tried a different method that uses DataTableReaders 5: Cleaned solution

1:重新启动2:SFC / ScanNow 3:更改SQL Server 4:尝试使用DataTableReaders的另一种方法5:清除解决方案

The only thing I did find that worked was copy & pasting the code from the main Visual studio instance, into another which had a simple console app. This then worked as expected (queried database and got results into a dataTable, created a datatablereader on that table, then queried hasrows before calling .Read()... All of which worked.

我唯一能找到的就是将Visual Studio实例中的代码复制并粘贴到另一个具有简单控制台应用程序的代码中。然后按预期工作(查询数据库并将结果放入dataTable,在该表上创建一个datatablereader,然后在调用.Read()之前查询hasrow ...所有这些都有效。

I am struggling to see what could cause this, as there are NO code faults - i'm 100% certain, as it runs perfectly when published to the webserver.

我正在努力寻找可能导致这种情况的原因,因为没有代码错误 - 我100%肯定,因为它在发布到网络服务器时运行得很好。

7 个解决方案

#1


7  

I think using the while(reader.read()) may solve your problem.

我认为使用while(reader.read())可以解决您的问题。

if (myReader.HasRows)
   while (myReader.Read())
     Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
else
   Console.WriteLine("No rows returned.");
myReader.Close();

UPDATE: Also from msdn: The HasRows property returns information about the current result set. If the DataTableReader contains multiple result sets, you can examine the value of the HasRows property immediately after you call the NextResult method in order to determine whether the new result set contains rows.

更新:同样来自msdn:HasRows属性返回有关当前结果集的信息。如果DataTableReader包含多个结果集,则可以在调用NextResult方法后立即检查HasRows属性的值,以确定新结果集是否包含行。

Use the HasRows property to avoid the requirement to call the Read method of the DataTableReader if there are no rows within the current result set.

如果当前结果集中没有行,则使用HasRows属性可以避免要求调用DataTableReader的Read方法。

DataTableReader.HasRows Property

#2


4  

Had the same problem and got rid of it after clearing the variables in the watch window.

有相同的问题,并在清除监视窗口中的变量后摆脱它。

#3


2  

Clearing the watch window & doing rebuilds worked for me. However, because I had to remember to frequently rebuild, I eventually just renamed it also. (prior to renaming, adding additional watch variables on an object could cause previous watch variables on that object to become invalid -- even without progressing through the code, ie staying on the same line)

清理观察窗口并进行重建对我有用。但是,因为我不得不记得经常重建,所以我最终也重命名了它。 (在重命名之前,在对象上添加其他监视变量可能会导致该对象上的先前监视变量变为无效 - 即使没有通过代码,也就是停留在同一行上)

#4


1  

Wrap usage of DataTableReader (and all IDisposables) with using.

使用包装使用DataTableReader(和所有IDisposables)。

#5


1  

OK.. Further down in the code, I have the following code:

好的..在代码的下面,我有以下代码:

using (DataTableReader tr = dtCustomers.CreateDataReader())
{
    ....
}

If I change this to read:

如果我将其更改为:

using (DataTableReader tr2 = dtCustomers.CreateDataReader())
{
    ....
}

Then, and remember this bit of code is much later down in the same procedure, BOTH bits of code work without fault!

然后,记住这段代码后来在同一个程序中,BOTH代码的工作没有错!

So, this doesn't work:

所以,这不起作用:

using (DataTableReader tr = dt.CreateDataReader())
{
    ...
}

....

using (DataTableReader tr = dt.CreateDataReader())
{
    ...
}

But this does:

但这样做:

using (DataTableReader tr = dt.CreateDataReader())
{
    ...
}

....

using (DataTableReader tr2 = dt.CreateDataReader())
{
    ...
}

I don't understand why this way works, but it does and as I've not had another answer, I'll be going with this.

我不明白为什么这种方式有效,但它确实如此,而且我没有另外的答案,我将继续这样做。

If you know why this works, and why the original doesn't, please can you enlighten me? :)

如果你知道为什么这个有用,为什么原来没有,请你能开导我吗? :)

#6


1  

Just thought I would post on here in case it's helps someone else. I tried a number of things and in the end i simply changed the name of the datareader and it worked, kind of similar to here. i dont know why for sure but i think it might be because the datareader (originally) wasnt being closed, so maybe after a few times debugging, there was lots of "stuff" in memory with a certain name attached and it said "no more!". still, i could be talking bullpies. my advice, change the name of your datareader variable and make sure you close it after you use it

只是想我会发布在这里,以防它帮助别人。我尝试了很多东西,最后我简单地更改了datareader的名称并且它起作用了,类似于这里。我不知道为什么肯定,但我认为这可能是因为datareader(最初)没有关闭,所以也许经过几次调试后,内存中有很多“东西”附加了某个名称,它说“不再!”。不过,我可以说是公牛。我的建议,更改datareader变量的名称,并确保在使用后关闭它

#7


1  

Seems like a bug on getting the tableReader... i have code that has been working for ears and if i change another method sometimes i get that errro... some times it's solved just recompiling (rebuild), another times i reinstalled the .NET framework or use the option repair of it... i am starting to put try catch sections to use the reader if the system "wants" to givme the reader and the DataTable if not. Greetings.

看起来像获取tableReader的错误...我有代码已经为耳朵工作,如果我改变另一种方法有时我得到错误...有时它解决了重新编译(重建),另一次我重新安装。 NET框架或使用它的选项修复...我开始把try catch部分用于读取器,如果系统“想要”给读者和DataTable,如果没有。问候。

#1


7  

I think using the while(reader.read()) may solve your problem.

我认为使用while(reader.read())可以解决您的问题。

if (myReader.HasRows)
   while (myReader.Read())
     Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
else
   Console.WriteLine("No rows returned.");
myReader.Close();

UPDATE: Also from msdn: The HasRows property returns information about the current result set. If the DataTableReader contains multiple result sets, you can examine the value of the HasRows property immediately after you call the NextResult method in order to determine whether the new result set contains rows.

更新:同样来自msdn:HasRows属性返回有关当前结果集的信息。如果DataTableReader包含多个结果集,则可以在调用NextResult方法后立即检查HasRows属性的值,以确定新结果集是否包含行。

Use the HasRows property to avoid the requirement to call the Read method of the DataTableReader if there are no rows within the current result set.

如果当前结果集中没有行,则使用HasRows属性可以避免要求调用DataTableReader的Read方法。

DataTableReader.HasRows Property

#2


4  

Had the same problem and got rid of it after clearing the variables in the watch window.

有相同的问题,并在清除监视窗口中的变量后摆脱它。

#3


2  

Clearing the watch window & doing rebuilds worked for me. However, because I had to remember to frequently rebuild, I eventually just renamed it also. (prior to renaming, adding additional watch variables on an object could cause previous watch variables on that object to become invalid -- even without progressing through the code, ie staying on the same line)

清理观察窗口并进行重建对我有用。但是,因为我不得不记得经常重建,所以我最终也重命名了它。 (在重命名之前,在对象上添加其他监视变量可能会导致该对象上的先前监视变量变为无效 - 即使没有通过代码,也就是停留在同一行上)

#4


1  

Wrap usage of DataTableReader (and all IDisposables) with using.

使用包装使用DataTableReader(和所有IDisposables)。

#5


1  

OK.. Further down in the code, I have the following code:

好的..在代码的下面,我有以下代码:

using (DataTableReader tr = dtCustomers.CreateDataReader())
{
    ....
}

If I change this to read:

如果我将其更改为:

using (DataTableReader tr2 = dtCustomers.CreateDataReader())
{
    ....
}

Then, and remember this bit of code is much later down in the same procedure, BOTH bits of code work without fault!

然后,记住这段代码后来在同一个程序中,BOTH代码的工作没有错!

So, this doesn't work:

所以,这不起作用:

using (DataTableReader tr = dt.CreateDataReader())
{
    ...
}

....

using (DataTableReader tr = dt.CreateDataReader())
{
    ...
}

But this does:

但这样做:

using (DataTableReader tr = dt.CreateDataReader())
{
    ...
}

....

using (DataTableReader tr2 = dt.CreateDataReader())
{
    ...
}

I don't understand why this way works, but it does and as I've not had another answer, I'll be going with this.

我不明白为什么这种方式有效,但它确实如此,而且我没有另外的答案,我将继续这样做。

If you know why this works, and why the original doesn't, please can you enlighten me? :)

如果你知道为什么这个有用,为什么原来没有,请你能开导我吗? :)

#6


1  

Just thought I would post on here in case it's helps someone else. I tried a number of things and in the end i simply changed the name of the datareader and it worked, kind of similar to here. i dont know why for sure but i think it might be because the datareader (originally) wasnt being closed, so maybe after a few times debugging, there was lots of "stuff" in memory with a certain name attached and it said "no more!". still, i could be talking bullpies. my advice, change the name of your datareader variable and make sure you close it after you use it

只是想我会发布在这里,以防它帮助别人。我尝试了很多东西,最后我简单地更改了datareader的名称并且它起作用了,类似于这里。我不知道为什么肯定,但我认为这可能是因为datareader(最初)没有关闭,所以也许经过几次调试后,内存中有很多“东西”附加了某个名称,它说“不再!”。不过,我可以说是公牛。我的建议,更改datareader变量的名称,并确保在使用后关闭它

#7


1  

Seems like a bug on getting the tableReader... i have code that has been working for ears and if i change another method sometimes i get that errro... some times it's solved just recompiling (rebuild), another times i reinstalled the .NET framework or use the option repair of it... i am starting to put try catch sections to use the reader if the system "wants" to givme the reader and the DataTable if not. Greetings.

看起来像获取tableReader的错误...我有代码已经为耳朵工作,如果我改变另一种方法有时我得到错误...有时它解决了重新编译(重建),另一次我重新安装。 NET框架或使用它的选项修复...我开始把try catch部分用于读取器,如果系统“想要”给读者和DataTable,如果没有。问候。