Search code examples
t-sqldapperdapper-extensions

Dapper QueryMultiple Stored Procedures w/o mapping to Objects


With dapper, I can do batch execute for Stored Procedures, something similar to:

connection.Execute(@"
  exec sp1 @i = @one, @y = @two 
  exec sp2 @i = @three",
  new { one = 1, two = 2, three = 3 });

However, the only means of retrieving data that I have seen till now is by using

results.Read<Type>()

What if the results don't map to an object? For instance, I am writing "generic" code to execute any SP with variable in/out parameters & result sets.

Thanks


Solution

  • What API do you want? If you can process the grids separately: do that:

    using(var multi = connection.QueryMultiple(...))
    {
        while(!multi.IsConsumed) {
            // ...
        }
    }
    

    where ... has access to:

    • Read() for dynamic rows - noting that each row also implements IDictionary<string,object>
    • Read<T>() for typed rows via generics
    • Read(Type) for typed rows without generics
    • Read<DapperRow>() (actually, this is just the T that Read<T>() uses to implement Read(), but perhaps more convenient), which provides slightly more access to metadata

    If you want to drop to a raw IDataReader, do that:

    using(var reader = connection.ExecuteReader(...)) {
        // whatever you want
    }
    

    With regards to parameters: the DynamicParameters class provides much richer access to parameter control, including parameter-direction etc.