MFCはADOを使ってデータベースに接続するクラスです.


MFCはADOを使ってデータベースに接続するクラスの下にコードがあります.
#include "StdAfx.h"

#include "ADOConnect.h"

//////////////////////////////////////////////////////////////////////
// StdAfx.h    #import "E:\vs2008\BuildNcProgram\BuildNcProgram\dll\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") //  ADO     
// 
//////////////////////////////////////////////////////////////////////
CAdoConnect::CAdoConnect(const CString& szConnectStr)
:m_szConnectStr(szConnectStr)
, m_szServer(_T(""))
, m_szDATABASE(_T(""))
, m_szUID(_T(""))
, m_szPWD(_T(""))
, m_nPort(1433)
{
    ::CoInitialize(NULL);    //   OLE/COM   
}
CAdoConnect::CAdoConnect(const CString& Server
    , const CString& DATABASE
    , const CString& UID
    , const CString& PWD
    , const int nPort
    )
    : m_szServer(Server)
    , m_szDATABASE(DATABASE)
    , m_szUID(UID)
    , m_szPWD(PWD)
    , m_szConnectStr(_T(""))
    , m_nPort(nPort)
{
    m_szConnectStr.Empty();
    ::CoInitialize(NULL);    //   OLE/COM   
}


CAdoConnect::~CAdoConnect(void)
{
    //        
    if (m_pRecordset != NULL)
    {
        if (m_pRecordset->GetState() == adStateOpen)
        {
            m_pRecordset->Close();
        }
        if (m_pRecordset)
        {
            m_pRecordset.Release();
            m_pRecordset = NULL;
        }
    }


    if (m_pConnection != NULL)
    {
        if (m_pConnection->GetState() == adStateOpen)
        {
            m_pConnection->Close();
        }
        m_pConnection.Release();
        m_pConnection = NULL;
    }

    ::CoUninitialize();    //       COM  
}

//      
BOOL CAdoConnect::OnInitAdoConnection(void)
{
    try
    {
        HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");//  Connection   
        if (SUCCEEDED(hr))
        {
            m_pConnection->CursorLocation = adUseServer;

            // "Provider=SQLOLEDB.1;Server=   IP  ;UID=sa;PWD=  sa    ;DATABASE=     ;";
            //_bstr_t strConnect = "driver={SQL Server};Server=192.168.0.117;DATABASE=TEST;UID=uid;PWD=passward;";                    
            CString szStr;
            if (m_szConnectStr.IsEmpty())
            {
                szStr.Format(_T("driver={SQL Server};Server=%s,%d;DATABASE=%s;UID=%s;PWD=%s;")
                    , m_szServer, m_nPort, m_szDATABASE, m_szUID, m_szPWD);
            }
            else
            {
                szStr = m_szConnectStr;// = "driver={SQL Server};Server=localhost;DATABASE=EOSDB;UID=sa;PWD=12345;";
            }
            //szStr = m_szConnectStr = "driver={SQL Server};Server=127.0.0.1,1433;DATABASE=EOSDB;UID=sa;PWD=12345;";

            _bstr_t strConnect = szStr;
            // "driver={SQL Server};Server=   IP  ;DATABASE=     ;UID=sa;PWD=  sa    ;";
            HRESULT hr = m_pConnection->Open(strConnect, "", "", adModeUnknown);
            if (SUCCEEDED(hr))
            {
                return TRUE;
            }
            else
            {
                AfxMessageBox("         !");
                return FALSE;
            }
        }
    }
    catch (_com_error e)
    {
        AfxMessageBox(e.Description());//      
        return FALSE;
    }
    catch (...)
    {
        return FALSE;
    }

    return TRUE;
}

//   Insert UpDate
BOOL CAdoConnect::ExecuteSQL(_bstr_t bstrSQL, long lOptions)
{
    try
    {
        //          
        if (m_pConnection == NULL)
        {
            OnInitAdoConnection();
        }
        m_pRecordset = m_pConnection->Execute(bstrSQL, NULL, lOptions);
        return TRUE;
    }
    catch (_com_error e)
    {
        AfxMessageBox(e.Description());
        return FALSE;
    }
    catch (...)
    {
        return FALSE;
    }
}


//    
_RecordsetPtr& CAdoConnect::GetRecordSet(_bstr_t bstrSQL)
{
    try
    {
        //        Connection            
        if (m_pConnection == NULL)
        {
            OnInitAdoConnection();
        }
        //       
        HRESULT hr = m_pRecordset.CreateInstance(__uuidof(Recordset));
        //       
        if (SUCCEEDED(hr))
        {
            m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
        }
    }
    catch (_com_error e)
    {
        //         
        AfxMessageBox(e.Description());
    }
    return m_pRecordset;
}#pragma once
//#import "E:\vs2008\BuildNcProgram\BuildNcProgram\dll\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF") //  ADO     
#import "c:\Program Files\Common Files\System\ado\msado15.dll"no_namespace \
    rename("EOF", "adoEOF")rename("BOF", "adoBOF")
class CAdoConnect
{
private:
    CString m_szConnectStr;
    CString m_szServer;
    CString m_szDATABASE;
    CString m_szUID;
    CString m_szPWD;
    int m_nPort;
private:
    _ConnectionPtr m_pConnection;     //       
    _RecordsetPtr  m_pRecordset;      //        

public:
    CAdoConnect(const CString& szConnectStr);
    CAdoConnect(const CString& Server
        , const CString& DATABASE
        , const CString& UID
        , const CString& PWD
        , const int nPort = 1433
        );
    virtual~CAdoConnect(void);

public:
    //      
    BOOL OnInitAdoConnection(void);
    //   Insert UpDate
    BOOL ExecuteSQL(_bstr_t bstrSQL, long lOptions = adCmdText);
    //          
    _RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);

    //      
public:
    _RecordsetPtr& operator->()
    {
        return m_pRecordset;
    }
};
転載はソースを明記してください.http://blog.csdn.net/defaultbyzt