【发布时间】:2013-04-27 11:46:43
【问题描述】:
我写了下面的代码来在 ComboBox 中查看我的分数,我把这一切都写在populate() 方法中,我称之为表单加载,但它显示了空的组合框。请告诉我这段代码有什么问题。
我为 DatabaseConnection 创建了一个单独的类。
public void populate()
{
DatabaseConnection connection = new DatabaseConnection();
OleDbCommand cmd = new OleDbCommand("Select score from Info", connection.Connection());
connection.Connection().Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader[0].ToString());
}
connection.Connection().Close();
}
【问题讨论】:
-
没有错,Info表里有记录吗?您确定连接到正确的数据库吗?
-
是的,我已连接到正确的数据库,插入和选择命令工作正常,但这个没有显示任何结果。
-
好吧,那么我将尝试了解问题出在 OleDbDataReader 循环还是在 combobox.Items.Add 中。使用调试器,您可以在进入循环之前放置一个断点,并逐行检查从数据库中返回的内容(如果有的话)
标签: c# ms-access combobox oledbdatareader