C# 存储过程中await关键字放在哪里

我想实现一个async功能。我的问题是;我不知道在下面的代码中将await关键字放在哪里:


public async Task<List<ManualReadTag>> GetManuallyReadTags(ParameterManualTags model)

{

    var db = new ApplicationDbContext();

    using (var cnxn = db.Database.Connection)

    {

        cnxn.Open();


        var cmd = cnxn.CreateCommand();

        cmd.CommandText = "GetManualReadForDedicated";

        cmd.CommandType = CommandType.StoredProcedure;


        var dtFrom = cmd.CreateParameter();

        dtFrom.ParameterName = "@DateFrom";

        dtFrom.DbType = DbType.Date;

        dtFrom.Direction = ParameterDirection.Input;

        dtFrom.Value = model.DateFrom;


        var dTo = cmd.CreateParameter();

        dTo.ParameterName = "@DateTo";

        dTo.DbType = DbType.Date;

        dTo.Direction = ParameterDirection.Input;

        dTo.Value = model.DateTo;


        var lane = cmd.CreateParameter();

        lane.ParameterName = "@Lane";

        lane.DbType = DbType.Int32;

        lane.Direction = ParameterDirection.Input;

        lane.Value = model.Lane;


        var plaza = cmd.CreateParameter();

        plaza.ParameterName = "@Plaza";

        plaza.DbType = DbType.String;

        plaza.Direction = ParameterDirection.Input;

        plaza.Value = model.Plaza;


        cmd.Parameters.Add(dtFrom);

        cmd.Parameters.Add(dTo);

        cmd.Parameters.Add(lane);

        cmd.Parameters.Add(plaza);


        try

        {

            using (var reader = cmd.ExecuteReader())

            {

                var result = ((IObjectContextAdapter) db)

                    .ObjectContext

                    .Translate<ManualReadTag>(reader)

                    .ToList();

                return result;

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

            MessageBox.Show(ex.Message);

            return null;

        }

    }

}

这段代码有效,我只需要输入 intawait关键字,这样我就可以使用async. 您能指出我正确的方法吗?


呼唤远方
浏览 120回答 2
2回答

神不在的星期二

您可以尝试ExecuteReaderAsync改为:await cmd.ExecuteReaderAsync()

拉莫斯之舞

到处都有异步 API。并且不要忘记丢弃所有一次性用品。public async Task<List<ManualReadTag>> GetManuallyReadTagsAsync(ParameterManualTags model){&nbsp; &nbsp; var db = new ApplicationDbContext();&nbsp; &nbsp; using (var cnxn = db.Database.Connection)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (var cmd = cnxn.CreateCommand())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.CommandText = "GetManualReadForDedicated";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.CommandType = CommandType.StoredProcedure;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dtFrom = cmd.CreateParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtFrom.ParameterName = "@DateFrom";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtFrom.DbType = DbType.Date;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtFrom.Direction = ParameterDirection.Input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtFrom.Value = model.DateFrom;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dTo = cmd.CreateParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dTo.ParameterName = "@DateTo";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dTo.DbType = DbType.Date;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dTo.Direction = ParameterDirection.Input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dTo.Value = model.DateTo;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lane = cmd.CreateParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lane.ParameterName = "@Lane";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lane.DbType = DbType.Int32;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lane.Direction = ParameterDirection.Input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lane.Value = model.Lane;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var plaza = cmd.CreateParameter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plaza.ParameterName = "@Plaza";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plaza.DbType = DbType.String;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plaza.Direction = ParameterDirection.Input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plaza.Value = model.Plaza;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.Add(dtFrom);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.Add(dTo);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.Add(lane);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.Add(plaza);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await cnxn.OpenAsync();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var reader = await cmd.ExecuteReaderAsync())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = ((IObjectContextAdapter)db)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ObjectContext&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Translate<ManualReadTag>(reader)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP