Why doesn't System.Data.SqlClient.SqlDataAdapter have a FillAsync method?

女生的网名这么多〃 提交于 2019-12-24 04:34:16

问题


Naturally-asynchronous operations are not CPU bound and don't have a thread. For example, file reads are done in the I/O hardware which can be kicked off asynchronously without a thread, enabling concurrency. Whenever I come across resources that discuss this, they always use external-system-bound operations as examples, and they almost always have "like I/O or database operations".

ADO.NET has plenty of async methods, like SqlCommand.ExecuteReaderAsync, but one thing it doesn't have is System.Data.SqlClient.SqlDataAdapter.FillAsync.

Many resources covering this happily suggest that you just use Task.Run, like this one and this one, e.g.:

public Task<DataSet> GetDataSetAsync
(string sConnectionString, string sSQL, params SqlParameter[] parameters)
{
    return Task.Run(() =>
    {
        using (var newConnection = new SqlConnection(sConnectionString))
        using (var mySQLAdapter = new SqlDataAdapter(sSQL, newConnection))
        {
            mySQLAdapter.SelectCommand.CommandType = CommandType.Text;
            if (parameters != null) mySQLAdapter.SelectCommand.Parameters.AddRange(parameters);

            DataSet myDataSet = new DataSet();
            mySQLAdapter.Fill(myDataSet);
            return myDataSet;
        }
    });
}

But that code isn't truly asynchronous: there's still a thread.

So was it an intentional decision not to have FillAsync, and if so, why?

来源:https://stackoverflow.com/questions/51597925/why-doesnt-system-data-sqlclient-sqldataadapter-have-a-fillasync-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!