您的位置:首页 > 数据库

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

2015-05-02 13:09 567 查看
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、程序编译运行。运行结果:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: