Async For Bulk Copy
Solution 1:
The Task.Runs are adding nothing useful here. Also, don't try to share a single connection object between the two runs of your method. Something like:
staticvoidMain(string[] args)
{
var insert1 = DataTableBulkInsert(DataTable1);
var insert2 = DataTableBulkInsert(DataTable2);
Task.WaitAll( insert1, insert2);
}
publicstaticasync Task DataTableBulkInsert(DataTable Table)
{
using(var localConnection = new SqlConnection(/* connection string */))
{
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(localConnection );
sqlBulkCopy.DestinationTableName = "dbo.DatabaseTable";
localConnection.Open();
await sqlBulkCopy.WriteToServerAsync(Table);
}
}
Normally return await is an anti-pattern, but here you want to use it so that the using statement doesn't close your connection until after the bulk load is complete.
Also, I switched to using Task.WaitAll which actually waits, which is more idiomatic than using Task.WhenAll and then immediately calling Wait on it.
Solution 2:
Task.WhenAll returns a Task object that needs to be awaited or the code that follows continues its normal execution and the main method exits immediately.
Since this is a console application and the main can't be marked as async, you can use this:
Task.WhenAll(insert1, insert2).Wait(); // wait for the returned Task object to CompleteThe normal usage is: await (Task.WhenAll(...)) but you can't mark Main as an async method.
Post a Comment for "Async For Bulk Copy"