设为首页 加入收藏

TOP

ADO操作总结(二)
2015-11-21 01:57:53 来源: 作者: 【 】 浏览:3
Tags:ADO 操作 总结
果没有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
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇sqlserver中如何批量修改一个表的.. 下一篇PostgreSQL数据库 OLTP高并发请求..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: