尝试使用SELECT方法在列表框中显示表数据

时间:2013-09-11 03:37:16

标签: c# mysql

我正在尝试使用名为studentHelper的类中的方法启动SELECT,然后在列表框中显示表数据,表格为person和列是personID

这是我的studentHelperClass

class studentHelperClass
{

  /// <summary>
  /// The SELECT method for the student
  /// </summary>
  public static void selectStudent()
  {
      MySqlConnection conn = connection();
      conn.Open();
      MySqlCommand cmd = new MySqlCommand();
      cmd.Connection = conn;
      MySqlDataAdapter adap = new MySqlDataAdapter(@"SELECT * FROM person", conn);
      MySqlCommandBuilder sqlCmd = new MySqlCommandBuilder(adap);
      DataSet sqlSet = new DataSet();
      adap.Fill(sqlSet, "personID");
      conn.Close();
  }

这是我的表格:

private void btnLoadListBox_Click(object sender, EventArgs e)
 {
     studentHelperClass.selectStudent();
 }

它不起作用,我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的帮助方法selectStudent()不会返回任何数据,也不会在UI中显示任何数据。它只填补 数据集。

您可以从selectStudent返回数据集,如下所示

   public static DataSet selectStudent()
    {
        MySqlConnection conn = connection();
        conn.Open();
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = conn;
        MySqlDataAdapter adap = new MySqlDataAdapter(@"SELECT * FROM person", conn);
        MySqlCommandBuilder sqlCmd = new MySqlCommandBuilder(adap);
        DataSet sqlSet = new DataSet();
        adap.Fill(sqlSet, "personID");
        conn.Close();
        return sqlSet;
    }

现在在按钮点击事件中设置DataSource的{​​{1}},如下所示,您还需要设置ListBoxDisplayMember

DisplayMember