VC使用ADO对象连接一个Access数据库

14 篇文章 0 订阅

1、创建一个基于对话框的应用程序ADO

2、在对话框中添加一个按钮空间。ID:IDC_TEST。标题:测试连接

3、在stdafx.h中添加代码msado15.dll动态链接库导入到程序中。(红色部分)

// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__B81BB961_FDF7_49DD_8352_B8A700865BF9__INCLUDED_)
#define AFX_STDAFX_H__B81BB961_FDF7_49DD_8352_B8A700865BF9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define VC_EXTRALEAN  // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h>  // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>   // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace \
 rename("EOF","adoEOF")rename("BOF","adoBOF")

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__B81BB961_FDF7_49DD_8352_B8A700865BF9__INCLUDED_)

4、在主窗口头文件ADODlg.h中声明一个连接智能指针。代码如下:

_ConnectionPtr m_pConnection;          //添加一个连接智能指针

5、初始化COM环境。在程序类的InitStance函数中初始化COM环境并在程序末尾释放Com环境

BOOL CADOApp::InitInstance()
{
 AfxEnableControlContainer();
 ::CoInitialize(NULL);                    //初始化Com环境            
 // Standard initialization
 // If you are not using these features and wish to reduce the size
 //  of your final executable, you should remove from the following
 //  the specific initialization routines you do not need.

#ifdef _AFXDLL
 Enable3dControls();   // Call this when using MFC in a shared DLL
#else
 Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

 CADODlg dlg;
 m_pMainWnd = &dlg;
 int nResponse = dlg.DoModal();
 if (nResponse == IDOK)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with OK
 }
 else if (nResponse == IDCANCEL)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with Cancel
 }
 ::CoUninitialize()             //释放Com环境           
 // Since the dialog has been closed, return FALSE so that we exit the
 //  application, rather than start the application's message pump.
 return FALSE;
}

6、在类视图中为CADODlg类添加两个void成员函数:OnInitADOConnection、ExitConnection。代码如下:

void CADODlg::OnInitADOConnection()            //连接数据库
{
 try
 {
  m_pConnection.CreateInstance("ADODB.Connection");   //创建连接对象实例
        CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\
            uid=;pwd=;DBQ=shujuku.mdb;";                                   //数据库名为“shujuku”      //设置连接字符串
        m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown); //使用Open方法连接数据库
  MessageBox("连接成功");
 }
 catch(_com_error e)
 {
  AfxMessageBox(e.Description());
 }
}

void CADODlg::ExitConnection()                         //关闭记录集和数据连接
{
 m_pConnection->Close();
}
7、用类向导为测试连接按钮添加消息响应函数:OnTest

void CADODlg::OnTest()                      //初始化数据库连接
{
 // TODO: Add your control notification handler code here
 OnInitADOConnection();
}
8、用类向导为CADODlg类添加WM_DESTROY添加消息相应函数OnDestroy,在销毁窗口时退出数据库连接

void CADODlg::OnDestroy()
{
 CDialog::OnDestroy();
 
 // TODO: Add your message handler code here
 ExitConnection();


}

9、把Access制作的名为“shujuku”的数据库拖放到工程文件夹下。

10、程序编译运行。运行结果:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值