c++ 調用ADO訪問SQL 2000

來源:互聯網
上載者:User
Code
// adotest.cpp : 定義控制台應用程式的進入點。
//

#include "stdafx.h"
#import "c:\Windows\System32\msado15.dll" no_namespace rename("EOF","adoEOF")

int _tmain(int argc, _TCHAR* argv[])
{
    
    ::CoInitialize(NULL);
    _ConnectionPtr    m_pConnection;
    HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
    if(!SUCCEEDED(hr))
    {
        std::cout<<"m_pConnection.CreateInstance(__uuidof(Connection)) error"<<std::endl;
        Sleep(-1);
        return 0;
    }
    std::string str = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC"; 
    m_pConnection->ConnectionString ="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Sell;Data Source=WUBIYU-PC" ;

    try
    {
        m_pConnection->Open((_bstr_t)str.c_str(),"","",NULL); 
    }
    catch(_com_error &e)
    {
        std::cout<<"開啟失敗:"<<e.Description()<<std::endl;
        Sleep(-1);
        return 0;
    }

    _RecordsetPtr recordsetP;
    

    if(FAILED(recordsetP.CreateInstance(__uuidof( Recordset ))))
    {
        std::cout<<"建立Recordset對象失敗:"<<std::endl;
        Sleep(-1);
        return 0;
    }
    try
    {
        //recordsetP->Open(_variant_t("Sell_Agent"),
        //    _variant_t((IDispatch *)m_pConnection,true), CursorTypeEnum::adOpenKeyset,LockTypeEnum::adLockOptimistic, CommandTypeEnum::adCmdTable);
        //recordsetP->Open(_variant_t("Sell_Agent"),
        //    _variant_t((IDispatch *)m_pConnection,true), ::adOpenKeyset,::adLockOptimistic, ::adCmdTable);
        
    }
    catch (_com_error &e)
    {
        std::cout<<"開啟失敗"<<e.Description()<<std::endl;
    }

    //std::string cmdStr = "UPDATE Sell_Agent SET Integral = 21 WHERE id = 2";
    std::string cmdStr = "select * from Sell_Agent WHERE id = 2";
    _CommandPtr pCmdChange;
    hr = pCmdChange.CreateInstance(__uuidof(Command));
    if(!SUCCEEDED(hr))
    {
        std::cout<<"create Command object error"<<std::endl;
        Sleep(-1);
        return 0;
    }
    pCmdChange->ActiveConnection = m_pConnection;
    pCmdChange->CommandText = (_bstr_t)cmdStr.c_str();
    m_pConnection->Errors->Clear();
    _variant_t recordsCount=0;
    recordsetP = pCmdChange->Execute(&recordsCount, NULL, adCmdText);
    std::cout<< (long)recordsCount<<std::endl;

    try
    {
        std::cout<<recordsetP->GetFields()->Count<<std::endl;
        recordsetP->MoveFirst(); 
        while(!recordsetP->adoEOF)
        {
            int id_value =(int)recordsetP->Fields->Item[_variant_t("Id")]->Value;
            std::string id = (_bstr_t)recordsetP->GetFields()->GetItem((long)0)->Name;
            std::string name_value =(_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Value;
            std::string name = (_bstr_t)recordsetP->GetFields()->GetItem((long)1)->Name;
            std::cout<<id.c_str()<<" :"<<id_value<<"    "<<name.c_str()<<" :"<<name_value.c_str()<<endl;
            recordsetP->MoveNext();
        }
    }
    catch(_com_error &el)
    {
        std::cout<<el.Description()<<std::endl;
    }
    std::cout<<"完成"<<std::endl;
    ::CoUninitialize();
    Sleep(-1);
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.