WIN7 64位系统+vs2010 32位+OCCI连接Oracl 32位
最近用Oracl做项目,使用ODBC连接数据速度不理想,所有用OCCI操作数据.使用过程中发现连接数据没有问题但在查询是使用getString会内存异常.release编译里createConnection异常.解决方法是下最新的OCCI动态库分别将动态分别放到Debug(oraocci11d.dll)和Release(oraocci11.dll)中.将Debug中的oraocci11d.dll改名为oraocci11.dll.千万别放错位置否则会有其它问题.如:ORA-24960: 属性 OCI_ATTR_USERNAME 的长度大于最大允许长度 255
1)下载安装新版Instant Client Setup 32位的官网上有.http://www.oracle.com/technetwork/cn/database/features/instant-client/index-092699-zhs.html?ssSourceSiteId=ocomcn
2)下载解压最新版occivc 32位的.http://www.oracle.com/technetwork/database/occidownloads-083553.html
3)新建MFC工程.设置
4).h中加入
#include <occi.h>
using namespace oracle::occi;
using namespace std;
#pragma comment(lib,"oci.lib")
#pragma comment(lib,"ociw32.lib")
#pragma comment(lib,"oraocci11.lib")
//定义
public:
string username,password,connstring; //数据库连接信息
Environment* m_pEnv;
Connection* m_pConn;
Statement *pStmt;
bool b_Conn;
bool CreateConnect(); //创建连接
void CloseConnect(); //关闭连接
5).cpp中加入
//创建连接
bool CTestDlgDlg::CreateConnect()
{
//"pymanager","pymanager","192.168.1.110:1521/PYORCL"
try
{
m_pEnv=NULL;
m_pConn=NULL;
pStmt=NULL;
m_pEnv=Environment::createEnvironment();
m_pConn=m_pEnv->createConnection(username,password,connstring);
pStmt=m_pConn->createStatement();
b_Conn=true;
}
catch (SQLException& ex)
{
MessageBox(ex.what());
b_Conn=false;
}
return b_Conn;
}
//关闭连接
void CTestDlgDlg::CloseConnect()
{
if(pStmt!=NULL)
{
m_pConn->terminateStatement(pStmt); //关闭Statement
pStmt = NULL;
}
if(m_pConn!=NULL)
{
m_pEnv->terminateConnection(m_pConn); //关闭连接
m_pConn = NULL;
}
if(m_pEnv!=NULL)
{
Environment::terminateEnvironment(m_pEnv); //释放
m_pEnv = NULL;
}
}
void CTestDlgDlg::OnBnClickedButton3() //这个根据自己实际情况写 我这里写到了一个按钮里了.
{
//COCCIEx m_occi;
//m_occi.InitConnect("pymanager","pymanager","192.168.1.110:1521/PYORCL");
//m_occi.CreateConnect();
//MessageBox(m_occi.GetLastError());
SYSTEMTIME systime1,systime2; //查看运算时间
ULARGE_INTEGER fTime1;
ULARGE_INTEGER fTime2;
GetSystemTime(&systime1);
username="pymanager";
password="pymanager";
connstring="192.168.1.110:1521/PYORCL";
CreateConnect();
if(b_Conn)//创建连接
{
CString sTemp;
ResultSet *pRs;
string sql="select t.* from cdqycsj t where drsj = (select max(drsj) from cdqycsj) order by sjrq";
string s;
try
{
//pStmt->setPrefetchRowCount(3);
pStmt->setSQL(sql);
pRs=pStmt->executeQuery();
while(pRs->next())
{
for(int i=1;i<=10;i++)
{
s=pRs->getString(i);
m_ListBox.AddString(s.c_str());
}
}
pStmt->closeResultSet(pRs);
}
catch(SQLException& ex)
{
MessageBox(ex.what());
return;
}
}
CloseConnect();//关闭连接
CString s_DrawTime;
GetSystemTime(&systime2);
SystemTimeToFileTime(&systime1,(FILETIME*)&fTime1);
SystemTimeToFileTime(&systime2,(FILETIME*)&fTime2);
s_DrawTime.Format("%d",(fTime2.QuadPart - fTime1.QuadPart)/10000);
MessageBox(s_DrawTime);
}
5)将前面下载的"最新版occivc 32"中的oraocci11d.dll和oraocci11.dll分别放在编绎后的Debug和Release中.将oraocci11d.dll改名为oraocci11.dll.
6)大功告成,编绎运行吧.