Async ADO.NET或TaskFactory

时间:2013-09-06 21:57:00

标签: asp.net ado.net parallel-processing task-parallel-library

我有4个繁重的数据库表(每个3000万行),我的查询从所有4个表中获取数据并执行联合以组合所有并且查询不返回多行(最多1000个)。所以让我们说整个查询需要20秒才能完成,我想为每个表编写一个单独的查询并使用异步或TPL调用它们然后将结果合并到我的业务对象(c#.Net 4.0)中然后返回到ASP.NET Web表单。

我看过BeginExecuteReader和Task.Factory.StartNew,但我不确定如何将所有内容放在一起,有没有人之前做过这样的事情?不知道怎么办?

1 个答案:

答案 0 :(得分:0)

如果其他人需要的话,我自己最终完成了这项工作。

DataTable dtReturn = new DataTable();
var connection = new SqlConnection((_connectionstring.IndexOf("MultipleActiveResultsets") == -1) ? _connectionstring + "MultipleActiveResultsets=True;" : _connectionstring);
try
{
    connection.Open();

    Task<DataTable>[] tasks = new Task<DataTable>[source.Rows.Count];
    for (int i = 0; i < source.Rows.Count; i++)
    {
        int s = Convert.ToInt32(source.Rows[i][0]);
        Task<DataTable> t = Task.Factory.StartNew(() =>
        {
            var command = connection.CreateCommand();
            command.CommandText = commandText;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandTimeout = timeout;

            foreach (SqlParameter p in parms)
            {
                if (p.ParameterName == "@Sources")
                    command.Parameters.Add(new SqlParameter() { SqlDbType = System.Data.SqlDbType.Structured, ParameterName = p.ParameterName, Value = s.ToString().ToIDList() });
                else
                    command.Parameters.Add(new SqlParameter() { ParameterName = p.ParameterName, Value = p.Value });
            }
            var dr = command.ExecuteReader();
            command.Parameters.Clear();
            DataTable dt = new DataTable();
            if (dr.HasRows)
            {
                dt.Load(dr);
                dr.Close();
            }
            return dt;
        });
        tasks[i] = t;
    }

    Task.WaitAll(tasks);
    if (tasks.Length > 0)
    {
        bool isSchemaCloned = false;
        foreach (var t in tasks)
        {
            if (!isSchemaCloned && t.Result.Columns.Count > 0 )
            {
                dtReturn = t.Result.Clone();
                isSchemaCloned = true; 
            }

            foreach (DataRow r in t.Result.Rows)
            {
                dtReturn.Rows.Add(r.ItemArray);
            }
        }
    }
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    if (connection.State != ConnectionState.Closed)
        connection.Close();
}

return dtReturn;