void HiDBOracle::create_rs(const char* name, const char* sql, _RecordsetPtr& rs) { ::CoInitialize(NULL); HRESULT hr = rs.CreateInstance(__uuidof(Recordset)); if (FAILED(hr)) { _com_error e(hr); on_excetion("HiDBOracle::ExecuteScalar", sql, e); } rs->PutCursorLocation(adUseClient); }
int CCommDBOper::ADORecordsetOpen(_ConnectionPtr &pCon,_RecordsetPtr pRs,CString strOpenSql,CString strDcrOpen,int iWarn) { //函数说明:执行指定记录集对象指定SQL操作 //pCon 数据库连接对象 //pRs 当前操作用记录集对象 //strOpenSql 要执行的SQL命令 //strDcrOpen是对要使用SQL命令的描述 _variant_t tempStrSql; tempStrSql = strOpenSql; if (pRs == NULL) { pRs.CreateInstance(__uuidof(Recordset)); pRs->CursorLocation = adUseClient; } ADOCloseRecordset(pRs,0); while (true) { try { //有错 pRs->Open(tempStrSql,_variant_t(pCon),adOpenDynamic,adLockOptimistic,adCmdText); pRs->Open(tempStrSql,(IDispatch *)pCon,adOpenDynamic,adLockOptimistic,adCmdText); break; } catch (_com_error &e) { CString strMsg; strMsg.Format("%s:%d %s", __FILE__, __LINE__, (LPSTR)e.Description()); AfxMessageBox(strMsg); } try { //如果第一次打开失败,看能否以只读方式打开 pRs->Open(tempStrSql,(IDispatch *)pCon,adOpenDynamic,adLockReadOnly,adCmdText); return 2; } catch (_com_error &e) { CString strMsg; strMsg.Format("%s:%d %s", __FILE__, __LINE__, (LPSTR)e.Description()); AfxMessageBox(strMsg); CString errormessage; errormessage.Format("功能说明: %s\r\n记录集对象执行下列SQL命令\r\n%s\r\n失败! 错误信息:%s",strDcrOpen,strOpenSql,e.ErrorMessage()); WarnMessage(errormessage,iWarn);//显示错误信息 return RET_FAILED; } } return RET_OK; }
_RecordsetPtr& GetRecordset(_bstr_t SQL) { m_pRecordset=NULL; try{ if(m_pConnection==NULL) //连接 Connect(); m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pRecordset->Open((_bstr_t)SQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e){ cout<<e.Description()<<endl; m_pRecordset=NULL; return m_pRecordset; } return m_pRecordset; }
DWORD CNHSQLServerDBO::OpenQuery(const _ConnectionPtr &pConnection, _RecordsetPtr &pRecordset, const wchar_t *const pwchSQL) { assert(NULL != pConnection); assert(NULL != pwchSQL); DWORD dwReturn(0x00); try { pRecordset.CreateInstance(L"ADODB.Recordset"); pRecordset->Open((_bstr_t)pwchSQL, _variant_t((IDispatch*)pConnection, true), adOpenStatic, adLockOptimistic, adCmdText); } catch (_com_error &e) { //#ifdef _DEBUG const int nErrMsgLength(MAX_PATH); wchar_t *pwchErrMsg = new wchar_t[nErrMsgLength](); _snwprintf_s(pwchErrMsg, nErrMsgLength, _TRUNCATE, L"CNHSQLServerDBO::OpenQuery发生错误(执行%s)。", pwchSQL); // 输出错误信息到输出窗口 OutputDebugStringW(L"\t"); OutputDebugStringW(pwchErrMsg); OutputDebugStringW(L"\n"); // 输出错误信息到日志文件 if (0 != wcscmp(m_wchLogFilePath, L"")) { // 当日志文件路径不为空时,写日志 CNHLogAPI::WriteLogEx(m_wchLogFilePath, LOG_ERR, L"NHSQLServerDBO", pwchErrMsg); } if (NULL != pwchErrMsg) { delete[] pwchErrMsg; pwchErrMsg = NULL; } OutputDBErrMsg(e); //#endif dwReturn = 0x01; } return dwReturn; }
LRESULT DBConnect::QuerySQL(LPCSTR bstrSQL, _RecordsetPtr& pRecordset, CursorTypeEnum cursorType, LockTypeEnum lockType) { if (!m_bDBOpen||m_pAdoConn->GetState()!=ADODB::adStateOpen) { return -1; } try { pRecordset.CreateInstance(__uuidof(Recordset)); pRecordset->Open((_bstr_t)bstrSQL, m_pAdoConn.GetInterfacePtr(), cursorType /*adOpenStatic*/ /*adOpenDynamic*/, lockType/*adLockOptimistic*/, adCmdText); } catch(_com_error e) { MessageBox(NULL,e.Description(),_T("提示") ,1); throw e; } return 0; }
//Get record of database bool DataConnection::GetRecordSet(_RecordsetPtr &m_pRecordset , _bstr_t bstrSQL) //第一个参数是引用类型,也就是传出参数,可看做函数的返回值,第二个参数是SQL语句,是输入。 { try { if(m_pConnection==NULL) //初始化连接 OnInitADOConn(); m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pConnection->CursorLocation = adUseClient; m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); return true; } catch(_com_error e) //失败 { MessageBox(NULL,e.Description(),"提示",MB_ICONINFORMATION | MB_OK); return false; } }
// 打开记录集 _RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL) { //TODO: insert return statement here try { if (m_pConnection==NULL) { OnInitADOConn(); } //创建记录对象 m_pRecordset.CreateInstance(__uuidof(Recordset)); //取得表中记录 m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic, adLockOptimistic,adCmdText); } catch (_com_error e) { e.Description(); } return m_pRecordset; }
void CMorphaResDlg::GetRecordSet(_RecordsetPtr &rs, int row1, int row2) { ASSERT(row1>=0&&row2>=0&&row1<=row2); CString strSQL; CString tablename; if(((CMorphaDataRetriveDlg*)(GetParent()->GetParent()->GetParent()))->IsInqueryState()) tablename = "morphaforinquery"; else tablename = "morphafordisplay"; if(row1 == 0 ) { strSQL.Format("select * from (select top %d * from %s) a " ,row2,tablename); } else { strSQL.Format("select * from (select top %d * from %s) a \ where pdetectno not in(select top %d pdetectno from %s)" ,row2,tablename,row1,tablename); } if(rs == NULL) rs.CreateInstance("adodb.recordset"); rs = theConnection->Execute((LPCSTR)strSQL,NULL,adCmdText); }
void CADOUtil::RunSP_ReturnRS(_bstr_t SP_Name, _RecordsetPtr& out_pRs, CParamArray *ParamArray) { if(!CheckCurrentProcess()) throw ERR_NEW_PROCESS_DETECTED; _ConnectionPtr pConn = NULL; _ParameterPtr pPrm = NULL; _ParameterPtr pprm = NULL; ErrorPtr pErr = NULL; _CommandPtr pComm = NULL; CParam* pr = NULL; LONG ErrSQLState = 0; LONG count = 0; LONG ErrorCode = 0; _variant_t vIndex; _bstr_t bsEmpty; //******************************************************************** //Connectin to Pool try { _bstr_t bstrConnString(szConnectionString); pConn.CreateInstance(__uuidof(Connection)); if(pConn == NULL) { throw(ErrorCode = ERR_UNABLE_CREATE_CONN); } pConn->ConnectionTimeout = 7; pConn->Open(bstrConnString, L"", L"", adConnectUnspecified); } catch(...) { Beep(2000, 100); //m_ExternalLink.Add2Log(WL_ERROR_LEVEL1,"Unable set connection to SQL Error = Unknown"); ErrorCode = ERR_UNABLE_CREATE_CONN; } if(ErrorCode) throw ErrorCode; //******************************************************************* //Execut command try { pComm.CreateInstance(__uuidof(Command)); if(pComm == NULL) { throw(ErrorCode = ERR_UNABLE_CREATE_COMM); } out_pRs.CreateInstance(__uuidof(Recordset)); if(out_pRs == NULL) { throw(ErrorCode = ERR_UNABLE_CREATE_RECSET); } pComm->ActiveConnection = pConn; pComm->CommandText = SP_Name; pComm->CommandType = adCmdStoredProc; if(ParamArray != NULL) { DWORD Count = ParamArray->GetSize(); for (DWORD i=0; i < Count; i++) { pr = (*ParamArray)[i]; pprm = pComm->CreateParameter(bsEmpty, pr->Type, pr->Direction, pr->Size, pr->Value); pComm->Parameters->Append(pprm); } } out_pRs->CursorLocation = adUseClient; out_pRs->Open(_variant_t((IDispatch *)pComm, true), vtMissing, adOpenForwardOnly, adLockReadOnly, NULL); out_pRs->PutRefActiveConnection(NULL); pConn->Close(); return; } //Handle Execut error catch(_com_error) { try { count = pConn->Errors->Count; if(count) { vIndex = _variant_t((LONG)0); pErr = pConn->Errors->GetItem(vIndex); ErrSQLState = atol((LPCSTR)pErr->SQLState); /* //m_ExternalLink.Add2Log(WL_ERROR_LEVEL3, "SQL (RS) Error %d [Error #%d Description \"%s\" (Source: %s)" "(SQL State: %s)](NativeError: %s)", e.Error(), pErr->Number, (LPCSTR)pErr->Description, (LPCSTR)pErr->Source, (LPCSTR)pErr->SQLState, (LPCSTR)pErr->NativeError);*/ } pConn->Close(); if (pComm->ActiveConnection != NULL) { pComm->PutRefActiveConnection(NULL); } } catch(...) { ErrSQLState = 1; } } catch(long errorCode) { //m_ExternalLink.Add2Log(WL_ERROR_LEVEL3,"SQL (RS) Error Unable Create Command object"); pConn->Close(); throw(errorCode); } catch(...) { //m_ExternalLink.Add2Log(WL_ERROR_LEVEL3,"SQL (RS) Error Unknown"); ErrSQLState = 1; } if(ErrSQLState) { switch(ErrSQLState) { case 23000: //Violation of PRIMARY KEY constraint throw (ErrorCode = ERR_PRIMARY_KEY_CONSTRAINT); break; default: //Code throw (ErrorCode = ERR_SQL_UNKNOWN_PROBLEM); break; } } }