我正在使用 c#、sqlce、数据表。我选择带有此查询的产品:
select price from products where productid=myproduct and mydate between startdate and finaldate and clientid=myclient
但这需要很长时间。 所以我可以使用数据表来存储产品:
datatable prices = new datatable();
prices=this.returntable("select productid,startdate,finaldate,client from products")
for(int i=0;i<allproductsihave;i++) {
product.newprice=this.getmyprice(prices,product.client,product.id,product.date);
}
private decimal getmyprice(datatable products,string myclient, string myproduct, datetime mydate)
{
//how do I do at here the equivalent to first sql query?
//for select a product from a datatable where my date is between 2 dates,
//and client=myclient and productid=myproduct
return Convert.Todecimal(datatable.prices[something]["price"].tostring());
}
通过这种方式,我不应该为每个产品查询连接到数据库。 可能吗?
maybe doing convert.todatetime to startdate and finaldate?
请您参考如下方法:
听起来您不想对数据库发出多个请求,因为您担心建立多个连接会比较慢?如果您不想连接每个产品,为什么不直接使用“where product in ('xxx', 'yyy')”呢?然后你有一个查询调用,并且数据库没有任何变化。然后,您可以在返回结果时对其进行处理。
你的老板应该和@pilotcam 谈谈。他是对的。它在 C# 中并不比在数据库中快。 :)
事实上,在 C# 中执行此操作意味着您正在获取您永远不会使用的信息(如果它是远程数据库,我假设通过网络),因此它可能更慢,具体多少取决于数据库中的数据量这不会出现在您的最终结果中!
