0

C# Code:

   try
        {
            string mydbConnection = "datasource=localhost;port=3306;username=root;password=Greenford123;";
            MySqlConnection connDB = new MySqlConnection(mydbConnection);
            MySqlCommand cmdDataBase = new MySqlCommand("SELECT * FROM project.student", connDB);
            MySqlDataReader DBReader;
            connDB.Open();
            DBReader = cmdDataBase.ExecuteReader();
            while (DBReader.Read())
            {
                List<string> mylist = new List<string>();
                mylist.Add(DBReader.ToString());

                foreach (var item in mylist)
                {
                    MessageBox.Show("The details are " + item);
                }  
            }
            connDB.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error! " + ex);
        }

Objective:

What I want to achieve in my code is:

-store contents from the DB into a list and then output the list.

However this is the output I get:

OUTPUT = "the details are MySQL.Data.MySqlClient.MySqlDataReader"

1 Answers1

0

Maybe you should iterate through the fields:

while (DBReader.Read())
{
    List<string> mylist = new List<string>();
    for(int i=0; i<DBReader.FieldCount; i++)
        myList.Add(DBReader.GetValue(i).ToString());


    foreach (var item in mylist)
    {
        MessageBox.Show("The details are " + item);
    }  
}

Otherwise your datareader is just converted to string, by default it is just showing its class name.

Psi
  • 6,387
  • 3
  • 16
  • 26