【发布时间】:2011-03-07 00:28:36
【问题描述】:
我喜欢 TPL 中 Parallel.For 和 Parallel.ForEach 扩展方法的简单性。我想知道是否有办法利用类似的东西,甚至是稍微高级一点的任务。
下面是 SqlDataReader 的典型用法,我想知道这是否可行,如果可行,如何用 TPL 中的内容替换下面的 while 循环。因为读者不能提供固定数量的迭代,所以 For 扩展方法是不可能的,这让我处理我收集的任务。我希望有人可能已经解决了这个问题,并通过 ADO.net 制定了一些注意事项。
using (SqlConnection conn = new SqlConnection("myConnString"))
using (SqlCommand comm = new SqlCommand("myQuery", conn))
{
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
// Do something with Reader
}
}
}
【问题讨论】:
标签: c# ado.net .net-4.0 task-parallel-library