重新打开SqlDataReader

发布于 2024-12-01 15:40:24 字数 727 浏览 2 评论 0原文

我的代码抛出错误

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

我正在使用 SqlDataReader 从数据库读取值,这就是我的代码:

while (rd.Read())
{
    string stateincharge = rd["stateincharge"].ToString();
    string email = rd["email"].ToString();
    cmd.Dispose();
    rd.Close();
    cmd = con.CreateCommand();
    cmd.CommandText = "str_procedure";
    cmd.Parameters.AddWithValue("@stateincharge", stateincharge);
    cmd.CommandType = CommandType.StoredProcedure;
    ad.SelectCommand = cmd;
    ad.Fill(ds);
    count = ds.Tables[0].Rows.Count;
    DataTable dt = new DataTable();
}

这在 ASP.NET 代码隐藏中的循环中运行。

我的问题是,由于显示错误,我认为我需要关闭 SqlDatReader。

如何在 while 循环结束时再次打开 sqlDataReader?

My code throws the error

Invalid attempt to call Read when reader is closed.

I'm using SqlDataReader to read values from database, and that is my code:

while (rd.Read())
{
    string stateincharge = rd["stateincharge"].ToString();
    string email = rd["email"].ToString();
    cmd.Dispose();
    rd.Close();
    cmd = con.CreateCommand();
    cmd.CommandText = "str_procedure";
    cmd.Parameters.AddWithValue("@stateincharge", stateincharge);
    cmd.CommandType = CommandType.StoredProcedure;
    ad.SelectCommand = cmd;
    ad.Fill(ds);
    count = ds.Tables[0].Rows.Count;
    DataTable dt = new DataTable();
}

This runs in a loop in ASP.NET code-behind.

My problem is that I think I need to close SqlDatReader because of an error shown.

How can I again open sqlDataReader at end of the while loop?

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

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

发布评论

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

评论(4

顾冷 2024-12-08 15:40:24
// connection for reader
using (SqlConnection connection1 = new SqlConnection(connectionString))
using (SqlCommand command1 = new connection1.CreateCommand())
{
    command1.CommandText = commandText1;

    connection1.Open();
    using (SqlDataReader reader = command1.ExecuteReader())
    {
        // fill table in loop
        while (reader.Read())
        {
            string stateincharge = reader["stateincharge"].ToString();
            string email = reader["email"].ToString();

            // connection for adapter
            using (SqlConnection connection2 = new SqlConnection(connectionString))
            using (SqlCommand command2 = new connection2.CreateCommand())
            {
                command2.CommandText = commandText2;

                command2.Parameters.AddWithValue("@stateincharge", stateincharge);
                command2.Parameters.AddWithValue("@email ", email );

                connection2.Open();

                DataTable table = new DataTable();
                using (SqlDataApapter adapter = new SqlDataAdapter(command2))
                {
                    adapter.Fill(table);
                    // yield return table;
                }
            }
        }
    }
}
// connection for reader
using (SqlConnection connection1 = new SqlConnection(connectionString))
using (SqlCommand command1 = new connection1.CreateCommand())
{
    command1.CommandText = commandText1;

    connection1.Open();
    using (SqlDataReader reader = command1.ExecuteReader())
    {
        // fill table in loop
        while (reader.Read())
        {
            string stateincharge = reader["stateincharge"].ToString();
            string email = reader["email"].ToString();

            // connection for adapter
            using (SqlConnection connection2 = new SqlConnection(connectionString))
            using (SqlCommand command2 = new connection2.CreateCommand())
            {
                command2.CommandText = commandText2;

                command2.Parameters.AddWithValue("@stateincharge", stateincharge);
                command2.Parameters.AddWithValue("@email ", email );

                connection2.Open();

                DataTable table = new DataTable();
                using (SqlDataApapter adapter = new SqlDataAdapter(command2))
                {
                    adapter.Fill(table);
                    // yield return table;
                }
            }
        }
    }
}
方圜几里 2024-12-08 15:40:24

您必须删除关闭读取器的行 rd.Close(); 因为您要在 while 循环内关闭读取器,然后尝试再次访问读取器。另外,我认为如果您使用新的 SQL 命令和新适配器,您将不会收到此错误。

You have to remove the line that closes the reader rd.Close(); because you are closing the reader inside the while loop then you try to access the reader again. Also, I think if you used a new SQL command and new adapter you will not receive this error.

旧竹 2024-12-08 15:40:24

每个连接最多可以有一个活动命令(其中包括“响应”,即阅读器)。您正在尝试对数据库进行嵌套调用,这要求您在循环之前完全加载数据(例如加载到 DataTable 中),或者使用第二个连接来进行嵌套调用到str_procedure

Each connection can have at most one active command (which includes the "response", that is, the reader). You're trying to do a nested call to the DB and this requires that you either load the data completely (for instance into a DataTable) before looping, or that you uise a second connection for the nested calls to str_procedure.

把时间冻结 2024-12-08 15:40:24
while (rd.Read())
    {
        string stateincharge = rd["stateincharge"].ToString();
        string email = rd["email"].ToString();
        cmd.Dispose();
        cmd = con.CreateCommand();
        cmd.CommandText = "str_procedure";
        cmd.Parameters.AddWithValue("@stateincharge", stateincharge);
        cmd.CommandType = CommandType.StoredProcedure;
        ad.SelectCommand = cmd;
        ad.Fill(ds);
        count = ds.Tables[0].Rows.Count;
        DataTable dt = new DataTable();
}
rd.Close();
while (rd.Read())
    {
        string stateincharge = rd["stateincharge"].ToString();
        string email = rd["email"].ToString();
        cmd.Dispose();
        cmd = con.CreateCommand();
        cmd.CommandText = "str_procedure";
        cmd.Parameters.AddWithValue("@stateincharge", stateincharge);
        cmd.CommandType = CommandType.StoredProcedure;
        ad.SelectCommand = cmd;
        ad.Fill(ds);
        count = ds.Tables[0].Rows.Count;
        DataTable dt = new DataTable();
}
rd.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文