读取器关闭时无效尝试调用hasrows。 [英] Invalid attempt to call hasrows when reader is closed.

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

问题描述

关闭阅读器时无效尝试调用HasRows。请指导我



Invalid attempt to call HasRows when reader is closed. please guide me

try
            {
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["TextItConnectionString"].ConnectionString);
                using (con)
                {
                    con.Open();
                    Library.writeErrorLog("connection build and open");
                    SqlCommand cmd = con.CreateCommand();
                    using (cmd)
                    {
                        cmd.CommandText = "Select [name] From [dbo].[Users]";
                        SqlDataReader reader = cmd.ExecuteReader();
                        using (reader)
                        {
                            user.dt.Load(reader);
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    Library.writeErrorLog(reader.GetString(0));
                                }
                            }
                            else
                                Library.writeErrorLog("no rows");
                            reader.Close();
                            con.Close();
                        }
                    }
                }
                //SqlDataAdapter adap = new SqlDataAdapter("Select [name] From [dbo].[Users]", con);
                //adap.Fill(user.dt);
            }
            catch (Exception ex)
            {
                Library.writeErrorLog(ex);
            }





我的尝试:



阅读很多在阅读之前不关闭连接的论坛。我最后关闭了。我也尝试删除关闭连接线,但有相同的错误



What I have tried:

read on alot of forums that dont close the connection before reading. i have closed in the end. i have also tried removing close connection line but having same error

推荐答案

DataReader是单向连接 - 一旦你读了一行,你就可以回去以前的一个。

因此,当您使用DataTable.Load从SqlDataReader读取时,它会在完成所有数据后关闭Reader。

为什么要尝试使用同一个DataReader读取所有数据两次?相反,循环访问DataTAble结果以提供您的日志信息。

更好,使用SqlDataAdapter并从中加载数据表,因为它为您打开了连接对象。

使用块不应该这样写:

A DataReader is a one direction connection - once you read a row, you can;t go back to teh previous one.
So when you use DataTable.Load to read from the SqlDataReader, it closes the Reader when it's finished with all data.
Why are you trying to use the same DataReader to read all the data twice? Instead, loop through the DataTAble result to provide your log info.
Better, use an SqlDataAdapter and load the datatable from that instead, as it opens teh connection object for you.
And usingblocks shouldn't be written like that:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TextItConnectionString"].ConnectionString))
{
    con.Open();
    Library.writeErrorLog("connection build and open");
    using (SqlCommand cmd = con.CreateCommand())
    {



是更好的方法,因为它确保变量在超出范围时是Disposed。


Is the better method as it ensures the variable is Disposed when it goes out of scope.


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

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