I have a small dilemma. I have a function which goes through a list of Queries in a dataset with their own connection string (could be same or different) and starts filling in all the DataTables and returns a filled DataSet with all the queries filled in.
Currently, this process is done one by one, so if one of the queries took 10mins and the other 3 queries took 2 minutes each, the Run Time will be 16mins.
I was wondering if it's possible to use multithreading in this situation. This should call FillTable all at separate thread and should get the run time down to 10minutes. These are explicitly just Fill DataTable calls (there won't be any update or delete calls).
This is what I got so far:
public void FillDataSet(ref DataSet Source)
{
foreach (var r in Source.Tables["queries"].Rows)
{
string query = r["QueryStatement"].ToString();
string qSource = r["QuerySource"].ToString();
string tableName = r["TableName"].ToString();
DBConnection db = new DBConnection();
var TempSource = Source;
taskConnection = Task.Factory.StartNew(() => callDB(db, query, tableName, ref TempSource));
Source = TempSource;
}
Task.WaitAll(taskConnection);
}
private void callDB(DBConnection db, string query, string tableName, ref DataSet Source)
{
using (var sql = new SqlConnection(db.ConnectionString))
{
sql.Open();
using (var adp = new SqlDataAdapter(query, sql))
{
adp.SelectCommand.CommandTimeout = 0;
adp.Fill(Source, tableName);
}
}
}
I had to create a TempSource because lambda expression does not like passing in ref of Parameters (I cannot change this). Currently this does not work, not sure what I'm doing wrong.