例外:读取器关闭时无效的读取尝试

时间:2017-11-11 19:36:37

标签: c# mysql

我正在尝试更新我的数据库,同时从中读取数据,但是阅读器会被关闭,并且会引发一个例外,即读取器关闭时无法读取。 update.ExecuteNonQuery()关闭了读者方法吗?

如果我摆脱了con.Close(); con.Open();行,我会There is already an open DataReader associated with this Connection which must be closed first.

那么如何在保持读数打开的同时更新我的​​数据库记录?

{
    public class MySqlReadUpdate
    {
        public int Id { get; set; }
        public string Notes { get; set; }
    }

    static void Main()
    {
        List<MySqlReadUpdate> dbData = new List<MySqlReadUpdate>();

        var config = "server=localhost;user id=root; database=restaurants; pooling=false;SslMode=none;Pooling=True";
        MySqlConnection con = new MySqlConnection(config);

        MySqlDataReader reader = null;
        string query = "SELECT id, notes FROM customers";

        MySqlCommand command = new MySqlCommand(query, con);
        con.Open();
        reader = command.ExecuteReader();

        while (reader.Read())
        {
            MySqlReadUpdate newMySqlReadUpdate = new MySqlReadUpdate();

            newMySqlReadUpdate.Id = (int)reader["id"];
            newMySqlReadUpdate.Notes = (string)reader["notes"];
            string note = newMySqlReadUpdate.Notes;
            var notesplit = note.Split(' ', '\n')[1];
            dbData.Add(newMySqlReadUpdate);
            Console.WriteLine(newMySqlReadUpdate.Id);
            Console.WriteLine(newMySqlReadUpdate.Notes);
            Console.WriteLine(note);
            Console.WriteLine(notesplit);
            con.Close();
            con.Open();

            string query2 = "UPDATE customers SET notes='" + notesplit + "' WHERE id='" + newMySqlReadUpdate.Id + "';";
            MySqlCommand update = new MySqlCommand(query2, con);
            update.ExecuteNonQuery();
        }
        con.Close();

        Console.WriteLine("Finished!");
        Console.Read();

    }
  }
}

1 个答案:

答案 0 :(得分:1)

您不能像现在这样关闭连接,因为数据阅读器依赖于它。致电otsQ15后,您就会打破它。这就是您下次到con.Close时看到异常的原因。

您想要的是在配置字符串中添加reader.Read();但是,正如我们在评论中所讨论的那样,MySQL显然仍然不支持它。因此,答案是两个连接实例。

MultipleActiveResultSets=True

我建议您修改代码以使用 List<MySqlReadUpdate> dbData = new List<MySqlReadUpdate>(); var config = "server=localhost;user id=root;database=restaurants;pooling=false;SslMode=none"; MySqlConnection con = new MySqlConnection(config); MySqlConnection cmdCon = new MySqlConnection(config); MySqlDataReader reader = null; string query = "SELECT id, notes FROM customers"; MySqlCommand command = new MySqlCommand(query, con); con.Open(); cmdCon.Open(); reader = command.ExecuteReader(); while (reader.Read()) { MySqlReadUpdate newMySqlReadUpdate = new MySqlReadUpdate(); newMySqlReadUpdate.Id = (int)reader["id"]; newMySqlReadUpdate.Notes = (string)reader["notes"]; string note = newMySqlReadUpdate.Notes; var notesplit = note.Split(' ', '\n')[1]; dbData.Add(newMySqlReadUpdate); Console.WriteLine(newMySqlReadUpdate.Id); Console.WriteLine(newMySqlReadUpdate.Notes); Console.WriteLine(note); Console.WriteLine(notesplit); string query2 = "UPDATE customers SET notes='" + notesplit + "' WHERE id='" + newMySqlReadUpdate.Id + "';"; MySqlCommand update = new MySqlCommand(query2, cmdCon); update.ExecuteNonQuery(); } con.Close(); cmdCon.Close(); Console.WriteLine("Finished!"); Console.Read(); 语句或在try-finally块中包装。