ADO 数据连接学习笔记

参考资料:VC知识库在线杂志http://www.vckbase.com/vckbase/default.aspx

记录一下重点步骤:

初始化ole连接

Initializing the OLE/COM Libraries

 1:  BOOL CADOMFC1App::InitInstance()
 2:  {
 3:      // Add this function to initialize the OLE/COM libraries
 4:      AfxOleInit();
引入ado组件

Changes to StdAfx.h

1: #include <comdef.h>

2:

3: #import "C:\program files\common files\system\ado\msado15.dll" \

4: no_namespace \

5: rename( "EOF", "adoEOF" )

 

申明数据连接变量

Changes to the Document Header File

 1:  class CADOMFC1Doc : public CDocument
 2:  {
 3:  // Attributes
 4:  public:
 5:      BOOL m_IsConnectionOpen;
 6:      _ConnectionPtr m_pConnection;
数据库连接及错误处理

OnNewDocument with Exception Handling

BOOL CADOMFC1Doc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
        return FALSE;

    // TODO: add reinitialization code here
    // (SDI documents will reuse this document)
    HRESULT hr;
    try
    {
        hr = m_pConnection.CreateInstance( __uuidof( Connection ) );
        if (SUCCEEDED(hr))
        {
            hr = m_pConnection->Open(_bstr_t(L"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tee;Data Source=SQL\\SQLSERVER2000"),
                _bstr_t(L""),
                _bstr_t(L""),
                adModeUnknown);

//获取数据连接字串可以新建一个.udl的文件,执行之后可以查看。
            if (SUCCEEDED(hr))
            {
                m_IsConnectionOpen = TRUE;
                TRACE("连接成功\r\n");
            }
        }

    }
    catch (_com_error &e)
    {
        // Get info from _com_error
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());
        TRACE( "Exception thrown for classes generated by #import" );
        TRACE( "\tCode = %08lx\n", e.Error());
        TRACE( "\tCode meaning = %s\n", e.ErrorMessage());
        TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource);
        TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
    }
    catch(...)
    {
        TRACE( "*** Unhandled Exception ***" );
    }  

    return TRUE;
}

数据集获取及数据显示 

retrieve and show data

//这里用到一个类,名字为CListCtrlEx,用于列表显示数据

_RecordsetPtr pRecordSet;
    CADOMFC1Doc * pDoc;
    pDoc = GetDocument();
    _bstr_t bstrQuery("SELECT * FROM Data");
    _variant_t vRecsAffected(0L);
    try
    {
        pRecordSet = pDoc->m_pConnection->Execute(bstrQuery, &vRecsAffected,
            adOptionUnspecified);
        if (!pRecordSet->GetadoEOF())
        {
            CListCtrlEx& ctlList = (CListCtrlEx&) GetListCtrl();
            ctlList.DeleteAllItems();
            while(ctlList.DeleteColumn(0));
            ctlList.AddColumn("  x  ",0);
            ctlList.AddColumn("  y  ",1);
            int i = 0;
            _variant_t vFirstName;
            _variant_t vLastName;
            while (!pRecordSet->GetadoEOF())
            {
                vFirstName = pRecordSet->GetCollect(L"x");
                ctlList.AddItem(i,0,(_bstr_t) vFirstName);
                vLastName = pRecordSet->GetCollect(L"y");
                ctlList.AddItem(i,1,(_bstr_t) vLastName);
                i++;
                pRecordSet->MoveNext();
            }
        }
        pRecordSet->Close();
    }
    catch( _com_error &e )
    {
        // Get info from _com_error
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());
        TRACE( "Exception thrown for classes generated by #import" );
        TRACE( "\tCode = %08lx\n", e.Error());
        TRACE( "\tCode meaning = %s\n", e.ErrorMessage());
        TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource);
        TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
    }
    catch(...)
    {
        TRACE( "*** Unhandled Exception ***" );
    }

断开连接

try
    {
        if (m_IsConnectionOpen)
        {
            m_IsConnectionOpen = FALSE;
            m_pConnection->Close();
            TRACE("断开成功\r\n");
        }

    }
    catch (_com_error &e)
    {
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());
        TRACE( "Exception thrown for classes generated by #import" );
        TRACE( "\tCode = %08lx\n", e.Error());
        TRACE( "\tCode meaning = %s\n", e.ErrorMessage());
        TRACE( "\tSource = %s\n", (LPCTSTR) bstrSource);
        TRACE( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
    }
    catch(...)
    {
        TRACE( "*** Unhandled Exception ***" );
    }  

posted @ 2010-09-06 11:41  东 哥  阅读(1568)  评论(2编辑  收藏  举报