It isn't always possible to neatly package all the parameters up in a single object / call. To help with more complicated scenarios, dapper allows the param
parameter to be an IDynamicParameters
instance. If you do this, your custom AddParameters
method is called at the appropriate time and handed the command to append to. In most cases, however, it is sufficient to use the pre-existing DynamicParameters
type:
var p = new DynamicParameters(new { a = 1, b = 2 });
p.Add("c", dbType: DbType.Int32, direction: ParameterDirection.Output);
connection.Execute(@"set @c = @a + @b", p);
int updatedValue = p.Get<int>("@c");
This shows:
Note that due to how RDBMS protocols work, it is usually only reliable to obtain updated parameter values after any data (from a Query
or QueryMultiple` operation) has been fully consumed (for example, on SQL Server, updated parameter values are at the end of the TDS stream).