出现异常:阅读器关闭时尝试读取无效

发布于 2024-12-14 23:33:25 字数 1802 浏览 2 评论 0原文

我正在尝试使用 MySQL 站点上的 .net MySQL 驱动程序从我的 C# 项目中读取 MySQL 数据库。

虽然我对此做了一些研究(包括这个 ),我仍然很困惑为什么会发生这种情况。后来我跑了一次尖峰,但仍然遇到同样的错误。 (在运行之前,我用一些默认值填充了数据库。)这是全部的尖峰代码。

class Program {
    static void Main (string[] args) {
        Console.WriteLine (GetUserAge ("john")); // o/p's -1
    }

    static int GetUserAge (string username) {
        string sql = "select age from users where name=@username";

        int val = -1;
        try {
            using (MySqlConnection cnn = GetConnectionForReading ()) {
                cnn.Open ();
                MySqlCommand myCommand = new MySqlCommand (sql, cnn);
                myCommand.Parameters.AddWithValue ("@username", username);

                using (MySqlDataReader reader = myCommand.ExecuteReader ()) {
                    DataTable dt = new DataTable ();
                    dt.Load (reader);

                    if (reader.Read ()) {
                        val = reader.GetInt32 (0);
                    }
                }
            }
        } catch (Exception ex) {
            Console.WriteLine (ex.Message);
        } finally {
        }

        return val;
    }

    private static MySqlConnection GetConnectionForReading () {
        string conStr = "Data Source=localhost;Database=MyTestDB;User ID=testuser;Password=password";
        return new MySqlConnection (conStr);
    }
}

上面的代码给了我一个例外:“阅读器关闭时尝试阅读无效。”

后来我修改了 if 条件,如下所示:

if (reader.HasRows && reader.Read ()) {
    val = reader.GetInt32 (0);
}

现在 o/p 是 -1。 (数据在表中。)如果由于某种原因结果集有零行,那么读者一开始就不应该进入 if 块。我的意思是,Read() 方法的重点是首先检查结果集中是否有任何行。

我已经无计可施了……只是不知道我哪里出了问题。

感谢您的帮助! :)

I am attempting to read a MySQL database from my C# project using the MySQL drivers for .net off the MySQL site.

Though I did a bit of research on this (including this), I am still flummoxed why this is happening. I later ran a spike and I still get the same error. (Prior to running this I populated the database with some default values.) Here's the spike code in toto.

class Program {
    static void Main (string[] args) {
        Console.WriteLine (GetUserAge ("john")); // o/p's -1
    }

    static int GetUserAge (string username) {
        string sql = "select age from users where name=@username";

        int val = -1;
        try {
            using (MySqlConnection cnn = GetConnectionForReading ()) {
                cnn.Open ();
                MySqlCommand myCommand = new MySqlCommand (sql, cnn);
                myCommand.Parameters.AddWithValue ("@username", username);

                using (MySqlDataReader reader = myCommand.ExecuteReader ()) {
                    DataTable dt = new DataTable ();
                    dt.Load (reader);

                    if (reader.Read ()) {
                        val = reader.GetInt32 (0);
                    }
                }
            }
        } catch (Exception ex) {
            Console.WriteLine (ex.Message);
        } finally {
        }

        return val;
    }

    private static MySqlConnection GetConnectionForReading () {
        string conStr = "Data Source=localhost;Database=MyTestDB;User ID=testuser;Password=password";
        return new MySqlConnection (conStr);
    }
}

The code above gives me the exception: "Invalid attempt to Read when reader is closed."

Later I modified the if-condition like so:

if (reader.HasRows && reader.Read ()) {
    val = reader.GetInt32 (0);
}

And now the o/p is -1. (The data's in there in the table.) If for some reason the result set had zero rows, the reader should not have got into the if-block in the first place. I mean, the whole point of the Read() method is to check if there are any rows in the result set in the first place.

At my wit's end here... just cannot figure out where I'm going wrong.

Thank you for your help! :)

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

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

发布评论

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

评论(1

じее 2024-12-21 23:33:25

我认为使用 DataTable.Load 会“消耗”阅读器,并且至少将其定位在末尾。它甚至可以解释关闭的连接(但我只是在这里猜测)。如果删除该行会怎样?我认为这里没有任何意义。

I think using DataTable.Load will "consume" the reader and, at the very least, position it at the end. It may even account for the closed connection (but I'm just guessing here). What if you remove that line? I don't think it makes any sense here.

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