Click here to Skip to main content
13,085,613 members (68,624 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 22 Jul 2008

NHibernate Simple VB.Net Example Tutorial

, 22 Jul 2008
Rate this:
Please Sign up or sign in to vote.
NHibernate using VB.Net


This article will help you create a simple VB.Net project using NHibernate

Background Information

Files Required:
1.) Download NHibernate1.2.1.GA.msi and install it in your computer.
This installation will contain nhibernate.dll files for .net framework 1.1 and 2.0 .
2.) Create a new project (Web or console or windows application)
3.) Add Reference Nhibernate.dll and NHibernate.Mapping.Attributes
4.) Create a Project with the name EmployeeManagement (or) anything you wish

Using the code

Employee.VB :

 // Create a Employee class with corresponding get and set methods

Namespace Domain
    Public Class Employee
        Private _EmpID As Integer
        Private _firstname As String
        Private _lastname As String
        Private _age As String
        Private _address As String
        Public Property EmpID() As Integer
                Return _EmpID
            End Get
            Set(ByVal Value As Integer)
                _EmpID = Value
            End Set
        End Property
        Public Property firstname() As String
                Return _firstname
            End Get
            Set(ByVal Value As String)
                _firstname = Value
            End Set
        End Property
        Public Property lastname() As String
                Return _lastname
            End Get
            Set(ByVal Value As String)
                _lastname = Value
            End Set
        End Property
        Public Property age() As Integer
                Return _age
            End Get
            Set(ByVal Value As Integer)
                _age = Value
            End Set
        End Property
        Public Property address() As String
                Return _address
            End Get
            Set(ByVal Value As String)
                _address = Value
            End Set
        End Property
    End Class
End Namespace            

Employee.hbm.xml: (Make sure you set the compile option as embedded Resource)

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="EmployeeManagement.Domain.Employee,EmployeeManagement" lazy="false">
    <id name="EmpID" type="integer" column="ID">
        <generator class="native" />

    <property name="firstname" type="string" column="FirstName"/>
    <property name="lastname" type="string" column="LastName"/>
    <property name="age" type="integer" column="Age"/>
    <property name="address" type="string" column="Address"/>

Web.Config (Or) App.Config:

 <!--<span class="code-comment"> Copy this code into your web.config or App.Config just below <configuration> tag --></span>

        <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

 <add key="hibernate.show_sql" value="false" />
 <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
 <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
 <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
 <add key="hibernate.connection.connection_string" value="************<Your dbconnectionstring>********************" />

DbSession.vb: (This class creates the session and takes care of saving the data to the database)

Imports System
Imports System.Reflection
Imports NHibernate
Imports NHibernate.Cfg

Namespace NhibernateDB
    Public Class DbSession
        Public Sub SaveEmployeeToDatabase(ByVal emp As Domain.Employee)
            Dim session As ISession
            Dim transaction As ITransaction
                session = OpenSession()
                transaction = session.BeginTransaction()
            Catch ex As Exception
                Throw ex
                transaction = Nothing
                session = Nothing
            End Try
        End Sub

        Public Function OpenSession() As ISession
            Dim myConfig As NHibernate.Cfg.Configuration = New NHibernate.Cfg.Configuration
                Dim SessionFactory As ISessionFactory = myConfig.BuildSessionFactory()
                Return SessionFactory.OpenSession()
            Catch ex As Exception
                Throw ex
            End Try
        End Function
    End Class
End Namespace            

Button click event to invoke the save function

Dim empObj As New Domain.Employee
Dim dbObj As New NhibernateDB.DbSession


empObj.firstname = firstname.Text
empObj.lastname = lastname.Text

If Not age.Text.Trim = "" Then
empObj.age = Convert.ToInt32(age.Text)
empObj.age = 0
End If

empObj.address = address.Text


Catch ex As Exception
Throw ex
empObj = Nothing
dbObj = Nothing
End Try


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHellooo Pin
HGIS20-Jan-13 17:35
memberHGIS20-Jan-13 17:35 
hi i'm new user in NHibernate.
i'd like to use 2005 with the backend Oracle 10g or 9i

firstly what is the best release of NHibernate can i use?

where can i find the NHibernate installer or the visual studio support package ??

i already visied the website but is seem that every thing for java not for visual studio !!!!!

thank for you efforts.
GeneralDatasets and Nhibernate Pin
questprogrammer23-Jul-08 9:19
memberquestprogrammer23-Jul-08 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 22 Jul 2008
Article Copyright 2008 by questprogrammer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid