【问题标题】:How to get the return value from a SQL Server Stored procedure into nHibernate?如何从 SQL Server 存储过程中获取返回值到 nHibernate?
【发布时间】:2010-10-30 01:18:48
【问题描述】:

1.数据库平台:SqlServer

2.数据访问:nHibernate 1.2

现在我们需要通过nHibernate访问存储过程,像这样:

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

我知道我可以通过 IQuery 获取用户列表, 我也想得到默认的返回值“1234”。

问题:

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

【问题讨论】:

  • 你为什么要调用“ALTER PROC”?你是要调用实际的proc吗?
  • 不,那是proc定义。关键是他想向我们展示他想要返回的东西。

标签: sql-server nhibernate stored-procedures


【解决方案1】:

NHibernate 不允许您以这种方式使用存储过程。但它确实允许使用普通的旧 ADO.NET API 进行调用。 NHibernate Documentation 表示如果你想使用这些程序,你必须通过 session.Connection 执行它们。这是一个例子-

ISession session = sessionFactory.GetSession();

using(ITransaction transaction = session.BeginTransaction())
{
   IDbCommand command = new SqlCommand();
   command.Connection = session.Connection;

   // Enlist IDbCommand into the NHibernate transaction
   transaction.Enlist(command);

   command.CommandType = CommandType.StoredProcedure;
   command.CommandText = "dbo.SetUserInfo";

   // Set input parameters
   var parm = new SqlParameter("@UserID", SqlDbType.Int);
   parm.Value = 12345;
   command.Parameters.Add(parm); 

   // Set output parameter
   var outputParameter = new SqlParameter("@Quantity", SqlDbType.Int);
   outputParameter.Direction = ParameterDirection.Output;
   command.Parameters.Add(outputParameter); 

   // Set a return value
   var returnParameter = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
   returnParameter.Direction = ParameterDirection.ReturnValue;
   command.Parameters.Add(returnParameter);

   // Execute the stored procedure
   command.ExecuteNonQuery();
}

您可以在这里找到更多详细信息 -

http://refactoringaspnet.blogspot.com/2009/06/how-to-use-legacy-stored-procedures-in.html

【讨论】:

    【解决方案2】:

    我就是这样做的:

    在我的 .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>
    

    在我的 repository.cs 中

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

    【讨论】:

    • 您能否编辑您的答案(或添加评论)以包括您如何映射PhysicalDocument 实体?我对Id 专栏特别感兴趣。谢谢。
    • Id列的映射在hbm中。我希望这有帮助。
    • 这似乎根本没有回答这个问题? OP 询问如何从存储过程中获取返回值,而不是如何映射命名查询..
    【解决方案3】:

    首先,这在我所见过的任何地方都不称为“默认返回值”。这只是返回值。它通常用于返回成功/错误状态。

    我不知道 nHibernate 是如何做事的,但在 ADO.NET 中,您可以使用将 Direction 属性设置为“Return”的参数。也许在 nHibernate 中有一个等价物。

    OTOH,更常见的是使用 OUTPUT 参数返回一个实际有用的值,并将 RETURN 值保留为错误代码或被忽略。

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-11
        • 2010-11-23
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多