1

我正在尝试使用一个调用类的方法studentHelper来启动一个SELECT然后在列表框中显示表数据,表是person,列是personID

这是我的学生助手类

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

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

4

1 回答 1

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如下 ListBox,您还需要设置DisplayMemberDisplayMember

private void btnLoadListBox_Click(object sender, EventArgs e)
{
    DataSet ds = studentHelperClass.selectStudent();
    lsbxTable.DataSource = ds.Tables["personID"];
    lsbxTable.DisplayMember = "Name";
    lsbxTable.ValueMember = "ID";
}
于 2013-09-11T03:49:56.023 回答