【发布时间】:2009-10-14 21:55:37
【问题描述】:
我在填写类型列表时遇到了性能问题。以下是我的简化代码:
public static List<Product> GetProducts()
{
List<Product> products = new List<Product>();
using (DbQuery query = new DbQuery()) //this is database access class
{
query.CommandText = "SELECT ProdID, ProdName,Price FROM SomeTable " ;+
using (IDataReader rdr = query.ExecuteReader())
{
while (rdr.Read())
{
Product prd = new Product();
prd.ProdID = DbQuery.ReadInt ( rdr, "ProdID", -1 );
prd.ProdName = DbQuery.ReadString(rdr, "ProdName", "");
prd.Price = DbQuery.ReadDouble(rdr, "Price", 0);
products.Add(prd);
}
}
}
}
我也有简单的 struct Product (ProdID, ProdName,Price)。
我的问题是执行 GetProducts() 需要 4 秒。该查询返回大约 600 条记录,返回结果需要几毫秒,所以我确信填充产品集合需要所有这些时间。我在这里做一些低效的事情吗?请帮忙。 谢谢, 格尔达
【问题讨论】: