阅读器关闭时调用 Read 的尝试无效

发布于 2024-12-21 22:09:50 字数 866 浏览 2 评论 0原文

我有点努力尝试绑定这个数据网格。每次运行代码时,我都会收到一条错误消息,指出“阅读器关闭时调用 Read 的尝试无效”。我不明白我要在哪里关闭我的阅读器。你能帮我吗?我加载数据网格的代码如下:

protected void LoadGrid()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["VTC"].ConnectionString;
                conn.Open();


                string sql = "select * from roi_tracking";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    using (SqlDataReader sqlReader = cmd.ExecuteReader())
                    {

                        gridROI.DataSource = sqlReader;
                        gridROI.DataBind();

                        sqlReader.Dispose();
                        cmd.Dispose();
                    }
                }

            }
        }

I'm kind of struggling trying to get this datagrid to bind. Everytime I run my code, I get an error message stating, "Invalid attempt to call Read when reader is closed". I don't see where I am closing my reader. Can you please help me? My code for loading the datagrid is below:

protected void LoadGrid()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["VTC"].ConnectionString;
                conn.Open();


                string sql = "select * from roi_tracking";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    using (SqlDataReader sqlReader = cmd.ExecuteReader())
                    {

                        gridROI.DataSource = sqlReader;
                        gridROI.DataBind();

                        sqlReader.Dispose();
                        cmd.Dispose();
                    }
                }

            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-12-28 22:09:51

您不能使用 SqlDataReader 作为 DataGrid 的数据源。

来自 MSDN:

数据源必须是实现以下任一功能的集合:
System.Collections.IEnumerable接口(如
System.Data.DataView、System.Collections.ArrayList 或
System.Collections.Generic.List(Of T)) 或 IListSource 接口
绑定到从 BaseDataList 类派生的控件。

数据源属性:
http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.basedatalist.datasource.aspx

SqlDataReader:
http://msdn.microsoft.com/en-us /library/system.data.sqlclient.sqldatareader.aspx

将查询结果绑定到数据网格的常见方法是使用 SqlDataAdapter 填充 DataTable 或 DataSet,然后将其绑定到您的数据网格。数据网格.数据源:

protected void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["VTC"].ConnectionString;
            conn.Open();

            string sql = "select * from roi_tracking";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;

                adapter.Fill((DataTable)results);
                gridROI.DataSource = results;
            }

        }
    }

You can't use a SqlDataReader as a DataSource for a DataGrid.

From MSDN:

A data source must be a collection that implements either the
System.Collections.IEnumerable interface (such as
System.Data.DataView, System.Collections.ArrayList, or
System.Collections.Generic.List(Of T)) or the IListSource interface to
bind to a control derived from the BaseDataList class.

Datasource property:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedatalist.datasource.aspx

SqlDataReader:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

A common methodology to bind the query results to your datagrid would be to use a SqlDataAdapter to fill a DataTable or DataSet and then bind that to your DataGrid.DataSource:

protected void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["VTC"].ConnectionString;
            conn.Open();

            string sql = "select * from roi_tracking";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;

                adapter.Fill((DataTable)results);
                gridROI.DataSource = results;
            }

        }
    }
流心雨 2024-12-28 22:09:51

除了伪编码器所说的之外,无论如何您都不想绑定到 SqlDataReader:只要读取器实例存在,与数据库的连接就会保持打开状态。

您肯定希望将数据反序列化为其他断开连接的数据结构,以便尽快将连接释放回池中。

In addition to what pseudocoder said, you wouldn't want to bind to a SqlDataReader anyway: the connection to the database will remain open so long as the reader instance exists.

You definitely want to deserialize the data into some other disconnected data structure so that you release the connection back into the pool as quickly as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文