ServiceStack MARS (Multiple Active Result Sets) using ORMLite and Output Parameters

南楼画角 提交于 2019-11-30 09:25:36
Ram Argid

It turns out that this is really quite simple (provided you know the magic to make it happen).

Based on the documentation and a seemingly misleading post indicating that Dapper is "included" in razor I assumed that when it was implied that Dapper was "built-in" that it was essentially a part of the included libraries.

Laugh if you will, but for those of us that aren't enlightened, I'm going to outline how to make the Dapper extensions show up. So here's the magic.

Using the Package Manager console execute the following:

Install-Package ServiceStack
Install-Package Dapper

Add the following using statements (C#) to your Service:

using ServiceStack.OrmLite;
using Dapper;

Now, when you leverage the Db object all the OrmLite AND Dapper methods will be there.

To get an output parameter it is now as simple as:

var p = new DynamicParameters();

p.Add("@param1", request.stuff1);
p.Add("@param2", request.stuff2);
p.Add("@param3", dbType: DbType.Int32, direction: ParameterDirection.Output);

Db.Execute("schema.sp_stored_proc_name", p, commandType: CommandType.StoredProcedure);

response.outputStuff = p.Get<int>("@param3");

In order to manage MARS (assume you have a SP that returns two result sets AND an output param):

p.Add("@param1", request.stuff1);
p.Add("@param2", request.stuff2);
p.Add("@param3", dbType: DbType.Int32, direction: ParameterDirection.Output);

var mars = Db.QueryMultiple("schema.sp_stored_proc_name", p, commandType: CommandType.StoredProcedure);

//firstSet contains the first result set
var firstSet = mars.Read().ToList();
//secondSet contains the second result set
var secondSet = mars.Read().ToList();

response.outputStuff = p.Get<int>("param3");

It's beautifully simple, once you know the magic :)

Here's a much more complicated example.

Hopefully this helps someone else out and saves them a bit of time.

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