NHibernate VB.Net 简单示例教程






2.10/5 (4投票s)
使用 VB.Net 的 NHibernate
引言
本文将帮助您使用 NHibernate 创建一个简单的 VB.Net 项目
背景信息
所需文件
1.) 下载 NHibernate1.2.1.GA.msi 并安装到您的计算机上。
此安装将包含适用于 .net framework 1.1 和 2.0 的 nhibernate.dll 文件。
2.) 创建一个新项目(Web 或控制台或 Windows 应用程序)
3.) 添加引用 Nhibernate.dll 和 NHibernate.Mapping.Attributes
4.) 创建一个名为 EmployeeManagement(或您想要的任何名称)的项目
使用代码
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 Get Return _EmpID End Get Set(ByVal Value As Integer) _EmpID = Value End Set End Property Public Property firstname() As String Get Return _firstname End Get Set(ByVal Value As String) _firstname = Value End Set End Property Public Property lastname() As String Get Return _lastname End Get Set(ByVal Value As String) _lastname = Value End Set End Property Public Property age() As Integer Get Return _age End Get Set(ByVal Value As Integer) _age = Value End Set End Property Public Property address() As String Get Return _address End Get Set(ByVal Value As String) _address = Value End Set End Property End Class End Namespace
Employee.hbm.xml:(确保将编译选项设置为嵌入式资源)
<?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" /> </id> <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"/> </class> </hibernate-mapping>
Web.Config (或) App.Config
<!-- Copy this code into your web.config or App.Config just below <configuration> tag --> <configSections> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <nhibernate> <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>********************" /> </nhibernate>
DbSession.vb:(此类创建会话并负责将数据保存到数据库)
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 Try session = OpenSession() transaction = session.BeginTransaction() session.Save(emp) transaction.Commit() Catch ex As Exception Throw ex Finally session.Close() transaction = Nothing session = Nothing End Try End Sub Public Function OpenSession() As ISession Dim myConfig As NHibernate.Cfg.Configuration = New NHibernate.Cfg.Configuration Try myConfig.AddAssembly("EmployeeManagement") Dim SessionFactory As ISessionFactory = myConfig.BuildSessionFactory() Return SessionFactory.OpenSession() Catch ex As Exception Throw ex End Try End Function End Class End Namespace
按钮点击事件以调用保存函数
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)
Else
empObj.age = 0
End If
empObj.address = address.Text
dbObj.SaveEmployeeToDatabase(empObj)
Catch ex As Exception
Throw ex
最后
empObj = Nothing
dbObj = Nothing
End Try