Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the await key word in stored procedure in c#

I want to implement an async function. My problem is; I do not know where to put the await key word in the code below:

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

This code works I just need to put int the await keyword so I can use the async. Could you please point me the right way to do this?

like image 701
Ibanez1408 Avatar asked Dec 08 '22 11:12

Ibanez1408


2 Answers

You can try ExecuteReaderAsync instead:

await cmd.ExecuteReaderAsync()
like image 157
Salah Akbari Avatar answered Dec 24 '22 04:12

Salah Akbari


Everywhere where there's an asynchronous API. And don't forget to dispose of all your disposables.

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;
            }
        }
    }
}
like image 34
Paulo Morgado Avatar answered Dec 24 '22 04:12

Paulo Morgado