如何从SQL Server存储过程获取返回值到nHibernate中? [英] How to get the return value from a SQL Server Stored procedure into nHibernate?

查看:131
本文介绍了如何从SQL Server存储过程获取返回值到nHibernate中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.数据库平台:SqlServer

1.Database platform: SqlServer

2.数据访问:nHibernate 1.2

2.Data Access: nHibernate 1.2

现在,我们需要通过nHibernate访问存储过程,如下所示:

Now we need access the store procedure by nHibernate,like this:

ALTER PROCEDURE TestProc() 
 AS 
  BEGIN 
    Select * From User 
    Return 1234 
  END 

我知道我可以通过IQuery获取用户列表, 而且我也想获得默认的返回值"1234".

I know I can get the User List by IQuery, And I want to get the default return value "1234" too.

问题:

  1. 如何获取此默认返回值?
  2. 如果不能直接获取,可以通过输出参数获取值吗?

推荐答案

这是我的方法:

在我的.hbm.xml中

in my .hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DocumentManagement.Data"  namespace="DocumentManagement.Data.Repositories" >

<sql-query name="GetDocument">    
<return class="DocumentManagement.Core.Models.PhysicalDocument, DocumentManagement.Core">      
    <return-property column="DocId" name="Id" />      
    <return-property column="Filepath" name="Filepath" />
    <return-property column="Filename" name="Filename" />
</return>
exec Investor_GetDocumentById :userId, :docId
</sql-query>

</hibernate-mapping>

在我的存储库中.

    public PhysicalDocument GetDocumentPath(int userId, int docId)
    {
        var query = Session.GetNamedQuery("GetDocument")
            .SetInt32("userId", userId)
            .SetInt32("docId", docId).List<PhysicalDocument>();

        return query[0];
    }

这篇关于如何从SQL Server存储过程获取返回值到nHibernate中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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