Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mapping OUT from SQL in Dapper

Without using Dapper, this code returns the correct result of "true":

using(connection= new SqlConnection(connectionString))
{
    using(var cmd = connection.CreateCommand())
    {
        cmd.CommandText= query;
        cmd.CommandType= CommandType.StoredProcedure;
        cmd.CommandTimeout = commandTimeout;

        var pDeviceId = new SqlParameter
        {
            ParameterName = "@DeviceId",
            DbType = DbType.String,
            Size = 150,
            Direction = ParameterDirection.Input,
            Value = parameter.DeviceId
        };
        cmd.Parameters.Add(pDeviceId);

        var pResponse = new SqlParameter
        {
            ParameterName = "@Response",
            DbType = DbType.Boolean,
            Direction = ParameterDirection.Output,
        };
        cmd.Parameters.Add(pResponse);

        await connection.OpenAsync();

        int i = cmd.ExecuteNonQuery();
        var response = (bool)cmd.Parameters["@Response"].Value;

        return response;
    }
}

But when using Dapper, I can’t get this code to work. It always returns a value of "False":

using (connection = new SqlConnection(connectionString))
{
    using(var cmd = connection.CreateCommand())
    {
        var parameters = new DynamicParameters();
        parameters.Add("@DeviceId", parameter.DeviceId);
        parameters.Add("@Response", dbType: DbType.Boolean, direction: ParameterDirection.Output);

        var reply = (await connection.QueryAsync<bool>(
            query,
            param: parameters,
            commandType: System.Data.CommandType.StoredProcedure).ConfigureAwait(false)).FirstOrDefault();
        return reply;
    }
}

SOLUTION AS PER YONG SHUN

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var parameters = new DynamicParameters();
                    parameters.Add("@DeviceId", parameter.DeviceId);
                    parameters.Add("@Response", dbType: DbType.Boolean, direction: ParameterDirection.Output);

                    var reply = (await connection.QueryAsync<bool>(
                        query,
                        param: parameters,
                        commandType: System.Data.CommandType.StoredProcedure).ConfigureAwait(false)).FirstOrDefault();
                    return parameters.Get<bool>("@Response");

Thank you.

>Solution :

From here,

var reply = (await connection.QueryAsync<bool>(
            query,
            param: parameters,
            commandType: System.Data.CommandType.StoredProcedure).ConfigureAwait(false)).FirstOrDefault();

this will get the value from the SELECT statement from the stored procedure, but not the value from the Output parameter.

To get the value from the output parameter, you should do as below:

await connection.ExecuteAsync(
    query,
    param: parameters,
    commandType: System.Data.CommandType.StoredProcedure);

var reply = parameters.Get<bool>("@Response");

References

  1. Dapper/ProcedureTest.cs (TestDateTime2LosePrecisionInDynamicParameters method)

  2. Using Parameters With Dapper (Dapper Output Parameter section)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading