I'm adding async implementations to all my SQL base classes used in my WebAPI projects. I'm fairly new to the TAP paradigm so I'm still learning.
I know, thanks to other posts, that spawning a thread via Task.Run() does not have any performance benefits in an ASP.NET context. So I'm being extra careful with my implementations.
I've changed my QueryExecutor method to the async implementation below. But cannot figure out what the best way to load the DataTable is. I'm guessing I would ideally want to use reader.ReadAsync()
to populate the DataTable but it seems there isn't anything available in the .NET 4.5 framework for that.
So I wanted to ask would it be worth writing my own extension method such as DataTable.LoadAsync(DbDataReader reader)
? I kind of don't want to if it can be helped since it won't be nearly as fool-proof as managed .Net code.
Let me know what you guys think.
private async Task<DataTable> ExecuteQueryInternalAsync(string commandText, CommandType commandType, SqlConnection sqlConnection, SqlTransaction transaction, params SqlParameter[] parameters)
{
using (SqlCommand cmd = new SqlCommand(commandText, sqlConnection) { CommandType = commandType, CommandTimeout = this.config.MainConnectionTimeoutInSeconds })
{
if (transaction != null)
cmd.Transaction = transaction;
if (parameters != null)
{
foreach (var parameter in parameters)
{
if (parameter != null)
{
if (parameter.Value == null)
parameter.Value = DBNull.Value;
cmd.Parameters.Add(parameter);
}
}
}
if (sqlConnection.State == ConnectionState.Closed)
await sqlConnection.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
//Want to use: reader.ReadAsync()
var tb = new DataTable();
tb.Load(reader);
return tb;
}
}
}
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…