在C#中将等待关键字放在存储过程中的何处

时间:2019-07-16 05:07:05

标签: c# .net stored-procedures async-await

我想实现一个async函数。我的问题是;我不知道将等待关键字放在以下代码中的位置:

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;
        }
    }
}

此代码有效,我只需要插入await关键字即可使用async。 您能指出正确的方法吗?

2 个答案:

答案 0 :(得分:13)

您可以改用ExecuteReaderAsync

await cmd.ExecuteReaderAsync()

答案 1 :(得分:1)

在任何有异步API的地方。而且,别忘了处理掉所有一次性用品。

public async Task<List<ManualReadTag>> GetManuallyReadTagsAsync(ParameterManualTags model)
{
    var db = new ApplicationDbContext();
    using (var cnxn = db.Database.Connection)
    {
        using (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);

            await cnxn.OpenAsync();

            using (var reader = await cmd.ExecuteReaderAsync())
            {
                var result = ((IObjectContextAdapter)db)
                    .ObjectContext
                    .Translate<ManualReadTag>(reader)
                    .ToList();
                return result;
            }
        }
    }
}