Dapper Output parameter interpreted as null by mysql

c# dapper mysql

Question

I am trying to run this simple Dapper example from the test suite in Mysql 5.6.10 (AWS Aurora).

var p = new DynamicParameters(new { a = 1, b = 2 });
p.Add("c", dbType: DbType.Int32, direction:ParameterDirection.Output);
cnn.Execute(@"set @c = @a + @b", p);
var results = p.Get<int>("@c");

However, an exception is being thrown. The @c is being interpreted as NULL> enter image description here

I would like to use two output parameters to return the number of rows affected by an insert statement and the last inserted ID.

Popular Answer

I found it seems impossible to fetch out parameter from mysql with dapper, so I modified you code a bit to achieve the result.

var p = new DynamicParameters(new { a = 1, b = 2 });
//p.Add("c", dbType: DbType.Int32, direction: ParameterDirection.Output);
//conn.Execute(@"set @c = @a + @b", p);
using (var reader = conn.ExecuteReader(@"set @c = @a + @b; select @c;", p))
{
    //var results = p.Get<int>("@c");
    if (reader.Read())
    {
        var result = reader.GetInt32(0);
    }
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why