惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
A
About on SuperTechFans
博客园 - Franky
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 叶小钗
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Last Week in AI
Last Week in AI
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
V
V2EX
G
Google Developers Blog

博客园 - lauer

通过配置文件添加MIME类型 Myeclipse闪退故障 Log4j快速使用精简版 Eclipse快捷键 10个最有用的快捷键 Java compiler level does not match解决方法 ArcMap常用VBA firefox浏览器中silverlight无法输入问题 C#导入Excel遇到数字字母混合列数据丢失解决 ArcMap计算PolyLine中点VBA ArcGIS Server .NET ADF中的AJAX之深入浅出/CallbackResult详解 地图缓存与瓦片切割步骤 柱状专题图实现例子 控制图层是否显示的方法 ArcGIS Server 开发初步 -- 自定义工具 ArcGIS Server 9.2 实现基于web浏览器的在线编辑 VB的Winsock控件GetData方法获取汉字乱码解决方法 汉字转拼音缩写(VB版) 汉字转拼音缩写(C#版) 汉字转拼音(C#版)
NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配...
lauer · 2018-09-13 · via 博客园 - lauer

NHibernate数据库配置参数在hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="ora10gFactory">
    <!--<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>-->
    <!--property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property-->
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.OracleManagedDataClientDriver</property>
    <property name="connection.connection_string">
      User ID=jg122;Password=jg122;Data Source=127.0.0.1:1521/orcl
    </property>
    <property name="show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <!--<property name="current_session_context_class">managed_web</property>
    <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property>-->
    <property name="hbm2ddl.keywords">none</property>
    <!--加载映射-->
    <mapping assembly="Spring.NHibernate.Model"/>
  </session-factory>
</hibernate-configuration>

其中dialect和driver值,可以从对象浏览器中查看NHibernate.dll的结构进行选择。

    

oracle连接使用的是Oracle.ManagedDataAccess.dll,因此driver选择OracleManagedDataClientDriver,Dialect选择最新的Oracle10gDialect

创建数据库实例类代码:

public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        /// <summary>
        /// 创建ISessionFactory
        /// </summary>
        public static ISessionFactory SessionFactory
        {
            get
            {
                // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
                //var cfg = new Configuration().Configure("");
                //// 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
                //// 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
                ////var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");

                ////如果用户Nunit进行单元测试,写相对路径即可。
                ////var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");

                //sessionFactory = cfg.BuildSessionFactory();


                //配置ISessionFactory
                var cfg = (new Configuration()).Configure();
                return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
            }
        }
    }

系统默认会在部署根目录下从App.config,web.config或者hibernate.cfg.xml查找配置参数;

       如果要指定nhibernate数据库配置文件,可以使用Configure的重载方法,传入路径参数。

方法一:创建多个cfg.xml配置文件,实现连接多个数据库

 1 public class NHibernateHelper
 2     {
 3         private static ISessionFactory _sessionFactory;
 4 
 5         /// <summary>
 6         /// 创建ISessionFactory
 7         /// </summary>
 8         public static ISessionFactory SessionFactory
 9         {
10             get
11             {
12                 // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
13                 //var cfg = new Configuration().Configure("");
14                 //// 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
15                 //// 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
16                 ////var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");
17 
18                 ////如果用户Nunit进行单元测试,写相对路径即可。
19                 ////var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
20 
21                 //sessionFactory = cfg.BuildSessionFactory();
22 
23                 string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate2.cfg.xml";
24                 //配置ISessionFactory
25                 var cfg = (new Configuration()).Configure(path);
26                 return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
27             }
28         }
29     }

方法二:在hibernate.cfg.xml中配置多个数据库连接参数

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

  <session-factory name="sqlite">
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">
      Data Source=D:\code\SummaryTool\CSPlugin\bin\Debug\test.db;Version=3;New=False;
    </property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">true</property>
    <property name="use_outer_join">true</property>
  </session-factory>

  <session-factory name="sqlserver">
   <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="prepare_sql">false</property>
  
   <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
   <property name="connection.connection_string">
        Server=(local);initial catalog=***;Integrated Security=SSPI;User ID=***;Password=***
      </property>
   <property name="show_sql">true</property>
    
   <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
  </session-factory>

</hibernate-configuration>

创建数据库实例代码:

 1 public static ISession GetSession(string strkey)
 2 {
 3     if (!mdic_sessionFactory.ContainsKey(strkey))
 4     {
 5         lock (m_lockHelper)
 6         {
 7             if (!mdic_sessionFactory.ContainsKey(strkey))
 8             {
 9 
10                 string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate.cfg.xml";
11                 //
12                 Configuration m_configurationtmp = new Configuration();
13                 XmlDocument xdoc = new XmlDocument();
14                 xdoc.Load(path);
15 
16 
17                 XmlNode xn = null;
18                 foreach (XmlNode xnsub in xdoc.DocumentElement.ChildNodes)
19                 {
20 
21                     if (xnsub.Attributes["name"].Value == strkey)
22                     {
23 
24                         xn = xnsub;
25                         break;
26                     }
27 
28                 }
29 
30                 XmlTextReader xtr = new XmlTextReader(new StringReader(xn.OuterXml));
31                 m_configurationtmp.Configure(xtr);
32                 //m_configurationtmp.AddAssembly("ServiceCoreModel");
33 
34                 mdic_sessionFactory[strkey] = m_configurationtmp.BuildSessionFactory();
35             }
36         }
37     }
38     return mdic_sessionFactory[strkey].OpenSession();
39 }