简体   繁体   English

使用NHibernate调用参数化存储过程

[英]Calling parametrized stored procedure using NHibernate

I want to call a stored procedure that return ClassID and Student id 我想调用一个存储过程,该过程返回ClassIDStudent id

My XML mapping is 我的XML映射是

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

And my stored procedure calling code is 我的存储过程调用代码是

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]); 
}

But when I run the application it gives the exception on the line 但是,当我运行该应用程序时,它会给出异常

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

that all parameters are not set. 没有设置所有参数。

You need to place a colon in front of your ClassId and StudentId properties in your mapping to indicate that they are placeholders for the parameters you are going to set: 您需要在映射中的ClassIdStudentId属性的前面放置一个冒号,以表明它们是您要设置的参数的占位符:

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

then you can call the named query from your code, 然后您可以从代码中调用命名查询,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM