1. 先 下载 NHibernate 框架, 我用的是 2.1 版;
2. 新建一 Asp.net Web 应用程序 NHTest ,并添加引用:
NHibernate.dll ,NHibernate.ByteCode.LinFu.dll ,LinFu.DynamicProxy.dll
3. 修改 Web.config 以配置 NHibernate 。在 configSections 节点内加入:
<
section
name
="hibernate-configuration"
type ="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
type ="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
然后在 与 configSections 平级的节点上增加:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
hibernate-configuration
xmlns
="urn:nhibernate-configuration-2.2"
>
< session-factory name ="NHTest" >
< property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >
server=127.0.0.1; uid=sa; pwd=123456; database=testcenter
</ property >
< property name ="adonet.batch_size" > 10 </ property >
< property name ="show_sql" > true </ property >
< property name ="dialect" > NHibernate.Dialect.MsSql2000Dialect </ property >
< property name ="use_outer_join" > true </ property >
< property name ="command_timeout" > 60 </ property >
< property name ="query.substitutions" > true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" > NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </ property >
< mapping assembly ="NHTest" />
</ session-factory >
</ hibernate-configuration >
< session-factory name ="NHTest" >
< property name ="connection.driver_class" > NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >
server=127.0.0.1; uid=sa; pwd=123456; database=testcenter
</ property >
< property name ="adonet.batch_size" > 10 </ property >
< property name ="show_sql" > true </ property >
< property name ="dialect" > NHibernate.Dialect.MsSql2000Dialect </ property >
< property name ="use_outer_join" > true </ property >
< property name ="command_timeout" > 60 </ property >
< property name ="query.substitutions" > true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" > NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </ property >
< mapping assembly ="NHTest" />
</ session-factory >
</ hibernate-configuration >
4. 在数据库里新建一表 NHUser :
![](https://i-blog.csdnimg.cn/blog_migrate/d6561d37b260208712eeac047075e6bc.jpeg)
5. 新建 NHUser.CS
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
namespace NHTest
{
public class NHUser
{
public NHUser() { }
public virtual int UserId
{ get ; set ; }
public virtual string UserName
{ get ; set ; }
public virtual string NickName
{ get ; set ; }
public virtual string UserPass
{ get ; set ; }
public virtual DateTime regTime
{ get ; set ; }
public virtual string regIp
{ get ; set ; }
}
}
namespace NHTest
{
public class NHUser
{
public NHUser() { }
public virtual int UserId
{ get ; set ; }
public virtual string UserName
{ get ; set ; }
public virtual string NickName
{ get ; set ; }
public virtual string UserPass
{ get ; set ; }
public virtual DateTime regTime
{ get ; set ; }
public virtual string regIp
{ get ; set ; }
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?
xml version="1.0" encoding="utf-8"
?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="NHTest.NHUser,NHTest" table ="NHUser" >
< id name ="UserId" column ="UserId" >
< generator class ="native" />
</ id >
< property name ="UserName" />
< property name ="NickName" />
< property name ="UserPass" />
< property name ="regTime" />
< property name ="regIp" />
</ class >
</ hibernate-mapping >
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="NHTest.NHUser,NHTest" table ="NHUser" >
< id name ="UserId" column ="UserId" >
< generator class ="native" />
</ id >
< property name ="UserName" />
< property name ="NickName" />
< property name ="UserPass" />
< property name ="regTime" />
< property name ="regIp" />
</ class >
</ hibernate-mapping >
注: 由于 UserId 在数据库里设置的是自增长 ID ,这里的 <generator class="native"/> 要设置为 native, 否则会引发 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 "NH_User" 中的标识列插入显式值 的异常。
下面就是如何使用了。在 default.aspx 页面中拖下几个控件:
后台 CS 代码 ,引用命名空间 :
using NHibernate;
using NHibernate.Cfg;
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
private
void
AddUser() {
Configuration cfg = new Configuration();
cfg.AddAssembly( " NHTest " );
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction trans = session.BeginTransaction();
NHUser u = new NHTest.NHUser()
{
NickName = tbnick.Text,
UserName = tbuname.Text,
UserPass = tbpass.Text,
regTime = DateTime.Now,
regIp = Request.UserHostAddress
};
session.Save(u);
session.Flush();
trans.Commit();
}
protected void btnOK_Click( object sender, EventArgs e)
{
AddUser();
}
Configuration cfg = new Configuration();
cfg.AddAssembly( " NHTest " );
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction trans = session.BeginTransaction();
NHUser u = new NHTest.NHUser()
{
NickName = tbnick.Text,
UserName = tbuname.Text,
UserPass = tbpass.Text,
regTime = DateTime.Now,
regIp = Request.UserHostAddress
};
session.Save(u);
session.Flush();
trans.Commit();
}
protected void btnOK_Click( object sender, EventArgs e)
{
AddUser();
}
经测试,运行正常。