我在一个名为 PurchaseOrder 的类中有一个方法。在这里,我曾经List<string>
检索供应商列表并将这些列表填充到组合框中。代码确实有效,但随着我在 SQL Server 数据库中添加越来越多的供应商,我在加载表单时遇到了一些缓慢的问题。我怎样才能优化这样的速度?
下面是两组代码,来自 PurchaseOrderClass.cs 的方法 PopulateSuppliers() 和一个 Form_Load 事件。
public object PopulateSuppliers()
{
//Create an instance of a List of Strings.
List<string> ListOfSuppliers = new List<string>();
string sqlString = "SELECT CompanyName FROM tbl_suppliers WHERE Archived = 'False';";
SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection);
sqlConnection.Open();
sqlReader = sqlCommand.ExecuteReader();
while (sqlReader.Read())
{
ListOfSuppliers.Add(sqlReader["CompanyName"].ToString());
}
sqlReader.Close();
sqlConnection.Close();
return ListOfSuppliers;
}
private void frmPurchaseOrderEditor_Load(object sender, EventArgs e)
{
...
//Populate a list of suppliers.
cboSuppliers.DataSource = PurchaseOrder.PopulateSuppliers();
...
}