如何使用Nhibernate动态连接Oracle数据库
1个回答
展开全部
一、使用配置文件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();
下面以使用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();
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询