1._RecordsetPtr 1)初始化 HRESULT hr = 0; try { hr = m_pRs.CreateInstance(__uuidof(Recordset)); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch(...) { // } 2)关闭 bool CloseRSet(void) { try { if(!is_RSetClosed()) { m_pRs->Close(); } } catch(...) { return false; } return true; } try { if(CloseRSet()) { m_pRs.Release(); } m_pRs = NULL; } catch(...) { // } 3)打开记录集 _variant_t val; HRESULT hr = 0; try { m_pRs->CursorLocation = adUseClient; hr = m_pRs->Open(sSQLCmd.GetBuffer(0), _variant_t(pCnn, true), adOpenDynamic, adLockOptimistic, adCmdText); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch (_com_error &e) { return false; } //另一种打开的方式 HRESULT hr = 0; try { m_pRs->CursorLocation = adUseClient; hr = m_pRs->Open((LPCTSTR)(sSQLCmd.GetBuffer(0)), (LPCTSTR)(sConnString.GetBuffer(0)), adOpenDynamic, adLockOptimistic, adCmdUnknown); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch (_com_error &e) { return false; } 4)获取某一字段值 m_pRs->Fields->GetItem(sFieldName.GetBuffer(0))->Value; 5)判断某值是否存在 bool is_ExistRecord(_ConnectionPtr& pCnn, CString sTable, CString sWhereCondition) { CString sSqlChkCmd(""); sSqlChkCmd.Format( " select count(*) as Cnt " " from %s where %s ", sTable, sWhereCondition); bool bFlags = false; if(OpenRSet(sSqlChkCmd, pCnn)) { int iFlags = (int)(m_pRs->Fields->GetItem("Cnt")->Value.iVal); if(iFlags > 0) bFlags = true; CloseRSet(); } return bFlags; } 2._ConnectionPtr 1) 初始化 HRESULT hr = 0; try { hr = m_pCnn.CreateInstance(__uuidof(Connection)); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch(...) { // } 2) 关闭 bool DisConnect(void) { try { if (!is_ConnectClosed()) { m_pCnn->Close(); } } catch(...) { return false; } return true; } try { if(DisConnect()) { m_pCnn.Release(); } m_pCnn = NULL; } catch(...) { // } 3) 连接 CString strTmpCnn(""); HRESULT hr = 0; // if sCnnString is Empty then connect default. if (sCnnString.IsEmpty()) { strTmpCnn.Format( "Provider=SQLOLEDB.1;" "Password=%s;" "Persist Security Info=True;" "User ID=%s;" "Initial Catalog=%s;" "Data Source=%s", "sa", "SA", "tablename", "127.0.0.1"); } try { SetCursor(LoadCursor(NULL, IDC_WAIT)); m_pCnn->ConnectionString = static_cast<_bstr_t>(strTmpCnn); m_pCnn->CursorLocation = adUseClient; hr = m_pCnn->Open("", "", "", adConnectUnspecified); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch(_com_error& e) { SetCursor(LoadCursor(NULL, IDC_ARROW)); return false; } catch(...) { SetCursor(LoadCursor(NULL, IDC_ARROW)); return false; } 4) 执行sql语句 _variant_t val; HRESULT hr = 0; try { m_pCnn->BeginTrans(); m_pCnn->Execute(sSqlCmd.GetBuffer(0), &val, adCmdText | adExecuteNoRecords); hr = m_pCnn->CommitTrans(); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch (_com_error &e) { m_pCnn->RollbackTrans(); return false; } catch(...) { return false; } return true; 5) 设置超时 m_pCnn->put_ConnectionTimeout(lTimeOut); 6)执行事务 bool ExecuteTrans(CStringArray arrStrSQL) { _variant_t val; HRESULT hr = 0; try { m_pCnn->BeginTrans(); for(int i=0; i < arrStrSQL.GetSize(); ++i) { m_pCnn->Execute(arrStrSQL[i].GetBuffer(0), &val, adCmdText | adExecuteNoRecords); } hr = m_pCnn->CommitTrans(); if (FAILED(hr)) throw (_com_error(hr,NULL)); } catch(_com_error &e) { m_pCnn->RollbackTrans(); return false; } return true; }

评论