What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 8340 |  Welcome, Guest!   Register  Login
Home > Articles > Pattern and Practices > Execute Stored Procedure in SQL Server using nHibernate

Execute Stored Procedure in SQL Server using nHibernate

Article posted by Ziaulh on 12/14/2009 | Views: 10391 | Category: Pattern and Practices | Level: Beginner red flag


I was facing a lot of challenges to execute Stored Procedure through nHibernate. As I was new to nHibernate technology, Googling also did not help me much on this front and I spent a lot of time to get the actual result. Here, I am sharing with you some tips while doing this type of activity.

Introduction

This article shows how to execute a stored procedure (SP) in Microsoft Visual Studio (C#) using nHibernate. nHibernate requires two things to execute 1) config file and 2) Domain Class as per config file. I will separately expalin all the details about it. Also i would like inform you that I am describing this content on the Concept of Repositary pattern. Please have a look on Repository pattern from internet.

Let our SP name is "GetCustomerOrderHistory" and CustomerID as input parameters and returns ProductName, Total as out output parameters.

Hibernate Config File

Points to remember while writing nHibernate configuration file (*.hbm.xml).

  • All the returned parameters in the stored procedure will be as "return-scalar" with column name and type.
  • Use db aliases instead of db column name in the stored procedure.
  • If your SP has input parameters, then use comma separated ? marks for each.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="GetCustomerOrderHistory"> //code name of your SP
<return-scalar column="ProductName" type="String" /> //all the return parameters 
<return-scalar column="Total" type="Int32" />
exec CustOrderHist :? 
</sql-query>
</hibernate-mapping>
Hibernate Model Class

The following are very important while writing a model class for your hibernate configuration for SP:

  • There will be a property for each returned parameter.
  • There will be only one parametric constructor in your model class and you need to set all private variables with a specific one.
public class CustomerOrderHistory
{
    public CustomerOrderHistory(string productName, int totalQuantity) {
    _productName = productName;
    _totalQuantity = totalQuantity;
}

public string ProductName {
    get { return _productName; }
}

public int TotalQuantity {
    get { return _totalQuantity; }
}

private string _productName;

private int _totalQuantity;
}

Hibernate Model Class

I assume that you are already aware how to initialize/create/mapped nHibernate session.

To execute a stored procedure, hibernate uses the GetNamedQuery method.

public class CustomerOrderHistoryDao 
{
    public List<CustomerOrderHistory> GetCustomerOrderHistory(string customerId) {
    IQuery query = NHibernateSession.GetNamedQuery("GetCustomerOrderHistory")
    query .SetString("CustomerID", customerId)
    query .SetResultTransformer(
    new NHibernate.Transform.AliasToBeanConstructorResultTransformer(
    typeof (CustomerOrderHistory).GetConstructors()[0]));
    return query.List<CustomerOrderHistory>() as List<CustomerOrderHistory>;
}
private ISession NHibernateSession {
    get {
        return NHibernateSessionManager.Instance.GetSession();
        }
    }
}
Conclusion

It is not a difficult job to find the solution, but it is important to remember some tips.  If you need any help or assistance, then please let me know. Thank you.


 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Ziaul Haque

Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, December 14, 2009
Level:Starter
Status: [Member]
Biography:Microsoft technology Specialist
>> Write Response - Respond to this post and get points
Related Posts

In Last articles, http://www.dotnetfunda.com/articles/article1514-how-and-where-microsoft-has-implemented-gof-design-patterns-in-net-framewo-.aspx we saw that how Microsoft has used abstract Factory (GOF design pattern) pattern in .Net framework library. In this article, we will see how Decorator Pattern has used in .Net Framework Library.

Design Patterns or one of the key things in Software Development. These patterns will help to improve coding style and also improves the productivity during development or while design

This is a SRS document for Hospital Patient Information Management System. Where the hospital department people will store the info of the patient who has admitted and would retrieve his info when ever required.

In this section we will cover State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective.

Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups depending on the nature of the design problem they intend to solve.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 5:45:53 PM