How do I use Dapper to get the return value of stored proc?
You can declare dynamic params with direction: ReturnValue
You can also use "select" instead of "return" and use Query<T>
extension.
create procedure sp_foo
as
begin
return 99
end
[Test]
public void TestStoredProcedure()
{
using (var conn = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo"))
{
var p = new DynamicParameters();
p.Add("@foo", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
conn.Execute("sp_foo", p, commandType: CommandType.StoredProcedure);
var b = p.Get<int>("@foo");
Assert.That(b, Is.EqualTo(99));
}
}
you'll have to define a SqlParameter for your stored procedure with a Direction.ReturnValue