C# 如何将sqlDataReader转换为任务异步

C# 如何将sqlDataReader转换为任务异步,c#,async-await,sqlconnection,sqlcommand,C#,Async Await,Sqlconnection,Sqlcommand,我有下面的代码谁能告诉我,如果我可以使这一异步任务 public MDTO GetIe(MDTO dtoItem) { string[] Tables = new string[] { "C", "CValue", "SP", "Ar", "P", "CR", "QC", "SR" }; using (SqlConnection connection = new SqlConnection(ConfigurationManager.Connection

我有下面的代码谁能告诉我,如果我可以使这一异步任务

public  MDTO GetIe(MDTO dtoItem)
    {

        string[] Tables = new string[] { "C", "CValue", "SP", "Ar", "P", "CR", "QC", "SR" };
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["tESTp"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetItem", connection))
            {
                DataSet Result = new DataSet();
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@ProjectId", SqlDbType.VarChar);
                command.Parameters["@ProjectId"].Value = dtoItem.ProjectId;


                connection.Open();
                Result.EnforceConstraints = false;
                Result.Load(command.ExecuteReader(CommandBehavior.CloseConnection), LoadOption.OverwriteChanges, Tables);
                dtoItem.SR = Result;
            }
        }
        return dtoItem;
    }

将command.ExecuteReader更改为异步版本:

public async Task<MDTO> GetIeAsync(MDTO dtoItem)
{
    ...
    connection.Open();
    Result.EnforceConstraints = false;

    SqlDataReader dataReader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection);
    Result.Load(dataReader, LoadOption.OverwriteChanges, Tables);
    dtoItem.SR = Result;
    ...
}
公共异步任务GetIeAsync(MDTO dtoItem) { ... connection.Open(); Result.EnforceConstraints=false; SqlDataReader=await command.ExecuteReaderAsync(CommandBehavior.CloseConnection); 结果.Load(数据读取器、LoadOption.OverwriteChanges、表格); dtoItem.SR=结果; ... }
切换方法的签名,将其标记为
async
并将
async
方法与
一起使用