一、使用配置文件nfig)连接数据库
下面以使用Nhibernate2.2连接Oracle10数据库为例,给出连接配置信息如下:
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="nnection_string">User ID=hzgaPassword=hzgaData Source=orclPersist Security Info=True</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</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>
</session-factory>
</hibernate-configuration>
二、通过提供一个IDictionary实例连接数据库
下面还是以使用Nhibernate2.2连接Oracle10数据库为例,给出连接配置信息如下:
cfg = new Configuration()
IDictionary<string, string>connProps = new Dictionary<string, string>()
connProps.Add("connection.driver_class", "NHibernate.Driver.OracleClientDriver")
connProps.Add("nnection_string", "User ID=hzgaPassword=hzgaData Source=orclPersist Security Info=True")
connProps.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'")
connProps.Add("dialect", "NHibernate.Dialect.Oracle10gDialect")
connProps.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
cfg.SetProperties(connProps)
cfg.AddAssembly(AssemblyName)
sessions = cfg.BuildSessionFactory()
三、如何使用Nhibernate动态直接连接Oracle数据库
上述两种方式都可以成功连接到Oracle数据库,但是前提条件是运行程序时需要安装Oracle客户端。在程序发布部署的时候安装Oracle客户端是一件非常麻烦的事件,那么有没有一种比较好的方法可以直接连接Oracle数据库,同时又不必安装Oracle客户端呢。
答案是肯定的,下面提供两种方式直接连接Oracle数据库:
1、通过OleDB和Oracle公司的驱动
cfg = new Configuration()
IDictionary<string, string>connProps = new Dictionary<string, string>()
connProps.Add("connection.driver_class", "NHibernate.Driver.OleDbDriver")
connProps.Add("nnection_string", "Provider=OraOLEDB.Oracle.1User ID=hzgaPassword=hzgaData Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.13.148)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))")
connProps.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'")
connProps.Add("dialect", "NHibernate.Dialect.Oracle10gDialect")
connProps.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
cfg.SetProperties(connProps)
cfg.AddAssembly(AssemblyName)
sessions = cfg.BuildSessionFactory()
2、通过OLEDB和微软公司的Oracle驱动
cfg = new Configuration()
IDictionary<string, string>connProps = new Dictionary<string, string>()
connProps.Add("connection.driver_class", "NHibernate.Driver.OleDbDriver")
connProps.Add("nnection_string", " Provider=MSDAORA.1User ID=lportalPassword=lportalData Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = zhbrserver)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))")
connProps.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'")
connProps.Add("dialect", "NHibernate.Dialect.Oracle10gDialect")
connProps.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
cfg.SetProperties(connProps)
cfg.AddAssembly(AssemblyName)
sessions = cfg.BuildSessionFactory()
既然有spring和hibernate做整合,那么数据库连接肯定是交给spring来管的。spring通常会创建数据连接,然后在创建事务等 *** 作,你直接配置多个数据连接应该就可以了。不过实话说我个人没有尝试过,但感觉应该可以,因为网上有很多实例。百度的一篇参考文章:
http://wenku.baidu.com/view/b7f5e10216fc700abb68fcd5.html
另一篇:
http://wenku.baidu.com/view/31e64cc79ec3d5bbfd0a74e0.html
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)