📄 adodb.cpp
字号:
// AdoDB.cpp: implementation of the CAdoDB class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AdoDB.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAdoDB::CAdoDB()
{
ReadInc();
busy=false;
}
CAdoDB::CAdoDB(char* _connstr)
{
strcpy(connetstr,_connstr);
busy=false;
}
CAdoDB::~CAdoDB()
{
Close();
}
bool CAdoDB::ConnectDB()
{
if(!comok) return false;
HRESULT hr;
try{
_bstr_t dsn(connetstr);
hr=m_connect->Open(dsn,_bstr_t(L""),_bstr_t(""),adModeUnknown);
if(SUCCEEDED(hr))
{
m_connect->PutCommandTimeout(0);
return true;
}
}
catch(_com_error &e)
{
_bstr_t msg(e.Description());
// throw CString((LPSTR)msg);
return false;
}
catch(...)
{
// throw CString("未知错误");
return false;
}
return true;
}
bool CAdoDB::ExecuteSql(char* sql)
{
CSingleLock lock(&m_lock);
lock.Lock();
int i=0;
int y=0;
while(1)
{
Sleep(1);
while(i>0)
{
y++;
if(ConnectDB()) break;
Sleep(50);
}
try
{
_bstr_t sqlstr(sql);
_variant_t v(0L);
m_connect->Execute(sqlstr,&v,adOptionUnspecified);
}catch(_com_error &e)
{
if(y>0)
{
_bstr_t msg(e.Description());
return false;
}
/* CString errorcode;
errorcode.Format("%08lx",e.Error());
if(errorcode=="80004005")
{
*/
i++;
m_connect->Close();
continue;
/* }*/
}
catch(...)
{
return false;
}
break;
}
return true;
}
bool CAdoDB::Get_Record(char* sql,IDispatch** rsset,\
enum CursorTypeEnum CursorType,\
enum LockTypeEnum LockType,\
long option)
{
CSingleLock lock(&m_lock);
lock.Lock();
_RecordsetPtr rs;
HRESULT hr;
hr=rs.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
return false;
int i=0;
int y=0;
while(1)
{
Sleep(1);
while(i>0)
{
y++;
if(ConnectDB()) break;
Sleep(50);
}
try{
_bstr_t sqlstr(sql);
rs->Open(sqlstr,(IDispatch*)m_connect,CursorType,LockType,option);
*rsset=(IDispatch*)rs;
rs->AddRef();
}catch(_com_error &e)
{
if(y>0)
{
_bstr_t msg(e.Description());
return false;
}
// CString errorcode;
// errorcode.Format("%08lx",e.Error());
// if(errorcode=="80004005")
// {
i++;
m_connect->Close();
continue;
// }
}catch(...)
{
return false;
}
break;
}
return true;
}
bool CAdoDB::Initconn(CString _server,CString _uid,CString _pwd,CString _db )
{
HRESULT hr;
hr=m_connect.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
comok=true;
else
comok=false;
strcpy(server,(char*)(LPCTSTR)_server);
strcpy(uid,(char*)(LPCTSTR)_uid);
strcpy(pwd,(char*)(LPCTSTR)_pwd);
strcpy(database,(char*)(LPCTSTR)_db);
return comok;
}
bool CAdoDB::Initconn()
{
HRESULT hr;
hr=m_connect.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
comok=true;
else
comok=false;
return comok;
}
void CAdoDB::Close()
{
if(comok)
{
try
{
m_connect->Close();
m_connect.Release();
}catch(...)
{
}
comok=false;
}
}
void CAdoDB::ReadInc()
{
GetPrivateProfileString("Database","server","qq",server,20,CONFIGFILE);
GetPrivateProfileString("Database","uid","sa",uid,15,CONFIGFILE);
GetPrivateProfileString("Database","pwd","sa",pwd,15,CONFIGFILE);
GetPrivateProfileString("Database","database","CDR",database,20,CONFIGFILE);
sprintf(connetstr,"driver={SQL Server};server=%s;uid=%s;pwd=%s;database=%s",server,uid,pwd,database);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -