如何使用Nhibernate动态连接Oracle数据库
1个回答
展开全部
关于如何使用Nhibernate配置连接其支持的数据源,网上一些相关的文档已经详细作了说明,同时在其2.0帮助文档中也有相关的描述。一般常用的有两种方式:
一、使用配置文件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=hzga;Password=hzga;Data Source=orcl;Persist 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=hzga;Password=hzga;Data Source=orcl;Persist 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.1;User ID=hzga;Password=hzga;Data 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.1;User ID=lportal;Password=lportal;Data 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();
一、使用配置文件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=hzga;Password=hzga;Data Source=orcl;Persist 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=hzga;Password=hzga;Data Source=orcl;Persist 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.1;User ID=hzga;Password=hzga;Data 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.1;User ID=lportal;Password=lportal;Data 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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询