I have a stored procedure that logs some data, how can I call this with NHibernate?
我有一个记录一些数据的存储过程,如何用NHibernate调用它?
So far I have:
到目前为止,我有:
ISession session = ....
IQuery query = session.CreateQuery("exec LogData @Time=:time @Data=:data");
query.SetDateTime("time", time);
query.SetString("data", data);
query.?????;
What should the method ?????
be? Or am doing something more fundamentally wrong?
应该怎么办??????是?或者我正在做一些更根本的错误?
7 个解决方案
#1
ExecuteUpdate on SQL Query should help you.
SQL Query上的ExecuteUpdate可以帮到你。
Sample:
ISession session = ....
IQuery query = session.CreateSQLQuery("exec LogData @Time=:time, @Data=:data");
query.SetDateTime("time", time);
query.SetString("data", data);
query.ExecuteUpdate();
#2
This seems to be a limitation of NHibernate, from NHibernate Documentation:
这似乎是NHibernate的限制,来自NHibernate文档:
The procedure must return a result set. NHibernate will use IDbCommand.ExecuteReader() to obtain the results.
该过程必须返回结果集。 NHibernate将使用IDbCommand.ExecuteReader()来获取结果。
#3
NHibernate allows you to do object-oriented programming and takes care of fetching the objects from and saving the objects to the database behind the scenes.
NHibernate允许您进行面向对象的编程,并负责从后台获取对象并将对象保存到数据库中。
NHibernate does not provide you with an easy API for simply executing stored procedures, because that doesn't seem to have much to do with object-oriented programming, whether fetching objects or saving them.
NHibernate没有为您提供简单的API来简单地执行存储过程,因为这似乎与面向对象的编程无关,无论是获取对象还是保存它们。
So you are doing something fundamentally wrong in attempting to use NHibernate directly to execute highly procedural code. If you want to use NHibernate, you have to tell it how executing this stored procedure behind the scenes will magically help with fetching objects from and saving objects to the database.
因此,在尝试直接使用NHibernate执行高度过程代码时,您正在做一些根本性的错误。如果你想使用NHibernate,你必须告诉它如何在幕后执行这个存储过程将神奇地帮助从中获取对象并将对象保存到数据库。
You can:
- Use ADO.NET directly, opening a new
IDbConnection
or getting theISession
's connection, creating anIDbCommand
, etc. Do this if you need a one-off approach to executing stored procedures. - Create an NHibernate listener and configure it in the
Configuration
, to execute this stored procedure when certain other events are sent through the NHibernate pipeline. Only do this if this stored procedure should actually be executed every time and only when these events occur.
直接使用ADO.NET,打开新的IDbConnection或获取ISession的连接,创建IDbCommand等。如果您需要一次性的方法来执行存储过程,请执行此操作。
创建一个NHibernate监听器并在Configuration中配置它,以便在通过NHibernate管道发送某些其他事件时执行此存储过程。只有在每次执行此存储过程并且仅在发生这些事件时才执行此操作。
#4
You can use UniqueResult to execute a stored proc that doesn't return anything. I'm using the following to call a stored proc that either inserts or updates a record to track users currently logged in to our ASP.NET MVC site.
您可以使用UniqueResult执行不返回任何内容的存储过程。我正在使用以下内容来调用存储过程,该过程插入或更新记录以跟踪当前登录到我们的ASP.NET MVC站点的用户。
IQuery query = session.GetNamedQuery("UserSession_Save");
query.SetInt32("UserID", userID);
query.SetString("CookieID", cookieID);
query.SetString("Controller", controller);
query.SetString("Action", action);
query.UniqueResult();
#5
In general, calling a procedure that does some other chores and return a result set at the end is not different than making a SELECT
query. Therefore, in the answers above, when executing the query in the last step you need to call
通常,调用执行其他一些杂务并在结尾处返回结果集的过程与进行SELECT查询没有什么不同。因此,在上面的答案中,在最后一步执行查询时需要调用
query.List<T>();
where T
is a POCO object that is defined in your code.
其中T是代码中定义的POCO对象。
#6
Do following solutions:
做以下解决方案:
public void Test(TestEntity TestEntity)
{
IQuery query = NHSession.CreateSQLQuery("exec LogData :Time, :Data");
query.SetParameter("Time", TestEntity.Time);
query.SetParameter("Data", TestEntity.Data);
object obj = query.UniqueResult();
}
#7
A Stored Procedure must return a result set. The first parameter of a procedure must be an OUT that returns a result set. This is done by using a SYS_REFCURSOR type in Oracle 9i or later.Even if you do not want to return any result set you must declare first parameter in stored procedure as CURSOR_NAME OUT SYS_REFCURSOR.
存储过程必须返回结果集。过程的第一个参数必须是返回结果集的OUT。这是通过在Oracle 9i或更高版本中使用SYS_REFCURSOR类型来完成的。即使您不想返回任何结果集,也必须将存储过程中的第一个参数声明为CURSOR_NAME OUT SYS_REFCURSOR。
#1
ExecuteUpdate on SQL Query should help you.
SQL Query上的ExecuteUpdate可以帮到你。
Sample:
ISession session = ....
IQuery query = session.CreateSQLQuery("exec LogData @Time=:time, @Data=:data");
query.SetDateTime("time", time);
query.SetString("data", data);
query.ExecuteUpdate();
#2
This seems to be a limitation of NHibernate, from NHibernate Documentation:
这似乎是NHibernate的限制,来自NHibernate文档:
The procedure must return a result set. NHibernate will use IDbCommand.ExecuteReader() to obtain the results.
该过程必须返回结果集。 NHibernate将使用IDbCommand.ExecuteReader()来获取结果。
#3
NHibernate allows you to do object-oriented programming and takes care of fetching the objects from and saving the objects to the database behind the scenes.
NHibernate允许您进行面向对象的编程,并负责从后台获取对象并将对象保存到数据库中。
NHibernate does not provide you with an easy API for simply executing stored procedures, because that doesn't seem to have much to do with object-oriented programming, whether fetching objects or saving them.
NHibernate没有为您提供简单的API来简单地执行存储过程,因为这似乎与面向对象的编程无关,无论是获取对象还是保存它们。
So you are doing something fundamentally wrong in attempting to use NHibernate directly to execute highly procedural code. If you want to use NHibernate, you have to tell it how executing this stored procedure behind the scenes will magically help with fetching objects from and saving objects to the database.
因此,在尝试直接使用NHibernate执行高度过程代码时,您正在做一些根本性的错误。如果你想使用NHibernate,你必须告诉它如何在幕后执行这个存储过程将神奇地帮助从中获取对象并将对象保存到数据库。
You can:
- Use ADO.NET directly, opening a new
IDbConnection
or getting theISession
's connection, creating anIDbCommand
, etc. Do this if you need a one-off approach to executing stored procedures. - Create an NHibernate listener and configure it in the
Configuration
, to execute this stored procedure when certain other events are sent through the NHibernate pipeline. Only do this if this stored procedure should actually be executed every time and only when these events occur.
直接使用ADO.NET,打开新的IDbConnection或获取ISession的连接,创建IDbCommand等。如果您需要一次性的方法来执行存储过程,请执行此操作。
创建一个NHibernate监听器并在Configuration中配置它,以便在通过NHibernate管道发送某些其他事件时执行此存储过程。只有在每次执行此存储过程并且仅在发生这些事件时才执行此操作。
#4
You can use UniqueResult to execute a stored proc that doesn't return anything. I'm using the following to call a stored proc that either inserts or updates a record to track users currently logged in to our ASP.NET MVC site.
您可以使用UniqueResult执行不返回任何内容的存储过程。我正在使用以下内容来调用存储过程,该过程插入或更新记录以跟踪当前登录到我们的ASP.NET MVC站点的用户。
IQuery query = session.GetNamedQuery("UserSession_Save");
query.SetInt32("UserID", userID);
query.SetString("CookieID", cookieID);
query.SetString("Controller", controller);
query.SetString("Action", action);
query.UniqueResult();
#5
In general, calling a procedure that does some other chores and return a result set at the end is not different than making a SELECT
query. Therefore, in the answers above, when executing the query in the last step you need to call
通常,调用执行其他一些杂务并在结尾处返回结果集的过程与进行SELECT查询没有什么不同。因此,在上面的答案中,在最后一步执行查询时需要调用
query.List<T>();
where T
is a POCO object that is defined in your code.
其中T是代码中定义的POCO对象。
#6
Do following solutions:
做以下解决方案:
public void Test(TestEntity TestEntity)
{
IQuery query = NHSession.CreateSQLQuery("exec LogData :Time, :Data");
query.SetParameter("Time", TestEntity.Time);
query.SetParameter("Data", TestEntity.Data);
object obj = query.UniqueResult();
}
#7
A Stored Procedure must return a result set. The first parameter of a procedure must be an OUT that returns a result set. This is done by using a SYS_REFCURSOR type in Oracle 9i or later.Even if you do not want to return any result set you must declare first parameter in stored procedure as CURSOR_NAME OUT SYS_REFCURSOR.
存储过程必须返回结果集。过程的第一个参数必须是返回结果集的OUT。这是通过在Oracle 9i或更高版本中使用SYS_REFCURSOR类型来完成的。即使您不想返回任何结果集,也必须将存储过程中的第一个参数声明为CURSOR_NAME OUT SYS_REFCURSOR。