我正在尝试使用名为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();
}
它不起作用,我做错了什么?
答案 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}},如下所示,您还需要设置ListBox
和DisplayMember
DisplayMember