DataTableReader 对当前 DataTable 'TempTable' 无效 [英] DataTableReader is invalid for current DataTable 'TempTable'

查看:22
本文介绍了DataTableReader 对当前 DataTable 'TempTable' 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

DataTableReader 对当前 DataTable 'TempTable' 无效."

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

问题是,如果我重新启动机器,它可以在不确定的时间内正常工作,然后会因上述情况而死亡.引发此错误的代码可能已经正常工作了几个小时,然后:砰.你得到这个错误.它也不限于一行;它是使用 DataTableReader 的每个位置.此外,此错误不会发生在生产网络服务器上 - 永远不会发生.

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.

这让我在一周的大部分时间里都发疯了,我在 Google 上找不到任何可以提供帮助的东西(因为我很确定这不是编码问题).

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).

一些技术信息:

开发箱:Vista 32 位(包含所有当前的 Windows 更新)Visual Studio 2008 v9.0.30729.1 SPdotNet 框架 3.5 SP1

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

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

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

网络服务器:Windows 2003 64 位(包含所有当前的 Windows 更新)

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

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

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

更新 1:

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

Ok - Have tried the following now with no success:

1:重新启动2:证监会/立即扫描3:更改了 SQL Server4:尝试使用DataTableReaders的不同方法5:清洗液

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

我发现唯一有效的方法是复制 &粘贴代码从主要的 Visual Studio 实例,到另一个具有简单控制台应用程序的实例.然后按预期工作(查询数据库并将结果放入数据表中,在该表上创建一个数据表读取器,然后在调用 .Read() 之前查询 hasrows...所有这些都奏效了.

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.

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

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.

推荐答案

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

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

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

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

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.

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

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.

DataTableReader.HasRows 属性

这篇关于DataTableReader 对当前 DataTable 'TempTable' 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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