c++builder 6.0 使用ado

可以使用系统的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提示框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白(litebai.com)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值