I have the following function in a .NET Core 2.0 app.
public DataTable CallDb(string connStr, string sql)
{
var dt = new DataTable();
var da = new SqlDataAdapter(sql, connStr);
da.Fill(dt);
return dt;
}
How to convert it to an async function?
public async Task<DataTable> CallDb(string connStr, string sql)
{
var dt = new DataTable();
var da = new SqlDataAdapter(sql, connStr);
da.Fill(dt); // No FillAsync to await?
return dt;
}
I need to use DataTable
because the sql may return data with different schema. Any better way to handle the dynamical schema?
SqlDataAdapter
was never updated to include the TPL version of the methods. You could do this:But that would be creating a thread that would do nothing useful.
A good approach would be to use something like this:
Of course, some changes like
using
statements should be made. However, here you are using asynchronous calls the right way.