MFC连接access数据库

//在stdaxh.h引入ADO支持库

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

dialog中

//检查是否创建access数据库

CString  m_str = "";
    m_str.Format("\\Alarm.mdb");
    CString strPath = Getpath() + m_str;
    CFileFind m_FileFind;
    if (!m_FileFind.FindFile(strPath))
    {
        MessageBox("文件不存在", "提示", MB_OK | MB_ICONHAND);
    }
    //将连接数据的字符串先写好
#if defined _WIN64//如果是64位程序
    m_csConnection.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=%s", strPath);
#elif defined _WIN32//如果是32位程序
    m_csConnection.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s", strPath);
#endif
    CoInitialize(NULL);//初始化窗口
    HRESULT hr;
    try
    {
        hr = m_pConnection.CreateInstance(__uuidof(Connection));
        if (SUCCEEDED(hr))
        {
            m_pRecordset.CreateInstance(__uuidof(Recordset));
        }
    }
    catch (_com_error* e)
    {
        AfxMessageBox(e->ErrorMessage());
    }

//增加数据

CString sqlstr;
    sqlstr.Format("Insert into alarm (日期,时间,错误代码,提示等级,模块分类,错误信息) values('%s','%s','%s','%s','%s','%s')", "1", "1", "1", "1", "1", "1");
    try
    {
        HRESULT hr = m_pConnection->Open(_bstr_t(m_csConnection), "", "", adModeUnknown);
        if (SUCCEEDED(hr))
        {
            m_pConnection->Execute(_bstr_t(sqlstr), NULL, adCmdText);
        }
        
    }
    catch (_com_error e)
    {
        AfxMessageBox(sqlstr + _T(" 增加记录失败!!!"));
    }
    m_pConnection->Close()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值