使用ORMLite和输出参数的ServiceStack MARS(多个活动结果集) [英] ServiceStack MARS (Multiple Active Result Sets) using ORMLite and Output Parameters

查看:113
本文介绍了使用ORMLite和输出参数的ServiceStack MARS(多个活动结果集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ServiceStack ORMLite很棒,我通常会避开ORM的想法,他们倾向于构建数据库,因为构建数据库而不是1:1类模型是有意义的。就是说,有几件事似乎让我陷入困境,我敢肯定这只是我的无知而已。

ServiceStack ORMLite is great, I've typically steered clear of the ORM mentality preferring to build databases as it makes sense to build databases instead of a 1:1 class model. That said, there are a couple of things that I seem to be running into difficulty around, I'm certain it's simply my ignorance shining through.

第一:

是否可以使用ORMLite管理多个结果集?我知道可以使用Dapper来使用QueryMultiple方法,但是出于任何原因,我都在浪费时间弄清楚如何使用ServiceStack的内置Dapper实现。

Is there a way to manage multiple result sets using ORMLite? I know that one can use the QueryMultiple method using Dapper, but for whatever reason I'm having a bear of a time figuring out how to use the built-in Dapper implementation of ServiceStack.

第二个:

是否可以使用ORMLite返回存储过程调用中的输出参数?

Is there a way using ORMLite to return output parameters within a stored procedure call?

理想情况下,我想避开MARS和输出参数,理想情况下,我希望生活在理想的世界中:)

Ideally, I'd like to steer clear of MARS and output parameters and ideally I'd like to live in an ideal world :)

我正在使用。 NET Framework 4.5,SQL Server 2008 R2和ServiceStack 3.9.46。

I'm using .NET framework 4.5, SQL Server 2008 R2 and ServiceStack 3.9.46.

推荐答案

事实证明,这确实非常简单(提供)您知道实现它的魔力)。

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

基于文档和一个貌似误导性的帖子,表明 Dapper被包含在剃须刀中 I假定当暗示Dapper是内置时,它实质上是所包含库的一部分。

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.

如果可以的话,请笑,但是对于那些没有成见的人,我将概述如何显示Dapper扩展程序。因此,这就是魔术。

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.

使用Package Manager控制台执行以下操作:

Using the Package Manager console execute the following:

Install-Package ServiceStack
Install-Package Dapper

添加以下using语句( C#)到您的服务:

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

using ServiceStack.OrmLite;
using Dapper;

现在,当您利用Db对象时,所有的OrmLite AND 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");

为了管理MARS(假设您有一个返回两个结果集和一个输出参数的SP) :

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 :)

这是一个更复杂的示例

希望这可以帮助其他人,并节省一些时间。

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

这篇关于使用ORMLite和输出参数的ServiceStack MARS(多个活动结果集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆