Chinaunix首页 | 论坛 | 博客
  • 博客访问: 191694
  • 博文数量: 11
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 855
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-16 16:26
文章分类
文章存档

2011年(1)

2010年(1)

2009年(2)

2008年(7)

我的朋友
最近访客

分类:

2009-06-02 11:42:15

作为一种通用的持久层框架,NH可以通过配置来指定所使用的数据库类型,并且可根据不同的数据库调整NH的运行参数。这些配置信息通常保存在配置文件中,当数据库配置发生改变时,我们只需要修改配置文件即可,不需要修改代码和编译。三种方式:
 
1.在代码中添加
Configuration cnfg = new Configuration();
cnfg.SetProperty("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
cnfg.SetProperty("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
cnfg.SetProperty("hibernate.connection.connection_string", "server=.;initial catalog=CKWL;uid=sa;pwd=");
cnfg.SetProperty("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect,NHibernate");
cnfg.SetProperty("hibernate.use_outer_join", "false");
cnfg.SetProperty("hibernate.show_sql", "true");
cnfg.AddAssembly("NhiberConsole.OK");
 
2.APP.CONFIG中配置


   
     

    
   
   
     
     
     
     
     
   

 
 
3.把连接写入hibernate.cfg.xml文件



 
   
    NHibernate.Connection.DriverConnectionProvider
    NHibernate.Driver.SqlClientDriver
    server=192.168.18.201;initial catalog=CKWL;uid=sa;pwd=
    false
    NHibernate.Dialect.MsSql2000Dialect,NHibernate
    true
   
   
 

 
注意:如果采用这种方式的话,则在DAO中必须这样写,
Configuration cnfg = new Configuration();
cnfg.Configure(); //这里是一定要的,他会去读取项目中既有的hibernate.cfg.xml文件.
否则会出现错误如:Could not compile the mapping document: NhiberConsole.Users.hbm.xml
 
4.NHibernate的版本号问题
  这个地方的版本号要跟DLL一致.
| | | | | | | | | | |
阅读(1251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~