0

C#代码:

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

客观的:

我想在我的代码中实现的是:

-将数据库中的内容存储到列表中,然后输出列表。

然而,这是我得到的输出:

OUTPUT = "详细信息是 MySQL.Data.MySqlClient.MySqlDataReader"

4

1 回答 1

0

也许您应该遍历这些字段:

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

否则,您的数据读取器只是转换为字符串,默认情况下它只显示其类名。

于 2017-02-25T14:14:39.493 回答