【问题标题】:Calling parametrized stored procedure using NHibernate使用 NHibernate 调用参数化存储过程
【发布时间】:2013-02-19 19:01:54
【问题描述】:

我想调用一个返回ClassIDStudent id的存储过程

我的 XML 映射是

<sql-query name="GetClassRevenuebyStudent_Sea">
    <return-scalar column="totalRevenew" type="System.String" />
    exec GetClassRevenue_Sea ClassID, StudentID
</sql-query>

而我的存储过程调用代码是

public static double Student_ShowRevenue(string classid, string studentid)
{
    ISession session = NHibernateHelper.GetSession();
    ITransaction trans = session.BeginTransaction();
    IQuery query = (IQuery)session.GetNamedQuery("GetClassRevenuebyStudent_Sea");
    query.SetParameter("ClassID", classid);
    query.SetParameter("StudentID", studentid);

    return Convert.ToDouble(query.List()[0]); 
}

但是当我运行应用程序时,它会在行上给出异常

return Convert.ToDouble(query.List()[0])

所有参数都没有设置。

【问题讨论】:

  • 您是否尝试过像 exec GetClassRevenue_Sea :ClassID, :StudentID 一样定义您的 SP(参数名称前有冒号)?
  • 什么样的异常? (你能给出整个例外吗?)
  • QueryException: All parameters are not set. 类型异常。

标签: c# .net xml nhibernate stored-procedures


【解决方案1】:

您需要在映射中的ClassIdStudentId 属性前面放置一个冒号,以表明它们是您要设置的参数的占位符:

<sql-query name="GetClassRevenuebyStudent_Sea">
    <return-scalar column="totalRevenew" type="System.String" />
    exec GetClassRevenue_Sea :ClassID, :StudentID <!-- notice the colon (:) -->
</sql-query>

然后你可以从你的代码中调用命名查询,

IQuery query = session.GetNamedQuery("GetClassRevenuebyStudent_Sea")
   .SetParameter("ClassID", classid)
   .SetParameter("StudentID", studentid);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2012-05-12
    • 2018-08-16
    • 1970-01-01
    相关资源
    最近更新 更多