How to use Dapper SqlMapper GridReader with a sql resultset?

dapper sql-server-2008

Question

*I have a class with classA and classB's list of properties.*

public class ClassA
{
public string prop1;
public string prop2;
public list<ClassB> Prop3;
}

and take a different class ClassB

public class ClassB
{
public string prop4;
public string prop5;
}

I have a stored process that would run and provide two sets of results. and I want to translate a classA-based result set to a classB-based result set.

I now use Dapper. SqlMapper. GridReader to map my class and read the list of this class.

What is Dapper used for. SqlMapper. Reading the resultset and map using GridReader

1
0
6/29/2012 6:47:23 PM

Accepted Answer

Did you ever try theQueryMultiple method? It is advised that:

Execute a command that returns multiple result sets, and access each in turn

This using statement must be included in order to make QueryMultiple available.

using Dapper; /* to add extended method QueryMultiple public static GridReader QueryMultiple(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); */
67
11/12/2018 4:28:42 PM

Popular Answer

QueryMultiple enables the handling of numerous result sets. We only imposed one design restriction: the grid reader's ability to use buffering was completely disabled. As a result, the whole API is streaming.

You may use: In the simplest scenario.

var grid = connection.QueryMultiple("select 1 select 2");
grid.Read<int>().First().IsEqualTo(1);
grid.Read<int>().First().IsEqualTo(2);

If the situation is a little more complex, you may do insane things like this:

var p = new DynamicParameters();
p.Add("a", 11);
p.Add("r", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

connection.Execute(@"create proc #spEcho
@a int
as 
begin

select @a Id, 'ping' Name, 1 Id, 'pong1' Name
select @a Id, 'ping' Name, 2 Id, 'pong2' Name
return @a
end");

var grid = connection.QueryMultiple("#spEcho", p, 
                                     commandType: CommandType.StoredProcedure);

var result1 = grid.Read<dynamic, dynamic, Tuple<dynamic, dynamic>>(
                  (a, b) => Tuple.Create((object)a, (object)b)).ToList();
var result2 = grid.Read<dynamic, dynamic, Tuple<dynamic, dynamic>>(
                  (a, b) => Tuple.Create((object)a, (object)b)).ToList();

((int)(result1[0].Item1.Id)).IsEqualTo(11);
((int)(result1[0].Item2.Id)).IsEqualTo(1);

((int)(result2[0].Item1.Id)).IsEqualTo(11);
((int)(result2[0].Item2.Id)).IsEqualTo(2);

p.Get<int>("r").IsEqualTo(11);

This using statement must be included in order to make QueryMultiple available.

using Dapper; /* to add extended method QueryMultiple public static GridReader QueryMultiple(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); */


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow