c# - MySQL ADO.Net 嵌套数据读取器

标签 c# mysql nested ado.net datareader

我想用一个 SQL 语句检索帐户列表,然后在运行更多 SQL 语句时循环遍历它们。当我尝试时,出现此错误:

Unhandled Exception: MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.

这是一个例子:

using (MySqlConnection connection = new MySqlConnection("host=..."))
{

    connection.Open();

    using (MySqlCommand cmdAccounts = connection.CreateCommand())
    {

        cmdAccounts.CommandText = "SELECT id, name FROM accounts";

        using (MySqlDataReader accounts = cmdAccounts.ExecuteReader())
        {

            while (accounts.Read())
            {

                Console.WriteLine("Account {0}:", account.GetString("name"));

                using (MySqlCommand cmdPictures = connection.CreateCommand())
                {

                    cmdPictures.CommandText = "SELECT id, width, height FROM pictures WHERE account_id = @accountId";
                    cmdPictures.Parameters.AddWithValue("@accountId", accounts.GetInt32("id"));

                    using (MySqlDataReader pictures = cmdPictures.ExecuteReader())
                    {

                        while (pictures.Read())
                        {
                            Console.WriteLine("\tPicture #{0}: {1} x {2}", pictures.GetInt32("id"), picture2.GetInt32("width"), picture2.GetInt32("height"));
                        }

                    }
                }
            }
        }
    }
}

我是否必须使用数据集,或者有没有办法在 MySQL 中仅使用 DataReader 来做到这一点?

最佳答案

您可以使用 DataReader 执行此操作,但您需要为两个级别的读取器创建一个单独的连接对象:

using (MySqlConnection connection = new MySqlConnection("host=..."))
using (MySqlCommand cmdAccounts = MySqlCommand("SELECT id, name FROM accounts" , connection))
{
    connection.Open();

    using (MySqlConnection connection2 = new MySqlConnection("host=..."))
    using (MySqlCommand cmdPictures = new MySqlCommand("SELECT id, width, height FROM pictures WHERE account_id = @accountId", connection2))
    using (MySqlDataReader accounts = cmdAccounts.ExecuteReader())
    {
        cmdPictures.Parameters.Add("@accountId", MySqlDbType.Int32); 
        connection2.Open()

        while (accounts.Read())
        {    
            Console.WriteLine("Account {0}:", account.getString("name"));

            cmdPictures.Parameters["@accountId"].Value = accounts.GetInt32("id");

            using (MySqlDataReader pictures = cmdPictures.ExecuteReader())
            {
                while (pictures.Read())
                {
                    Console.WriteLine("\tPicture #{0}: {1} x {2}", pictures.GetInt32("id"), picture2.GetInt32("width"), picture2.GetInt32("height"));
                }
            }
        }
    }
}

但即使这样好多了,首先考虑问题的方式仍然是错误的。您确实想JOIN数据库中的两个表:

string sql = 
    "SELECT a.id as AccountID, a.name, p.id as PictureID, p.width, p.height" + 
    " FROM accounts a" +
    " INNER JOIN pictures p on p.account_id = a.id" +
    " ORDER BY a.name, a.id";

using (var cn = new MySqlConnection("host=..."))
using (var cmd = new MySqlCommand(sql, cn))
{
    cn.Open();
    using (var rdr = cmd.ExecuteReader())
    {
        bool reading = rdr.Read();
        while (reading)
        {
            int CurrentAccount = rdr.GetInt32("AccountId");
            Console.WriteLine("Account {0}:", rdr.GetString("name"));

            while (reading && CurrentAccount == rdr.GetInt32("AccountId"))
            {
               Console.WriteLine("\tPicture #{0}: {1} x {2}", 
                 rdr.GetInt32("PictureId"), rdr.GetInt32("width"), rdr.GetInt32("height"));
               reading = rdr.Read();
            }
        }
    }
}

关于c# - MySQL ADO.Net 嵌套数据读取器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48430894/

相关文章:

jQuery - 获取嵌套列表子项的路径

c# - WindowsIdentity 和经典 .Net 应用程序池

c# - 关于如何制作应用程序的建议/代码示例 "Cluster Aware"

c# - 在 C# 中的变量内拆分字符串

mysql - 有没有办法在 Apache Instance on Scale 之前启动 MySQL Instance?

mysql - 准备语句语法错误

php - SQL注入(inject)和addSlashes

Python:嵌套循环的问题。想要多次运行随机变量并对结果求平均值......

jquery - 使用 jquery 获取深度嵌套的 css 元素

c# - asp.net mvc 模型绑定(bind)不绑定(bind)集合