2013-02-19 114 views
2

我在加载表单中运行这个函数来显示从我的数据库到c#中的dataGridView的信息。问题是在尝试访问广告之前弹出窗体来检索数据。这是它在调试器中的运行方式。我运行下面的函数。使用C#连接到MySql

public void loadData() 
{ 
    var list = mysql.Select(); 
    try 
    { 
     //start from first row 
     for (int i = 0; i < dataGridView1.RowCount; i++) 
     { 
      //insert IDs 
      dataGridView1[0, i].Value = list[0][i]; 
      //insert Names 
      dataGridView1[1, i].Value = list[1][i]; 
      for (int j = 0; j < dataGridView1.ColumnCount; j++) 
      { 
       if (list[3][j] != null) 
       { 
        dataGridView1[j + 2, i].Value = list[3][j]; 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

然后它调用mysql类中的Select函数并停止在MySqlDataReader并弹出窗口窗体。有人可以告诉我什么是攻击?

这里是Mysql.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; 
    } 
} 
+2

替换无意义的冠军,你的问题的具体问题,请。 – abatishchev 2013-02-19 01:40:27

+0

@AaronLS他/她只是删除该代码 - 它在'form_load' – 2013-02-19 01:42:12

+0

你是什么意思停止?它是否达到了你设置的一个突破点?有错误吗?你有没有尝试在while(dataReader.Read())行上放置一个断点? – Maciej 2013-02-19 01:44:13

回答

3

虽然它的处理MySQL命令,并取出由它出现在应用程序已给定的时间来绘制表格数据库信息。

简单的解决办法是做这样的:

private void frmMain_Load(object sender, EventArgs e) 
    { 
      this.visible = false; 
      loadData(); 
      this.visible = true; 
    } 
+0

它仍然在MySqlDataReader弹出窗体。我还能做什么? – user1874435 2013-02-19 01:50:35

+0

将'Select()'代码放入'try'' catch'我认为这里有一个空例外 – tttony 2013-02-19 03:55:23

+0

谢谢tttony,你是我的救命! – user1874435 2013-02-19 06:46:11