VC下利用ADO连接Access数据库

#include <iostream.h>
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename ("EOF", "adoEOF")  

int main(){

    //使用ADO连接数据库...
    //---------------------------------------------------------------------------------
    _ConnectionPtr m_pConnection;

    CoInitialize(NULL);
    m_pConnection.CreateInstance(__uuidof(Connection));

    // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
    // 因为它有时会经常出现一些想不到的错误。
    try                 
    {    
        // 打开本地Access库db1.mdb
        m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);
        
    }
    catch(_com_error e)
    {
        cout<<"数据库连接失败,确认数据库db1.mdb是否在当前路径下!"<<endl;
        return FALSE;
    }
    //-------------------------------------------------------------------------------------
    //建立数据集
    //-------------------------------------------------------------------------------------
    _RecordsetPtr m_pRecordset;
    m_pRecordset.CreateInstance(__uuidof(Recordset));

    // 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
    // 因为它有时会经常出现一些意想不到的错误。
    try
    {    
        m_pRecordset->Open("SELECT * FROM Home2",       
        m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
        adOpenDynamic,
        adLockOptimistic,
        adCmdText);
    }
    catch(_com_error *e)
    {
        //AfxMessageBox(e->ErrorMessage());
        cout<<e->ErrorMessage()<<endl;
    }    

    //--------------------------------------------------------------------------------------
    //读取数据
    //--------------------------------------------------------------------------------------
    _variant_t var;
    char *strID,*strX,*strY;
    float X,Y;
    try
    {
        if(!m_pRecordset->BOF)
            m_pRecordset->MoveFirst();
        else
        {
            cout<<"表内数据为空"<<endl;
            return 1;
        }
        
        // 读入库中各字段并加入列表框中
        while(!m_pRecordset->adoEOF)
        {
            var = m_pRecordset->GetCollect("ID");
            if(var.vt != VT_NULL)
                strID= _com_util::ConvertBSTRToString((_bstr_t)var);    //_variant_t转字符串
            var = m_pRecordset->GetCollect("X");
            if(var.vt != VT_NULL)
                    strX=_com_util::ConvertBSTRToString((_bstr_t)var);
            var = m_pRecordset->GetCollect("Y");
            if(var.vt != VT_NULL)
                strY=_com_util::ConvertBSTRToString((_bstr_t)var);
            cout<<strID<<"is"<<strX<<" "<<strY<<endl;
        
            m_pRecordset->MoveNext();
        }
    }
    catch(_com_error *e)
    {
        cout<<e->ErrorMessage()<<endl;
    }  

    //--------------------------------------------------------------------------------------
    //关闭数据集
    m_pRecordset->Close();
    m_pRecordset = NULL;    
    //--------------------------------------------------------------------------------------
    //关闭数据库连接
    //--------------------------------------------------------------------------------------
    if(m_pConnection->State)
    m_pConnection->Close();
    m_pConnection= NULL;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值