【问题标题】:Read the contents of a column several times with Mysql datareader用Mysql datareader多次读取一列的内容
【发布时间】:2021-01-31 21:00:00
【问题描述】:

我正在创建一个控制台应用程序,它输出“密码”列和“用户名”列的内容。有谁知道每次密码和用户名行上出现新数据时我如何重写输出?

我曾想过每次都重新创建实例并以这种方式将MySqlDataReader重新分配给cmd.ExecuteReader()

for (int a = 0; a == 0;)
    {
        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine("Username: " + reader.GetString("username"));
            Console.WriteLine("Password: " + reader.GetString("password") + "\n");
        }
                   
}

但这给出了这个错误:

MySql.Data.MySqlClient.MySqlException (0x80004005):已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭它。 在 MySql.Data.MySqlClient.Interceptors.ExceptionInterceptor.Throw(Exception 异常) 在 MySql.Data.MySqlClient.MySqlConnection.Throw(异常前) 在 MySql.Data.MySqlClient.MySqlCommand.Throw(异常前) 在 MySql.Data.MySqlClient.MySqlCommand.CheckState() 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior 行为)中 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() 在 C:\Users\utente\source\repos\showaph\Program.cs:riga 中的 showaph.Program.Main(String[] args) 51

我该如何解决?

这是我的完整代码:

namespace showaph
{
    class Program
    {            
        static void Main(string[] args)
        {
            try
            {
                MySqlConnection connection = new MySqlConnection("Server=sql7.freemysqlhosting.net; Port=3306; Database=sql7389377; Uid=sql7389377; Pwd=*******; ");              
                connection.Open();
                if (connection.State == ConnectionState.Open)
                {
                    Console.WriteLine("Connected \n");    
                }
                else
                {
                    Console.WriteLine("not connected");
                }
                string query = "SELECT * FROM users";
                MySqlCommand cmd = new MySqlCommand(query, connection);
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("Username: " + reader.GetString("username"));
                        Console.WriteLine("Password: " + reader.GetString("password") + "\n");
                    }

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 一次将所有行读取到某个列表或数组中,然后您可以通过该列表或数组随意(重新)访问它们。附带说明:您似乎可以将密码存储为纯文本。不要那样做。加盐和哈希。
  • @stickybit 你是什么意思?如果我将密码和电子邮件值放在两个数组中,它们是否总是实时更新?

标签: c# mysql mysqldatareader


【解决方案1】:

你需要关闭你的阅读器

using (MySqlDataReader reader = cmd.ExecuteReader())
{

    while (reader.Read())
    {
        Console.WriteLine("Username: " + reader.GetString("username"));
        Console.WriteLine("Password: " + reader.GetString("password") + "\n");
    }
                   
}

【讨论】:

  • 我尝试添加您的代码,但它不起作用
  • 我按照您自己的建议更新了问题中的代码。还是不行
  • @PietroContadini 我的代码是正确的。但是 bugs.mysql.com/bug.php?id=89159 这里有一个错误,所以需要一些解决方法。
  • "您的代码,但它不起作用",您遇到了什么错误?
  • 当我向列添加数据时,它不会在控制台中更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
相关资源
最近更新 更多