我遇到了这个问题,我不明白它是如何工作的 当我运行程序时,Form1假设从mysql.Select()获取结果并将其保存在列表中。它假设打开MessageBox,但它没有。所以我注释掉list = mysql.Select(),然后它就可以了。是什么导致这个问题? Mysql_schedule是一个不同的类。 感谢
public List<String>[] list;
MySql_Schedule mysql = new MySql_Schedule();
private void Form1_Load(object sender, EventArgs e)
{
loadData();
}
public void loadData()
{
list = mysql.Select();
MessageBox.show("testing");
for (int i = 0; i < dataGridView1.RowCount; i++)
{
//insert IDs
dataGridView1[0,i].Value = "1";// list[0][i];
//insert Names
dataGridView1[1,i].Value = "testing"; //list[1][i];
}
}
这是我在myslq_schedule中的select函数
public List <string> [] Select()
{
string query = "SELECT id,name,weekday,description FROM employee e INNER JOIN schedule s ON e.id=s.id";
//Create a list to store the result
List<string>[] list = new List<string>[4];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
list[3] = new List<string>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["id"] + "");
list[1].Add(dataReader["name"] + "");
list[2].Add(dataReader["weekday"] + "");
list[3].Add(dataReader["description"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}