是否可以使用 SqlDbType.Structured 在 NHibernate 中传递表值参数?
我想将 id 集合传递给将使用 NHibernate 进行映射的存储过程。这项技术是在 Sql Server 2008 中引入的(更多信息请参见此处 => 表值参数)。我只是不想在 nvarchar
参数中传递多个 id,然后在 SQL Server 端截断其值。
I want to pass a collection of ids to a stored procedure that will be mapped using NHibernate. This technique was introduced in Sql Server 2008 ( more info here => Table-Valued Parameters ). I just don't want to pass multiple ids within an nvarchar
parameter and then chop its value on the SQL Server side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以毫无麻烦地传递值集合。
示例:
NHibernate 将为每个元素创建一个参数。
You can pass collections of values without the hassle.
Example:
NHibernate will create a parameter for each element.
对于我的情况,需要在打开的事务中间调用我的存储过程。
如果存在打开的事务,则此代码可以工作,因为它会自动重用 NHibernate 会话的现有事务:
但是,对于我的新存储过程,参数并不像
Int64
那么简单。它是一个表值参数(用户定义的表类型)我的问题是我找不到正确的 Set 函数。
我尝试了
SetParameter("spData", tvpObj)
,但它返回此错误:无论如何,经过一些尝试和错误,下面的方法似乎有效。
Enlist()
函数是此方法的关键。它基本上告诉SQLCommand
使用现有事务。没有它,将会出现错误由于我通过这种方法使用
SqlParameter
类,因此SqlDbType.Structured
可用。这是分配 wiSnList 的函数:
For my case, my stored procedure needs to be called in the middle of an open transaction.
If there is an open transaction, this code works because it is automatically reusing the existing transaction of the NHibernate session:
However, for my new Stored Procedure, the parameter is not as simple as an
Int64
. It's a table-valued-parameter (User Defined Table Type)My problem is that I cannot find the proper Set function.
I tried
SetParameter("spData", tvpObj)
, but it's returning this error:Anyways, after some trial and error, this approach below seems to work.
The
Enlist()
function is the key in this approach. It basically tells theSQLCommand
to use the existing transaction. Without it, there will be an error sayingSince I am using the
SqlParameter
class with this approach,SqlDbType.Structured
is available.This is the function where
wiSnList
gets assigned:比接受的答案更简单的解决方案是使用 ADO.NET。 NHibernate 允许用户将 IDbCommands 加入 NHibernate 事务中。
A simpler solution than the accepted answer would be to use ADO.NET. NHibernate allows users to enlist
IDbCommands
into NHibernate transactions.我的第一个临时想法是实现我自己的
IType
。不再执行任何方法;其余的都包含
throw new NotImplementedException();
。接下来,我为IQuery
创建了一个简单的扩展。对我来说典型用法是
好的,但是什么是
"IntTable"
?它是为传递表值参数而创建的 SQL 类型的名称。some_sp
可能就像它当然只适用于 Sql Server 2008,并且在这个特定的实现中使用单列
DataTable
。它只是 POC,不是完整的解决方案,但它可以工作,并且在定制时可能会很有用。如果有人知道更好/更短的解决方案,请告诉我们。
My first, ad hoc, idea was to implement my own
IType
.No more methods are implemented; a
throw new NotImplementedException();
is in all the rest. Next, I created a simple extension forIQuery
.Typical usage for me is
Ok, but what is an
"IntTable"
? It's the name of SQL type created to pass table value arguments.And
some_sp
could be likeIt only works with Sql Server 2008 of course and in this particular implementation with a single-column
DataTable
.It's POC only, not a complete solution, but it works and might be useful when customized. If someone knows a better/shorter solution let us know.