我想在单个存储过程的执行将返回50个多个单独选择的情况下使用Dapper,单个结果集中没有一个会非常宽,最多可能是20或30列。下面的代码来自Dapper测试,我想知道这个例子是否是一个很好的原型。
谢谢你, 斯蒂芬
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");
}
修改
以下是基于Marcs答案的示例。
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();
}
答案 0 :(得分:22)
这个来自主页,但测试中应该有类似的内容:
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();
...
}
参数等正常工作,如果指定了CommandType,则应直接映射到已定义的参数名称。
对.Read<T>()
的每次调用都与连续的结果网格有关。