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、程序编译运行。运行结果:
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、程序编译运行。运行结果:
相关文章推荐
- ADO的一次开发实践(vc通过ADO连接access数据库做的一个多功能查询系统)
- VC使用ADO对象控制ACCESS数据库的访问
- VC使用ADO对象控制ACCESS数据库的访问
- VC使用ADO对象控制ACCESS数据库的访问-举例
- VC++ ado连接数据库(可以在VC6.0使用,以access数据库为实例)(1)
- 解决VC++在WIN7下使用ADO方式连接ACCESS数据库到XP不能运行的问题
- VC 在WIN7下使用ADO方式连接ACCESS数据库到XP不能运行
- 解决VC++在WIN7下使用ADO方式连接ACCESS数据库到XP不能运行的问题
- VC++下使用ADO连接Access数据库
- VC使用ADO连接oracle中文字符问题:涉及oracle字符集、连接字串。
- VS2008(VC++)使用Ado读取SQL Server和Access数据库
- VC使用ADO连接远程oracle数据库
- 使用ADO 对象开发数据库应用程序 (数据库和MFC应用程序连接)
- VC使用ADO连接SQLServer数据库(精简实用版)
- VC使用ADO连接SQLServer数据库
- VC++使用ADO连接SQL Server数据库 .
- VC++使用ADO连接数据库(SqlServe
- VC++下使用ADO访问Access数据库完整篇
- VC使用ADO连接SQLServer数据库
- WINCC中使用ADO对象连接数据库 例子 常用属性 方法 原创