MFC学习记录0

关于MFC连接数据库相知识点记录:

1.界面搭建

2.函数借口编写

3.sql server和access

 

接下来就是一步步实现

1.MFC学习记录0MFC学习记录0

2.程序运行时有一个message对话框窗口,显示“初始化ADO COM成功!”,废话不多说,上代码:

    2-1:在adoCom.cpp里面的InitInstance函数里面加入如下代码:

    hr=CoInitialize(NULL);
    try{
        if(SUCCEEDED(hr)){
            AfxMessageBox(_T("初始化ADO COM成功!"));
        }
        else{
            AfxMessageBox(_T("初始化ADO COM失败!"));
        }
    }
    catch(_com_error e){
        CString errMsg;
        errMsg.Format(_T("%s"),e.ErrorMessage());
        AfxMessageBox(errMsg);
    }

这里时初始化ADO COM检验是否创建成功

2-2:然后在“stdafx.h”中导入“msado15.dll”动态链接库文件:

#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

2-3:为四个按钮添加事件:

    2-3-1:在“adoComDlg.h”定义两个连接对象:

    _ConnectionPtr m_pConnectionA;//连接对象A
    _ConnectionPtr m_pConnectionB;//连接对象B

     2-3-2:在构造函数“CadoComDlg"里面创建两个连接对象:

         //创建链接对象A
    if(!SUCCEEDED(m_pConnectionA.CreateInstance(__uuidof(Connection)))){
        m_pConnectionA=NULL;
        AfxMessageBox(_T("连接对象A创建失败!"));
    }
    //创建链接对象B
    if(!SUCCEEDED(m_pConnectionB.CreateInstance(__uuidof(Connection)))){
        m_pConnectionB=NULL;
        AfxMessageBox(_T("连接对象B创建失败!"));
    }

    2-3-3:打开数据库连接(SQL)“OnBnClickedButtonOpenSqlserver”:

          if(m_pConnectionA->State & adStateOpen){
                AfxMessageBox(_T("SQL Server连接已打开,不能重复打开同一个数据库连接"));
               return;
           }

    CString datadbStr=_T("lians");
    CString strConnect=_T("Provider=SQLOLEDB.1;User ID=sa;Password=123456;Persist Security Info=True;Data Source=SC-202008232218;Initial Catalog=lians");
    if(SUCCEEDED(m_pConnectionA->Open(_bstr_t(strConnect),_T(""),_T(""),adConnectUnspecified))){
        AfxMessageBox(_T("打开SQLServer连接成功"));
    }
    else{
        AfxMessageBox(_T("打开SQLServer连接失败"));
    }

    2-3-4:关闭数据库连接(SQL)“OnBnClickedButtonCloseSqlserver”:

      if(!(m_pConnectionA->State & adStateOpen)){
        AfxMessageBox(_T("SQL Server连接已关闭,不能重复关闭!!"));
        return;
    }
    if(SUCCEEDED(m_pConnectionA->Close())){
        AfxMessageBox(_T("关闭SQL Server 连接成功"));
    }
    else{
        AfxMessageBox(_T("关闭SQL Server 连接失败"));
    }

    2-3-5:打开数据库连接(Access)OnBnClickedButtonOpenAccess:

     if(m_pConnectionB->State & adStateOpen){
        AfxMessageBox(_T("Access连接已打开,不能重复打开同一个数据库连接"));
        return;
    }
    //CString strConnect=_T("Provider=SQLOLEDB.1;User ID=sa;Password=123456;Persist Security Info=True;Data Source=SC-202008232218;Initial Catalog=lians");
    CString strConnect=_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\demo\\demo.mdb");
    if(SUCCEEDED(m_pConnectionB->Open(_bstr_t(strConnect),_T(""),_T(""),adConnectUnspecified))){
        AfxMessageBox(_T("打开Access连接成功"));
    }
    else{
        AfxMessageBox(_T("打开Access连接失败"));
    }

         2-3-6:关闭数据库连接(Access)OnBnClickedButtonCloseAccess:

        if(!(m_pConnectionB->State & adStateOpen)){
        AfxMessageBox(_T("连接已经关闭,不能重复关闭"));
        return;
    }
    if(SUCCEEDED(m_pConnectionB->Close())){
        AfxMessageBox(_T("关闭Access连接成功"));
    }
    else{
        AfxMessageBox(_T("关闭Access连接失败"));
    }

3.主要用到的时SQl server 和Access数据库:

以上代码要想运行成功,需要把数据库运行成功

   3-1:sql serverMFC学习记录0

3-2 access数据库:

MFC学习记录0