Dapper QueryMultiple Stored Procedures w/o mapping to Objects

主宰稳场 提交于 2019-12-23 22:36:37

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25620620/dapper-querymultiple-stored-procedures-w-o-mapping-to-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!