I was wondering if it is possible to call a number of different stored procedures with the same parameters asynchronously by using tasks and then waiting for all the results to return.
I have the following:
private Task<DataTable> DataBaseCall(string procedureName, params Pair[] where)
{
DataTable data = new DataTable();
SqlConnection connection = new SqlConnection(connStr);
SqlCommand command = new SqlCommand(procedureName, connection);
connection.Open();
for (int i = 0; i < where.Length; i++)
{
command.Parameters.Add(where[i].First.ToString(), where[i].Second.ToString());
}
var readerTask = Task<SqlDataReader>.Factory.FromAsync(command.BeginExecuteReader, command.EndExecuteReader, null);
return readerTask.ContinueWith(t =>
{
var reader = t.Result;
try
{
reader.Read();
data.Load(reader);
return data;
}
finally
{
reader.Dispose();
command.Connection.Close();
command.Connection.Dispose();
command.Dispose();
}
});
}
Which I call with:
private void SetReportVariables(string reportName, string[] storedProcedureName, string _clientGroup, string _clientCode, string _finYear, string _period)
{
Task[] tasks = new Task[storedProcedureName.Length];
for (int i = 0; i < storedProcedureName.Length; i++)
{
List<Pair> parameters = new List<Pair>();
parameters.Add(new Pair("@ClientGroup", _clientGroup));
parameters.Add(new Pair("@ClientCode", _clientCode));
parameters.Add(new Pair("@FinYear", _finYear));
tasks[i] = DataBaseCall(storedProcedureName[i], parameters.ToArray());
}
Task.WaitAll(tasks);
...........Do something with the DataTables.........
}
I have three questions.
- Can anyone tell me if this is a good way to do this?
- Any idea's why my _finYear variable seems sometimes be omitted, which causes an error.
- Can I return the datatable from the task?
Thanks
Mike