ado数据库读取例子

用appwizard新建一个dialog based的mfc项目,加入项目名字叫“contact”, 在StdAfx.h底部加入代码:

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

CContactDlg::OnInitDialog()代码如下

BOOL CContactDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    // Add "About..." menu item to system menu.
    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);
    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }
    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    // ado数据库操作
    AfxOleInit();
    _ConnectionPtr m_pConnection;
    _RecordsetPtr m_pRecordset;
    _variant_t RecordsAffected;
     HRESULT hr;
    try
    {
        hr = m_pConnection.CreateInstance("ADODB.Connection");
        if(SUCCEEDED(hr))
        {
            m_pConnection->ConnectionTimeout=5;
            if(m_pConnection->State)
                m_pConnection->Close();
            hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=contact.mdb","","",adModeUnknown);
            AfxMessageBox("连接成功");
            m_pRecordset.CreateInstance("ADODB.Recordset");
            m_pRecordset=m_pConnection->Execute("select * from contacts",&RecordsAffected,adCmdText);
            _variant_t vIndex = (long)0;
            _variant_t vCount=m_pRecordset->GetCollect(vIndex);
            _variant_t var;
            char *str;
            try
            {
                if(!m_pRecordset->BOF)
                {
                    m_pRecordset->MoveFirst();
                }
                else
                {
                    AfxMessageBox("表内数据为空");
                    return TRUE;
                }
                while(!m_pRecordset->adoEOF)
                {
                    var = m_pRecordset->GetCollect("position");
                    if(var.vt != VT_NULL)
                    {
                        str=_com_util::ConvertBSTRToString((_bstr_t)var);
                        AfxMessageBox(str);
                        m_pRecordset->MoveNext();
                    }
                }
            }
            catch (_com_error e)
            {
                CString errormessage;
                errormessage.Format("读取错误:%s",e.ErrorMessage());
                AfxMessageBox(errormessage);
            }
            //CString msg;
            //msg.Format("共有%d条记录",vCount.lVal);
            //AfxMessageBox(msg);
        }
    }
    catch(_com_error e)
    {
        CString errormessage;
        errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
        AfxMessageBox(errormessage);
    }
    return TRUE;  // return TRUE  unless you set the focus to a control
}

contact.mdb里面的contacts表的字段有id,name,position,mobile,phone_home,phone_office,email,address。

发表评论

电子邮件地址不会被公开。 必填项已用*标注