1、首先,创建一个ASP.NET站点项目
2、添加引用:
1)引用DALayer.dll程序集
2)把OrmCodeGenerator输出目录下的hibernate.cfg.xml添加到bin目录下。我下载的OrmCodeGenerator默认输出的默认文件名是hibernate.cfg.config,你可以将其扩展名更改为XML或者在OrmCodeGenerator源码中修改输出文件的扩展名。
3、在Default.aspx的前台界面拖放一个GridView控件,然后在Page_Load中写如下代码:
Code
1
using System;
2
using DAL;
3
using Model;
4
5
public partial class _Default : System.Web.UI.Page
6

{
7
DalNewsCatalog dalCatalog = new DalNewsCatalog();
8
NewsCatalog catalog = new NewsCatalog();
9
protected void Page_Load(object sender, EventArgs e)
10
{
11
if (!IsPostBack)
12
{
13
//新增
14
//catalog.CatalogName = "微波炉";
15
//catalog.CatalogCode = "1010";
16
//dalCatalog.Save(catalog);
17
18
//调用直接使用T-Sql查询的方法
19
gvDemo.DataSource = dalCatalog.GetNewsCatalogBySql();
20
21
//读取所有
22
//gvDemo.DataSource = dalCatalog.GetAllNewsCatalog();
23
24
//根据ID查询
25
gvDemo.DataBind = dalCatalog.GetNewsCatalogById(6);
26
gvDemo.DataBind();
27
}
28
}
29
}
30
测试通过!
4、配置数据库连接字符串
1)在Web.config中配置
<?
xml version
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
configuration
>
<
configSections
>
<
section name
=
"
hibernate-configuration
"
type
=
"
NHibernate.Cfg.ConfigurationSectionHandler, NHibernate
"
/>
<
section name
=
"
log4net
"
type
=
"
log4net.Config.Log4NetConfigurationSectionHandler,log4net
"
/>
</
configSections
>
<
hibernate
-
configuration xmlns
=
"
urn:nhibernate-configuration-2.2
"
>
<
session
-
factory
>
<
property name
=
"
connection.provider
"
>
NHibernate.Test.DebugConnectionProvider, NHibernate.Test
</
property
>
<
property name
=
"
cache.provider_class
"
>
NHibernate.Cache.HashtableCacheProvider, NHibernate
</
property
>
<
property name
=
"
cache.use_query_cache
"
>
true
</
property
>
<
property name
=
"
connection.isolation
"
>
ReadCommitted
</
property
>
<!--
This
is
the System.Data.dll provider
for
MSSQL Server
-->
<
property name
=
"
connection.driver_class
"
>
NHibernate.Driver.SqlClientDriver
</
property
>
<
property name
=
"
connection.connection_string
"
>
Server
=
(local);initial catalog
=
demo;uid
=
sa;pwd
=123
</
property
>
<
property name
=
"
show_sql
"
>
false
</
property
>
<
property name
=
"
dialect
"
>
NHibernate.Dialect.MsSql2005Dialect
</
property
>
<
property name
=
"
use_outer_join
"
>
true
</
property
>
<
property name
=
"
command_timeout
"
>
444
</
property
>
<
property name
=
"
query.substitutions
"
>
true
1
,
false
0
, yes
'
Y
'
, no
'
N
'
</
property
>
</
session
-
factory
>
</
hibernate
-
configuration
>
<!--
This section contains the log4net configuration settings
-->
<
log4net debug
=
"
false
"
>
<!--
Define some output appenders
-->
<
appender name
=
"
trace
"
type
=
"
log4net.Appender.TraceAppender, log4net
"
>
<
layout type
=
"
log4net.Layout.PatternLayout,log4net
"
>
<
param name
=
"
ConversionPattern
"
value
=
"
%d [%t] %-5p %c - %m%n
"
/>
</
layout
>
</
appender
>
<
appender name
=
"
console
"
type
=
"
log4net.Appender.ConsoleAppender, log4net
"
>
<
layout type
=
"
log4net.Layout.PatternLayout,log4net
"
>
<
param name
=
"
ConversionPattern
"
value
=
"
%d [%t] %-5p %c - %m%n
"
/>
</
layout
>
</
appender
>
<
appender name
=
"
rollingFile
"
type
=
"
log4net.Appender.RollingFileAppender,log4net
"
>
<
param name
=
"
File
"
value
=
"
log.txt
"
/>
<
param name
=
"
AppendToFile
"
value
=
"
false
"
/>
<
param name
=
"
RollingStyle
"
value
=
"
Date
"
/>
<
param name
=
"
DatePattern
"
value
=
"
yyyy.MM.dd
"
/>
<
param name
=
"
StaticLogFileName
"
value
=
"
true
"
/>
<
layout type
=
"
log4net.Layout.PatternLayout,log4net
"
>
<
param name
=
"
ConversionPattern
"
value
=
"
%d [%t] %-5p %c - %m%n
"
/>
</
layout
>
</
appender
>
<!--
Setup the root category, add the appenders and
set
the
default
priority
-->
<
root
>
<
priority value
=
"
WARN
"
/>
<
appender
-
ref
ref
=
"
console
"
/>
</
root
>
</
log4net
>
</
configuration
>
修改你的数据库版本和连接字符串。
更改DAL项目中所有文件中的如下一行代码
ISessionFactory factory
=
new
Configuration().Configure().BuildSessionFactory();
为
ISessionFactory factory
=
new
Configuration().AddAssembly(
"
AppModel
"
).Configure().BuildSessionFactory();
站点中添加对NHibernate.Test.dll的引用。”AppModel“是Model项目程序集名称。
2)使用单独配置文件
使用代码生成器生成的hibernate.cfg.xml,已拷贝到站点的bin目录下,修改其中的数据库连接字符串就可以了,DAL层中依然使用
ISessionFactory factory
=
new
Configuration().Configure().BuildSessionFactory();
创建ISessionFactory实例。
<?
xml version="1.0" encoding="utf-8"
?>
<
hibernate-configuration
xmlns
="urn:nhibernate-configuration-2.2"
>
<
session-factory
>
<
property
name
="connection.provider"
>
NHibernate.Connection.DriverConnectionProvider
</
property
>
<
property
name
="connection.driver_class"
>
NHibernate.Driver.SqlClientDriver
</
property
>
<
property
name
="connection.connection_string"
>
Server=.;initial catalog=Demo;User Id=sa;Password=123;
</
property
>
<
property
name
="show_sql"
>
true
</
property
>
<
property
name
="dialect"
>
NHibernate.Dialect.MsSql2005Dialect
</
property
>
<
property
name
="use_outer_join"
>
true
</
property
>
<
mapping
assembly
="AppModel"
/>
</
session-factory
>
</
hibernate-configuration
>