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配置文件,实现连接多个数据库

 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(); string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate2.cfg.xml";
//配置ISessionFactory
var cfg = (new Configuration()).Configure(path);
return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
}
}
}

方法二:在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>

创建数据库实例代码:

 public static ISession GetSession(string strkey)
{
if (!mdic_sessionFactory.ContainsKey(strkey))
{
lock (m_lockHelper)
{
if (!mdic_sessionFactory.ContainsKey(strkey))
{ string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate.cfg.xml";
//
Configuration m_configurationtmp = new Configuration();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(path); XmlNode xn = null;
foreach (XmlNode xnsub in xdoc.DocumentElement.ChildNodes)
{ if (xnsub.Attributes["name"].Value == strkey)
{ xn = xnsub;
break;
} } XmlTextReader xtr = new XmlTextReader(new StringReader(xn.OuterXml));
m_configurationtmp.Configure(xtr);
//m_configurationtmp.AddAssembly("ServiceCoreModel"); mdic_sessionFactory[strkey] = m_configurationtmp.BuildSessionFactory();
}
}
}
return mdic_sessionFactory[strkey].OpenSession();
}

NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接的更多相关文章

  1. Oracle.ManagedDataAccess.dll 连接Oracle数据库不需要安装客户端

    最开始,连接Oracle 数据是需要安装客户端的,ado.net 后来由于微软未来不再支持 System.Data.OracleClient 这个 Data Provider 的研发,从 .NET 4 ...

  2. 在Visual Studio 2017中,.NET(C#)通过Oracle.ManagedDataAccess来连接Oracle数据库

    C#如何通过Oracle.ManagedDataAccess来连接Oracle数据库 1.先创建一个项目,鼠标右击项目中的引用,再点击管理NuGet程序包(也可以先下载dll文件,再选添加引用),在搜 ...

  3. C#使用托管程序连接Oracle数据库(Oracle.ManagedDataAccess.dll)

    一.概述 Oracle Data Provider for  .NET, Managed Driver: Oracle官方的托管数据库访问组件,单DLL,Oracle.ManagedDataAcces ...

  4. C#连接Oracle数据库的方法(System.Data.OracleClient、Oracle.DataAccess.Client也叫ODP.net、Oracle.ManagedDataAccess.dll)

    官方下载地址(ODP.net)(中文):http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/index.html 官方下载地址(O ...

  5. C#使用Oracle.ManagedDataAccess.dll

    在刚接触C#的时候由于公司使用的就是Oracle数据库,那么C#怎么连接Oracle数据库就成了首要去掌握的知识点了.在那时没有ODP.NET,但visual studio却对Oralce数据库的调用 ...

  6. Oracle.ManagedDataAccess.dll方式操作oracle数据库

    Oracle.ManagedDataAccess.dll方式操作oracle数据库 一.查询语句: using (OracleConnection conn = new OracleConnectio ...

  7. Oracle.ManagedDataAccess.dll

    C#使用Oracle.ManagedDataAccess.dll System.Data.OracleClient程序集没有多大的不同,基本上拿以前使用System.Data.OracleClient ...

  8. 在开发框架中扩展微软企业库,支持使用ODP.NET(Oracle.ManagedDataAccess.dll)访问Oracle数据库

    在前面随笔<在代码生成工具Database2Sharp中使用ODP.NET(Oracle.ManagedDataAccess.dll)访问Oracle数据库,实现免安装Oracle客户端,兼容3 ...

  9. 在代码生成工具Database2Sharp中使用ODP.NET(Oracle.ManagedDataAccess.dll)访问Oracle数据库,实现免安装Oracle客户端,兼容32位64位Oracle驱动

    由于我们开发的辅助工具Database2Sharp需要支持多种数据库,虽然我们一般使用SQLServer来开发应用较多,但是Oracle等其他数据库也是常用的数据库之一,因此也是支持使用Oracle等 ...

随机推荐

  1. 杂记:防火墙、企业微信登陆、RestFrameWork

    192.168.0.250重启后查看端口正常,外部ping得通,但是访问192.168.0.250进不了Nginx欢迎界面 netstat -tlunp 关闭了防火墙就行了,原来80端口都要防火墙. ...

  2. redist命令操作(三)--集合Set,有序集合ZSet

    1.Redis 集合(Set) 参考菜鸟教程:http://www.runoob.com/redis/redis-sets.html Redis 的 Set 是 String 类型的无序集合.集合成员 ...

  3. gulp打开gbk编码的html文件乱码

    先上图,好忧伤:

  4. MATLAB常用函数(不定时更新)

    1.pause 一般情况下pause(a)表示程序暂停a秒后继续执行,但有时候也存在这种情况,程序中只有pause:并没有参数a,这样的意思是程序暂停,按任意键程序继续执行.2.uiwait(h,ti ...

  5. OneNote无法打开链接出现错误:你的组织策略阻止我们为你完成此操作

    首先打开注册表编辑器,按键盘win+r,调出运行窗口,输入regedit打开注册表编辑器 打开HKEY_CURRENT_USER\Software 打开\Classes 最后选中.html,在默认选项 ...

  6. Linux内存管理(二)

    Linux内存管理之二:Linux在X86上的虚拟内存管理 本文档来自网络,并稍有改动. 前言 Linux支持很多硬件运行平台,常用的有:Intel X86,Alpha,Sparc等.对于不能够通用的 ...

  7. MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents

    MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.6 Defining Projections and Extents 一.前言 当在m ...

  8. learning websocket protocol

    websocket的产生背景: 众所周知,Web应用的通信过程通常是客户端通过浏览器发出一个请求,服务器端接收请求后进行处理并返回结果给客户端,客户端浏览器将信息呈现.这种机制对于信息变化不是特别频繁 ...

  9. week8

    ---恢复内容开始--- week8 1.动态导入模块 2.断言 3.ftp 4.socketserver 1.动态导入模块 import importlib a = importlib.import ...

  10. hdu1856

    Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more ...