11

I'd like to use Dapper in a situation where the execution of a single stored procedure will return 50 multiple separate selects, none of the individual result sets will be very wide, maybe 20 or 30 columns at most. The code below is from the Dapper Tests and I'm wondering if this example is a good prototype to use.

Thank you, Stephen

public void TestMultiMap()
        {
            var createSql = @"
                create table #Users (Id int, Name varchar(20))
                create table #Posts (Id int, OwnerId int, Content varchar(20))

                insert #Users values(99, 'Sam')
                insert #Users values(2, 'I am')

                insert #Posts values(1, 99, 'Sams Post1')
                insert #Posts values(2, 99, 'Sams Post2')
                insert #Posts values(3, null, 'no ones post')";

                connection.Execute(createSql);

            var sql = @"select * from #Posts p 
                      left join #Users u on u.Id = p.OwnerId 
                      Order by p.Id";

            var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post; }).ToList();
            var p = data.First();

            p.Content.IsEqualTo("Sams Post1");
            p.Id.IsEqualTo(1);
            p.Owner.Name.IsEqualTo("Sam");
            p.Owner.Id.IsEqualTo(99);

            data[2].Owner.IsNull();

           connection.Execute("drop table #Users drop table #Posts");
    }

EDIT

Here is a sample based on Marcs answer.

        const string sql = @"__sp_GetMISMOLoanInfo";
        using (var multi = _connection.QueryMultiple(sql, new { loannum = "3192381" }, commandType: CommandType.StoredProcedure))
        {
           var address = multi.Read<ADDRESS>().Single();
           var amortizationRule = multi.Read<AMORTIZATION_RULE>().Single();
           var appraiserLicense = multi.Read<APPRAISER_LICENSE>().Single();
           var automatedUnderwriting = multi.Read<AUTOMATED_UNDERWRITING>().Single();
           var avm = multi.Read<AVM>().Single();
           var borrowerDetail = multi.Read<BORROWER_DETAIL>().Single();
        }
4
  • Where's the SP? In particular, is it a wide result, or multiple separate selects? Both work with dapper, but the syntax is different between the two layouts Commented Jul 19, 2011 at 17:05
  • (I know it was just an example, but a table variable might have been better in that, btw) Commented Jul 19, 2011 at 17:08
  • Marc, I've edited the post to reflect your questions. Commented Jul 19, 2011 at 17:54
  • See codeproject.com/Articles/835519/… for a wrapper that simplifies the task
    – Yulia V
    Commented Jan 25, 2016 at 18:06

1 Answer 1

22

This one is from the home page, but there should be similar in the tests:

var sql = @"...";
using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var customer = multi.Read<Customer>().Single();
   var orders = multi.Read<Order>().ToList();
   var returns = multi.Read<Return>().ToList();
   ...
} 

Arguments etc work as normal, and should map directly to defined parameter names if CommandType is specified.

Each call to .Read<T>() relates to a successive results grid.

4
  • Marc, how do I pass the paramater? Procedure or function '__sp_GetMISMOLoanInfo' expects parameter '@loannum', which was not supplied. Commented Jul 19, 2011 at 18:44
  • @SPATEN did you pass, for example, a new {loannum = yourValue} ? Commented Jul 19, 2011 at 18:47
  • Yes, just like the code I pasted as the EDIT. I've even set it to be a string because that is our 'Loan Number' data type. CREATE PROCEDURE [dbo].[__sp_GetMISMOLoanInfo] @loannum varchar(15) Commented Jul 19, 2011 at 18:53
  • Problem was NOT setting the CommandType as Marc had indicated. Problem solved. Commented Jul 19, 2011 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.