正文

ADO编程手记2006-11-29 10:43:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lost/21007.html

分享到:

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; }  

阅读(299) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册