This example shows how to use a stored procedure that executes inside itself a parameterized SELECT query to a table and returns table records that satisfy the query condition as the list of corresponding entity objects, e.g. like this one:
The script for creating a stored procedure and a table for the SQL Server DBMS is as follows:
CREATE TABLE dbo.Department (
DeptNo int NOT NULL,
DName varchar(14),
Loc varchar(13),
PRIMARY KEY (DeptNo)
)
GO
CREATE PROCEDURE dbo.SelectDepartmentByLocation(@Loc varchar(13))
WITH
EXECUTE AS CALLER
AS
SELECT * FROM dbo.Department WHERE Loc = @Loc;
GO
Create a model and add an entity to it corresponding to the Department table, and a method corresponding to the procedure (either at the stage of creation at the Select database objects page of Create Model Wizard, or by dragging the corresponding stored procedure from the Database Explorer window to the diagram area or in the Model Explorer window of an already existing model).
As a result, we will get a method in the model corresponding to the stored procedure:
With the following parameters:
As a result of code generation for the model, the corresponding method will be generated having a signature close to the relevant stored procedure:
C#:
public static IList<testmodellermodel.department> SelectDepartmentByLocation(NHibernate.ISession session, string Loc)
{
NHibernate.IQuery query = session.GetNamedQuery(@"SelectDepartmentByLocation");
query.SetParameter(@"Loc", Loc);
return query.List<testmodellermodel.department>();
}
Visual Basic:
Public Shared Function SelectDepartmentByLocation ( _
session As NHibernate.ISession, Loc As String) As List(Of testmodellerModel.Department)
Dim query As NHibernate.IQuery = session.GetNamedQuery("SelectDepartmentByLocation")
query.SetParameter("Loc", Loc)
Return query.List(Of testmodellerModel.Department)()
End Function
Now it is possible to use this stored procedure in the application with the help of method wrapper. This allows working with the stored procedure with all possible convenience, as wrapper methods are strongly typed, are found by IntelliSense and have signatures close to the corresponding stored procedures.