c++builder 6.0 使用ado

版权声明:本文为博主原创文章,未经博主允许不可以转载。 https://blog.csdn.net/aasmfox/article/details/84438581

可以使用系统的TADOConnection 组件,这里使用原生对象来操作,只用于学习

1.导入ado库 import library ,生成adodb_ocx.h,adodb_tlb.h

2.修改生成的头文件,

  String sql_update = "update userinfo set name   = 'abc'";
     TConnection* db = new TConnection(this);
     int iState =0 ;

     try
     {
     try
     {

     db->ConnectionString = UTF8Decode("Provider=SQLOLEDB.1;Password=000000;Persist Security Info=True;User ID=sa;Initial Catalog=TEST;Data Source=127.0.0.1");
     db->Open(NULL,NULL,NULL,adConnectUnspecified);

     if(db->State != adStateOpen) ;
     {

      if(db->Errors->Count >0)
      {
       Variant i = 0 ;
       String str = db->Errors->get_Item(i)->Description ;

       MessageBox(Handle,str.c_str(),"error",MB_ICONSTOP);
       }

      return  ;
     }

     Variant iRows = -1;

     db->Execute(UTF8Decode(sql_update),iRows,adCmdText);
     Edit1->Text = IntToStr(iRows.lVal);
     }
     catch(Exception& e )
     {

      if(db->Errors->Count >0)
      {
       Variant i = 0 ;
       String str = db->Errors->get_Item(i)->Description ;

       MessageBox(Handle,str.c_str(),"error",MB_ICONSTOP);
       }
     }

     }
     __finally
     {
     db->Close();
     delete db ;
     }

遇到的错误:

1.函数调用,没用任何反应:

修改h文件中的声明

 __property BSTR ConnectionString={ read=get_ConnectionString,write = set_ConnectionString, stored=false};

 stored=false 改为stored=true

2.com异常提示框 "HRCHECK"

#if !defined(OLECHECK)
#define OLECHECK(hrexpr) DebugHlpr_HRCHECK(hrexpr, #hrexpr, __FILE__, __LINE__)
#endif

void DebugHlpr_THROW(T* msg, HRESULT hr, T* file, bool /*assertFailed*/)
{
#if defined(ComObjHPP)
  // NOTE: This does not retrieve rich error information, the way Delphi and VB environments
  //       do. Eventually this 'throw' will either throw a rich EOleException or some other
  //       OLE exception class (something equivalent to _com_error, maybe??)
  //
  //       For now, you can specialized [T = TCHAR] 'DebugHlpr_THROW' to retrieve rich error
  //       information and throw a VCL exception class, if you're using VCL classes already,
  //       or throw a custom exception class.
  //
  //       NOTE: Use the assertFailed parameter to distinguish between Assertion and
  //             OLECHECK failures. (Maybe throw something different??)
  throw EOleException(msg, hr, file, _T(""), 0);
#else
  throw msg;  // Hopefully we never get here: Need something much better to throw!!
#endif
}

处理:#define OLECHECK 1,默认是检查OLE异常


#if !defined(PROMPT_ON_HRCHECK_FAILURE)
    int i = IDYES;
#else
    int i = DebugHlpr_PROMPT(_T("HRCHECK: "), szMsg);
#endif
    if (i == IDYES)
      DebugHlpr_THROW(szMsg, hr,lfile, false);
    else if (i == IDCANCEL)
      ::DebugBreak();
    // NOTE: IDNO - implies we keep chugging along
  }

处理:#define PROMPT_ON_HRCHECK_FAILURE 1   ,就不会在运行时报HRCHECK提示框

猜你喜欢

转载自blog.csdn.net/aasmfox/article/details/84438581