c++builder 6.0 使用原生ADO接口访问数据库

用BCB的ado组件访问数据,总觉得别扭,用惯了vc的格式,还是想用自己熟悉的写法来操作,之前想用import library的方法来操作原生的ADO,今天发现c++builder 已经封装好了,就在adoinit.hpp文件中,真是方便极了。

我想要的东西BCB都有现成的,BCB真是好用!

为什么要用原生的ADO来访问呢,BCB封闭的TADO类很好用,功能太复杂,用不到的功能太多了。

上代码解释,写个代码在这里备注,省得以后重新学习没有现在的笔记查询:

#pragma hdrstop

//---------------------------------------------------------------------------
#include <adoint.h>
#include <adoint.hpp>
#include <iostream>
using namespace std ;

#pragma argsused
#include <ComObj.hpp>
#define null NULL 
namespace System
{
    void __fastcall CheckSafecallResult(HRESULT hr)
    {
        OleCheck(hr);
    }
}

int main(int argc, char* argv[])
{
    ::CoInitialize(null);

    CoConnection* pCom = new CoConnection() ;
    try
    {

    _di__Connection db =  pCom->Create(NULL);
    db->ConnectionTimeout = 5;
    db->CursorLocation = adUseClient ; 
    db->Open(L"Provider=SQLNCLI11.1;Persist Security Info=False;User ID=sa;password=00000;Initial Catalog=test;Data Source=129.9.1.100;",L"",L"",adConnectUnspecified) ;
    if(db->State !=adStateOpen)
     {
       String msg  = db->Errors->Item[0]->Description.c_bstr();
        cout<<"连接错误:"<<msg.c_str()  <<endl;
     }


    String CommandText;
    OleVariant RecordsAffected;

    _di__Recordset rs  ;
    db->Errors->Clear();
    db->Execute("select * from table1 ",RecordsAffected,adOptionUnspecified ,rs);
    if(db->Errors->Count >0)
    {
       String msg  = db->Errors->Item[0]->Description.c_bstr();
        cout<<"执行错误:"<<msg.c_str()  <<endl;
    }

    long iCount = rs->RecordCount ;

    cout<<"RecordCount:" << iCount<<endl;
    
    rs->Close();
    db->Close() ;

    }
    __finally
    {
        delete pCom ;
    }

    ::CoUninitialize() ; 
    getchar();
    return 0;
}

宝蓝德公司已经封装好了你想要的一切

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿峰的编程博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值