我正在使用以下代码来填充我的组合框以获取更多功能。有没有更好的方法在课堂上下文中填充我的组合框?我意识到,如果记录数以千计,那么这可能不是最佳实践。
private void Form1_Load(object sender, EventArgs e)
{
Book myBook = new Book;
myBook.Connect();
comboBox1.DataSource=myBook.IDs();
}
class Book
{
OleDbCommand Comm;
OleDbConnection Conn;
OleDbDataReader Reader;
string queryString;
public void Connect()
{
Conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Book.accdb");
}
public List<string> IDs()
{
string singleID = null;
List<string> IDs = new List<string>();
queryString = "Select bID from Books";
Comm = new OleDbCommand(queryString, Conn);
Reader = Comm.ExecuteReader();
while (Reader.Read())
{
singleID = Reader[0].ToString();
IDs.Add(singleID);
}
Conn.Close();
Reader.Close();
return IDs;
}
}