Friday, January 9

Using Stored Procedure in NHibernate 2.0

Suppose you have a stored procedure named GetStudent for table, Student, the mapper be Students.hbm.xml and entity class Students.cs.

Let the stored procedure be the following (Example)

ALTER PROCEDURE [dbo].[GetStudent]
-- Add the parameters for the stored procedure here
(@Id int)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT * FROM Student WHERE id=@Id;
END

Then add the following code in Students.hbm.xml



Add the following code in program file

IQuery query = session.GetNamedQuery("GetStudent");
query.SetInt32("Id",id );
IList student = query.List();

This way you can use any stored procedure :)

No comments: