Search code examples
dapperpetapoco

Handling varying number of result sets in Petapoco or Dapper?


I have used QueryMultiple before for handling multiple result sets, but I knew the exact number of result sets being returned. In this case, when I call a stored proc with QueryMultiple, the number of result sets returned varies. Is it possible to handle this in PetaPoco or another orm such as Dapper?


Solution

  • Dapper's QueryMultiple method returns a GridReader; a GridReader has a .IsConsumed property that should change to true when you've read all the available result sets, so that might work:

    using(var reader = conn.QueryMultiple(...)) {
        do {
            var data = reader.Read<...>().AsList();
            // ...
        } while(!reader.IsConsumed);
    }
    

    Alternatively, Dapper has an ExecuteReader method that just does the "pack the parameters and invoke step", and a GetTypeDeserializer method that exposes just the "materialize a row into an object" code, so you could manually combine those, i.e.:

    using(var reader = conn.ExecuteReader(...)) {
        do {
            var parser = SqlMapper.GetTypeDeserializer(...);
            while(reader.Read()) {
                var obj = parser(reader);
                // ...
            }
        } while(reader.NextResult());
    }
    

    (I don't know much about petapoco, sorry)