您的位置:首页 > 编程语言 > PHP开发

使用_RecordsetPtr读表数据时切记要加上Try..catch捕捉错误

2011-12-12 20:04 295 查看
 
HRESULT  hr2=m_pRecorset.CreateInstance(__uuidof( Recordset ));

   if (SUCCEEDED(hr2))

   {

    AfxMessageBox("connect successfully!");

    try

    {

     m_pRecorset->Open("SELECT * from user.",m_pConnection.GetInterfacePtr()

      ,adOpenStatic,adLockOptimistic,adCmdText);

     //

     CString idStr,nameStr,wStr,hStr;

     _variant_t vRVariant;

     while (!m_pRecorset->EndOfFile)

     {

      vRVariant=m_pRecorset->Fields->GetItem((long)1)->GetValue();

      idStr=(char*)(_bstr_t)vRVariant;

      vRVariant=m_pRecorset->Fields->GetItem((long)2)->GetValue();

      nameStr=(char*)(_bstr_t)vRVariant;

      vRVariant=m_pRecorset->Fields->GetItem((long)3)->GetValue();

      wStr=(char*)(_bstr_t)vRVariant;

      vRVariant=m_pRecorset->Fields->GetItem((long)4)->GetValue();

      hStr=(char*)(_bstr_t)vRVariant;

     }

    }

    catch (  _com_error   &e)

    {

     _bstr_t   bstrSource   (e.Source());

     _bstr_t   bstrDescription   (e.Description());

     CString   strError;

     strError.Format( "无法连接test数据库。\r\n错误代码是:%08lx\r\n错误的原因是:%s\r\n错误源是:%s\r\n错误的描述是:%s\r\n ",   e.Error   (),   e.ErrorMessage   (),   (LPCTSTR)   bstrSource,   (LPCTSTR)   bstrDescription);

     MessageBox   (strError,   bstrDescription,MB_OK);

     return  ;

    }

    catch (CFileException* e)

    {

     e->ReportError();

     e->Delete();

     return ;

    }

    catch (CException* e)

    {

     TRACE   (   "***   无法解决的错误   *** "   );

     return  ;

    }

   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库