C#-无效的尝试在关闭阅读器时调用Read [英] C# - Invalid attempt to call Read when reader is closed

查看:440
本文介绍了C#-无效的尝试在关闭阅读器时调用Read的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

由于某种原因,它正在生成异常无效的调用Read Reader的尝试已关闭。有人可以帮我解决吗?我不知道为什么它会在生成读取器内容后关闭连接的情况下生成此异常。

For some reason or another, it is generating the exception "Invalid attempt to call Read reader is closed". Can someone help me to solve it? I have no idea why it is generating this exception as I am closing the connection after reading the contents of the reader.

它在此行生成异常:

                    while (rdr.Read())


推荐答案

我解决了。

问题是我正在读取的数据表中加载数据块。我修改了代码,如下所示,现在可以使用。

The problem was that I was loading the datatable inside the read block. I modified the code as follows and it works now.

if (rdr.HasRows)
{
    results.Load(rdr);
}

这篇关于C#-无效的尝试在关闭阅读器时调用Read的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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