一、使用配置文件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()
//动态库和普通进程调用数据库方式没有任何区别,以下的程序兼容WINDOWS ODBC数据库连接和LINUX MYSQL连接,是跨平台的。#ifdef WIN32
#include <windows.h>
#include <odbcinst.h>
#include <sqlext.h>
#else
#include <mysql.h>
#include <unistd.h>
#define SQLHANDLE static MYSQL
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
SQLHANDLE hDBEnv, hDBC
int DB_Open(char * dbcn, char * usr, char * pwd)
{
int r
#ifdef WIN32
r = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hDBEnv)
if(r) return 0
r = SQLSetEnvAttr(hDBEnv,SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3,0)
if(r) return 0
r = SQLAllocHandle(SQL_HANDLE_DBC, hDBEnv, &hDBC)
if(r) return 0
r = SQLConnect(hDBC,
(unsigned char *)dbcn, strlen(dbcn),
(unsigned char *)usr, strlen(usr),
(unsigned char *)pwd, strlen(pwd))
return r==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO
#else
mysql_init(&hDBC)
MYSQL * rx = mysql_real_connect(
&hDBC, dbcn, usr, pwd, NULL, 0, NULL, 0)
if(!rx) return 0
return 1
#endif
}//end DB_Open
int DB_Exec(char * sql)
{
#ifdef WIN32
SQLHANDLE hStatement = NULL
SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStatement)
SQLExecDirect(hStatement,(unsigned char *)sql, strlen(sql))
SQLCloseCursor(hStatement)
SQLFreeHandle(SQL_HANDLE_STMT, hStatement)
hStatement = NULL
#else
mysql_real_query (&hDBC, sql, strlen(sql))
#endif
return 1
}//end DB_Exec
int DB_Close(void)
{
#ifdef WIN32
SQLDisconnect(hDBC)
SQLFreeHandle(SQL_HANDLE_DBC, hDBC)
SQLFreeHandle(SQL_HANDLE_ENV, hDBEnv)
#else
mysql_close(&hDBC)
#endif
return 1
}//DB_Close()
#ifndef WIN32
typedef struct tagMySQLRecordset {
MYSQL_RES * hRecord
void * * row
int * size
int cols
} MYSQLRecordset
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)