|
果没有Open 不知道为嘛Connect会崩溃
if(m_pConnect)
{
///> 如果数据库已经打开则关闭
if(m_pConnect->State)
m_pConnect->Close();
m_pCommand.Release();
m_pConnect = NULL;
}
if(m_pCommand)
{
m_pCommand.Release();
m_pCommand = NULL;
}
}
bool CAdo::Open(TCHAR *szConnectString)
{
try
{
if(m_pConnect)
{
//> 不知道为嘛超时值不起作用
//m_pConnect->ConnectionTimeout = 15;
m_pConnect->ConnectionString = szConnectString;
HRESULT hr = m_pConnect->Open("","","",adConnectUnspecified);
if(SUCCEEDED(hr))
{
///> 设置游标类型
m_pConnect->put_CursorLocation(adUseClient);
return true;
}
}
}
catch(_com_error e)
{
ADOERR(e.ErrorMessage());
return false;
}
return false;
}
_RecordsetPtr CAdo::Exec(TCHAR *szExecSql,VARIANT *recordseffected,CommandTypeEnum cmdtype,structStoreProcParam *Parameters,int nCountParam)
{
///> 数据库连接一定存在而且打开
try
{
if(m_pConnect && m_pConnect->State)
{
///> 使用Command 来执行命令
m_pCommand->ActiveConnection = m_pConnect;
m_pCommand->CommandText = (_bstr_t)szExecSql;
m_pCommand->CommandType = cmdtype;
///> 设置参数
///m_paramOUT=m_CommandPtr->CreateParameter("OUTPARAM",adInteger,adParamOutput,sizeof(int),(_variant_t)(long)0);
_ParameterPtr param[100];
for (int i = 0;i < nCountParam;++i)
{
param[i] = m_pCommand->CreateParameter(Parameters[i].szParamName.c_str(),Parameters[i].DataType,Parameters[i].Direction,Parameters[i].DataSize,Parameters[i].strVal);
m_pCommand->Parameters->Append(param[i]);
}
return m_pCommand->Execute(recordseffected,NULL,cmdtype);
}
}
catch(_com_error e)
{
ADOERR(e.ErrorMessage());
}
return NULL;
}
bool CAdo::OpenRecorder(TCHAR *szSelectSql,_RecordsetPtr &pRes,CursorTypeEnum CursorType,LockTypeEnum LockType)
{
try
{
pRes.CreateInstance(_T("ADODB.Recordset"));
if(pRes)
{
HRESULT hr = pRes->Open(szSelectSql,m_pConnect.GetInterfacePtr(),CursorType,LockType,adCmdText);
if(SUCCEEDED(hr))
return true;
}
}
catch(_com_error e)
{
ADOERR(e.ErrorMessage());
}
return false;
}
bool CAdo::GetStoreProcParamVal(TCHAR *szParamName,VARIANT &val)
{
try
{
if(m_pCommand)
{
val = m_pCommand->Parameters->GetItem(szParamName)->GetValue();
return true;
}
}
catch(_com_error e)
{
ADOERR(e.ErrorMessage());
}
return false;
}
bool CAdo::GetRecordSetCount(TCHAR *szExeSql,long &lcout)
{
VARIANT vt;
vt.vt = VT_I4;
vt.intVal = 0;
_RecordsetPtr prs = Exec(szExeSql,&vt);
if(prs)
{
try
{
///> 获取第一个字段的值
_variant_t vCount = prs->GetCollect((_variant_t)(long)0);
lcout = vCount.lVal;
prs.Release();
return true;
}
catch(_com_error e)
{
prs.Release();
ADOERR(e.ErrorMessage());
}
}
return false;
}
bool CAdo::GetFiledsCount(_RecordsetPtr &pRes,long *lFiledsCount)
{
try
{
HRESULT hr = pRes->Fields->get_Count(lFiledsCount);
if(SUCCEEDED(hr))
return true;
}
catch(_com_error e)
{
ADOERR(e.ErrorMessage());
}
return false;
}
bool CAdo::GetFileds(_RecordsetPtr &pRes,std::vector<_tstring> &vTitle)
{
try
{
vTitle.clear();
///> 记录集为空
if(pRes->BOF)
{
return true;
}
long lfileds = 0;
HRESULT hr = pRes->Fields->get_Count(&lfileds);
if(FAILED(h |