Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a stored procedure from NHibernate that has no result?

I have a stored procedure that logs some data, how can I call this with 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?

like image 315
thatismatt Avatar asked Jul 07 '09 10:07

thatismatt


People also ask

How do you call a stored procedure?

You can call an SQL stored procedure with the execute, open, or get statement; in each case, you use the #sql directive. A stored procedure is a set of instructions for a database, like a function in EGL.

How do you check where a stored procedure is called?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure.

How can we call stored procedure in hibernate?

You can use createSQLQuery() to call a store procedure directly. Declare your store procedure inside the @NamedNativeQueries annotation. -Call it with getNamedQuery().


2 Answers

ExecuteUpdate on SQL Query should help you.

Sample:

ISession session = .... IQuery query = session.CreateSQLQuery("exec LogData @Time=:time, @Data=:data"); query.SetDateTime("time", time); query.SetString("data", data); query.ExecuteUpdate(); 
like image 119
Sathish Naga Avatar answered Oct 09 '22 09:10

Sathish Naga


This seems to be a limitation of NHibernate, from NHibernate Documentation:

The procedure must return a result set. NHibernate will use IDbCommand.ExecuteReader() to obtain the results.

like image 45
thatismatt Avatar answered Oct 09 '22 09:10

thatismatt