MySQL ADO.Net嵌套数据读取器

时间:2018-01-24 20:16:46

标签: c# mysql nested ado.net datareader

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

  

未处理的异常:MySql.Data.MySqlClient.MySqlException:已经有一个与此Connection关联的打开的DataReader,必须先关闭它。

以下是一个例子:

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"));
                        }

                    }
                }
            }
        }
    }
}

我是否必须使用DataSet,或者只使用DataReaders在MySQL中有这样的方法吗?

2 个答案:

答案 0 :(得分:3)

您可以使用DataReaders执行此操作,但您需要为两个级别的读者提供单独的连接对象:

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"));
                }
            }
        }
    }
}

但即使这样做得更好,但首先考虑这个问题仍然是错误的方式。你真的想加入数据库中的两个表:

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();
            }
        }
    }
}

答案 1 :(得分:1)

为什么不用SqlDataAdapter填充数据表?  然后,foreach在datatable中的行,提取第二个sql命令并显示日期? 只是来自初学者的意见,尝试或检查更多答案