VC使用ADO对象连接一个Access数据库
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、程序编译运行。运行结果:
VC使用ADO对象连接一个Access数据库相关推荐
- VB.NET学习笔记:ADO.NET操作ACCESS数据库——ADO.NET数据访问接口
一.ADO.NET概述 Ado.net基于XML和离线计算模型. ADO.net的两个核心组件:.Net数据提供程序.DataSet. .net数据提供程序用来与数据库的连接,如SQLServer.n ...
- MFC 使用 ADO技术连接Access数据库
学了很久的MFC,于是想着使用MFC的ADO技术连接Access数据库. 记录一下,以备后面复习. 1.首先需要了解MDB数据库文件,本次示例代码连接的是MDB数据库文件.*.MDB文件是(Micro ...
- ADO.NET连接Access数据库实例
连接Access程序代码片段: using System.Data; using System.Data.OleDb; string strConnection="Provider=Micr ...
- ADO.NET远程访问Access数据库的连接字符串
private string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Work\文件名\em ...
- VC++使用ADO连接SQL Server数据库
基本步骤: 1.创建一个基于对话框的应用程序ADODatabase. 2.创建一个用来连接的ADO类库.系统菜单中Insert->New Class项,class type选择Generic C ...
- VB.NET学习笔记:ADO.NET操作ACCESS数据库——OleDbDataAdapter的Update方法更新数据库的秘密(行状态RowState和行版本 DataRowVersion)
在<数据集DataSet数据管理(DataAdapter查询更新数据库)>一文中提到可以使用OleDbDataAdapter对象的Update方法还可将对 DataSet 所做的更改解析回 ...
- VB.NET学习笔记:ADO.NET操作ACCESS数据库——使用OleDbDataReader对象
OleDbDataReader类提供从数据源读取数据行的只进流的方法. 此类不能被继承.也就是说我们只能通过它读取数据而不能修改数据且读取记录的游标只会不断前进,每次只能读取一行,可以减少系统开销,所 ...
- 64位程序 ADO连接 读取ACCESS数据库解决方案
1.官网下载AccessDatabaseEngine_64.exe https://www.microsoft.com/zh-cn/download/details.aspx?id=13255 2.打 ...
- VC++使用ADO开发ACCESS数据库
VC++使用ADO开发ACCESS数据库 ADO和ADOX到底是什么,二者的作用和区别 ADO是Microsoft 最新推出的数据库访问的高层软件接口.它和Microsoft以前的数据库访问接口DAO ...
最新文章
- 2021年大数据ELK(十一):Elasticsearch架构原理
- ORB_SLAM2代码阅读(2)——tracking线程
- 【亲测有效】如何下载和安装Resilio BTSync
- pandas使用transpose函数对dataframe进行转置、将dataframe的行和列进行互换(flip the rows and columns in dataframe)
- R语言——linux离线安装R包
- ABAddressBookSave关于保存到通讯录失败的问题
- 使用Helm优化Kubernetes下的研发体验:基础设施即代码
- 我是这样开始学编程的
- Java最佳实践– Char到Byte和Byte到Char的转换
- html 自定义打印模板,HTML+CSS入门 自定义模板详解
- Linux内核分析 - 网络[三]:从netif_receive_skb()说起
- 再破记录!2019天猫双11八小时总成交1504.9亿,开场后8分1秒发货量破1亿
- Linux下tensorflow1.13.1 C++ API的编译和使用
- 《Linux编程》作业 ·003【文件I/O操作】
- Fluent使用(一)
- 工业机器人电气系统拆装实训平台
- 生态系统类型空间分布数据/土地利用数据/植被类型数据/NPP数据/土壤侵蚀数据/土壤质地分类/降雨量栅格数据
- word删除页眉线,添加第几页共几页
- Android弹出关闭输入法
- cad显示有点暗_CAD参照的图框颜色太暗怎么办?CAD调整参照图纸明暗的方法
热门文章
- 利用IC负载开关的特性,安全地降低功耗
- java代码生成密钥库_【Java加解密系列】- SM2生成密钥
- Windows XP 超强syskey命令
- ORACLEL临时表空间扩张
- maya! board_如何使用maya和zbrush制作次世代女性角色?值得一辈子收藏的干货
- 如何在微信订阅号里实现oauth授权登录
- Out of memory: Kill process 内存不足
- 微信小程序自定义组件:带未读数目的tabBar按钮(小红点+带未读数目)
- 阿里云服务器公网带宽大小范围200Mbps而非5M带宽
- jenkins的热部署_Jenkins+tomcat自动发布的热部署/重启及遇到的问题解决办法(推荐)...