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

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

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

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

猜你喜欢

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