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();
 
 

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 ***" );
    }  

转载于:https://www.cnblogs.com/hyd10000/archive/2010/09/06/1818921.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值