Read the contents of a column several times with Mysql datareader

0

I am creating a console application that outputs the contents of the "password" column and the "username" column. Does anyone know how I could rewrite the output every time new data appears on the password and username rows?

I had thought about recreating the instance every time and to reassign MySqlDataReader to cmd.ExecuteReader() in this way:

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

But that give this error:

MySql.Data.MySqlClient.MySqlException (0x80004005): There is already an open DataReader associated with this Connection which must be closed first. in MySql.Data.MySqlClient.Interceptors.ExceptionInterceptor.Throw(Exception exception) in MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) in MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex) in MySql.Data.MySqlClient.MySqlCommand.CheckState() in MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) in MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() in showaph.Program.Main(String[] args) in C:\Users\utente\source\repos\showaph\Program.cs:riga 51

How could I solve?

This is my full code:

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();
        }
    }
}
c#
mysql
mysqldatareader
asked on Stack Overflow Jan 31, 2021 by Pietro Contadini • edited Feb 1, 2021 by T.S.

1 Answer

0

you need to close your reader

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

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

answered on Stack Overflow Jan 31, 2021 by T.S.

User contributions licensed under CC BY-SA 3.0