MFC中使用ADO进行access数据库操作

MFC中使用ADO进行数据库操作

    前段时间做了一个IP分配工具。选择了Access数据库来存储内容。做了一些简单的查询,插入,删除操作。后文简明扼要的通过实例,重点阐述了Access数据库的连接方法及操作方法如,Select、Delete、Insert等等。其中包含了如何编写含有变量的SQL语句。

①连接

在stdafx.h中文末的#endif上插入代码:

//2018-6-27 17:39:31 关联数据库的
#import "msado15.dll" \
 no_namespace \
 rename ("EOF", "adoEOF")        

//注意去C盘搜索msado15.dll文件,直接拷贝到项目文件中和可执行文件生成文件夹下。

②项目.h文件添加

#pragma once
#include "stdafx.h"

//………………分割线Others……………………

public:

    HRESULT hr;   //连接时使用,可用其保存连接数据库时返回的参数,确定连接是否成功。
    _ConnectionPtr pConn;  //连接数据库使用
    _CommandPtr     m_pCommand;  //本次未用到

    _RecordsetPtr  m_pRecordset;   //读取数据库数据时用的记录集。

public: //根据需要随便写写

    void tips(CString str);   //界面控件写提示语

    //查询备注信息
    VOID GetMansRemarks(int iptmp);
    //插入新员工信息
    BOOL InsertNewMans(CString Name, int DepartID, int IP, CString Remark);
    //删除旧员工信息,函数的重载
    BOOL DeleteOldMansRecord(CString Name,int IP);
    BOOL DeleteOldMansRecord(int IP);

③在App::InitInstance()中添加初始化语句

if (!AfxOleInit())  //API,初始化数据库,成功则跳出,反之则弹框(项目用可修改为记录日志)
{
    AfxMessageBox(_T("初始化OLE DLL失败!"));
    return FALSE;
}

④.cpp文件中需要添加的几个必要的函数,函数定义已在.h文件中添加。

//tips函数,主要用于快速写入类日志信息。写日志提示信息比较频繁,就单独做了一个函数。你们从后面的代码中可以看出来这个有多么的方便。

void C**Dlg::tips(CString str)
{
    if ("" == str)
    {
        m_static1.SetWindowText(str);
        UpdateData(FALSE);//没有这一句不会显示
    }
    else
    {
        str += "\r\n";
        m_static1.SetWindowText(str);  //m_result 是与edit控件相关联CEDIT类变量   
        UpdateData(FALSE);//变量到控件是TRUE,反之FALSE
        str = "";
    }
    return;
}

 //ConnectAdo,连接数据库,连接正常时就打开数据库,异常时则提示数据库连接失败。

BOOL C**Dlg::ConnectAdo()
{
    ::OleInitialize(NULL);
    try
    {
        hr = pConn.CreateInstance(_T("ADODB.Connection"));
        strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data                 Source=dhip.mdb;Persist Security Info=False");

        if (SUCCEEDED(hr))
        {
            hr = pConn->Open(strConnection, "", "", -1);//打开数据库
            //tips(_T("数据库连接成功")); 
            return TRUE;
        }
    }
    catch (_com_error e)///捕捉异常
    {
        CString errormessage;
        errormessage.Format(_T("连接数据库失败!\r\n错误信息:%s"), e.ErrorMessage());
        AfxMessageBox(errormessage);///显示错误信息
    }
    return FALSE;
}

//CloseAdo,关闭数据库。每一次数据库操作后,必然需要关闭数据库。否则恐引起异常或者泄露。 

void C**Dlg::CloseAdo()        //每一次数据库查询完毕即关闭一次。
{
    if (m_pRecordset->State)
    {
        m_pRecordset->Close();
    }
    if (pConn->State)
    {
        pConn->Close();
    }

    //pConn->Close();         //关闭数据库连接
    ::CoUninitialize();       //释放COM环境                       
}

//DeleteOldManRecord,删除功能,根据Name及IP。读取某个(Statci或者其它)控件传入选择条件,而后根据传入的条件生成SQl命令。 SQL语句是带条件参数的命令语句,可以收纳。

BOOL C**Dlg::DeleteOldMansRecord(CString Name, int IP)
{
    if (!ConnectAdo())
    {
        return -1;
    }
    long index = 0;//注意:必须是long类型
    int countItem = 0;
    try
    {
        //生成查询的字符串
        strSQL.Format(_T("Delete * from [People] where IP = %d and Name = '%s'"), IP,Name);
        m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
        m_pRecordset->Open(strSQL.GetBuffer(),
        pConn.GetInterfacePtr(),
        adOpenDynamic,
        adLockOptimistic,
        adCmdText);
        CloseAdo();
        return TRUE;
    }
    catch (_com_error &e)
    {
        CString errormessage;
        errormessage.Format(_T("删除People表失败!\r\n错误信息:%s"), e.ErrorMessage());
        AfxMessageBox(errormessage);///显示错误信息
        CloseAdo();
        return FALSE;
    }
    CloseAdo();
    return FALSE;
}

//DeleteOldMansRecord,删除功能。是一个重载函数。根据Ip输出记录。其余同上。

BOOL C**Dlg::DeleteOldMansRecord(int IP)
{
    if (!ConnectAdo())
    {
        return -1;
    }
    long index = 0;//注意:必须是long类型
    int countItem = 0;
    try
    {
        //生成查询的字符串
        strSQL.Format(_T("Delete * from [People] where IP = %d"), IP);
        m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
        m_pRecordset->Open(strSQL.GetBuffer(),
        pConn.GetInterfacePtr(),
        adOpenDynamic,
        adLockOptimistic,
        adCmdText);
        CloseAdo();
        return TRUE;
    }
    catch (_com_error &e)
    {
        CString errormessage;
        errormessage.Format(_T("删除People表失败!\r\n错误信息:%s"), e.ErrorMessage());
        AfxMessageBox(errormessage);///显示错误信息
        CloseAdo();
        return FALSE;
    }

    CloseAdo();
    return FALSE;
}

//SelectRecord,查询功能。根据姓名及IP。执行查询操作。name及IP可以根据(Static或其它)控件获取。而后生成Sql语句。数据需要读出,故而代码相对而言较多一些。

BOOL C**Dlg::SelectRecord(CString name, int IP)
{
    if (!ConnectAdo())
    {
        return -1;
    }
    long index = 0;//注意:必须是long类型
    int countItem = 0;
    CString str5;
    CString strmark;
    int IPtmp;
    int intdepart;
    try
    {
        //生成查询的字符串
        if (_T("") != name && 0 != IP)//二者俊可用
        strSQL.Format(_T("Select * from [People] where Name = '%s' and IP = %d"),name, IP);
        else if (_T("") == name && 0 != IP)//IP可用
        strSQL.Format(_T("Select * from [People] where IP = %d"), IP);
        else if (_T("") != name && 0 == IP)//姓名可用
        strSQL.Format(_T("Select * from [People] where Name = '%s'"), name);
        else//IP为零及其它异常情况
        {
            tips(_T("不支持查询IP为0或IP异常值"));
            //CloseAdo();
            return FALSE;
        }
        m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
        m_pRecordset->Open(strSQL.GetBuffer(),
        pConn.GetInterfacePtr(),
        adOpenDynamic,
        adLockOptimistic,
        adCmdText);
        //处理数据,并显示
        while (!m_pRecordset->adoEOF)
        {
            tobeornot = TRUE;
            index = 0;
            varbuffer = m_pRecordset->GetCollect(_variant_t(index));
            varname = m_pRecordset->GetCollect(_variant_t(index+1));
            vardepart = m_pRecordset->GetCollect(_variant_t(index + 2));
            varip = m_pRecordset->GetCollect(_variant_t(index + 3));
            varmark = m_pRecordset->GetCollect(_variant_t(index+4));
            if (varbuffer.vt != VT_NULL)
            {
                //IPbuffer = varbuffer.lVal;
                //(char *)(_bstr_t)m_pRecordset->GetCollect("姓名");//
                str5 = (LPCSTR)_bstr_t(varname);
                m_name1.SetWindowText(str5);
                IPtmp = varip.lVal;
                m_ip1.SetAddress(10, 10, 90, IPtmp);
                strmark = (LPCSTR)_bstr_t(varmark);
                m_mark1.SetWindowTextW(strmark);
                //tips(strmark);
                intdepart = vardepart;
                m_box1.SetCurSel(intdepart - 1000);
                UpdateData(FALSE);
            }
            m_pRecordset->MoveNext();
            countItem++;
        }

        CloseAdo();
        return TRUE;
    }
    catch (_com_error &e)
    {
        CString errormessage;
        errormessage.Format(_T("查询People表失败!\r\n错误信息:%s"), e.ErrorMessage());
        AfxMessageBox(errormessage);///显示错误信息
        CloseAdo();
        return FALSE;
    }
    CloseAdo();
    return FALSE;
}

猜你喜欢

转载自blog.csdn.net/i_likechard/article/details/80893912
今日推荐