VC++对Access数据库的操作(查询、插入、更新、删除等)
Microsoft Office Access是由 微软 发布的 关系 数据库 管理系统 。 Access 数据库 常应用于小型软件系统中, 比如: 生产管理 、 销售管理 、 库存管理 等各类企业管理软件,其最大的优点是:简单易学、使用灵活。 下面我们结合实例来详细说明,在VC MFC中
Microsoft Office Access是由微软发布的关系数据库管理系统。Access数据库常应用于小型软件系统中,比如:生产管理、销售管理、库存管理等各类企业管理软件,其最大的优点是:简单易学、使用灵活。
下面我们结合实例来详细说明,在VC++ MFC中,如何使用Access数据库文件进行数据的存储,如何实现对数据库中数据的查询、插入、更新和删除等操作。
(实例可在我的CSDN资源中下载:http://download.csdn.net/detail/margin1988/8235865)
首先,怎样创建一个可供VC++ MFC程序使用的Access数据库,并在该数据库中创建数据表呢?
第一步:打开Microsoft Office Access软件,点击“空白数据库”;
第二步:设置预创建空白数据库的文件名和文件类型(文件名:point32.mdb,文件类型:Microsoft Office Access 2000 数据库(*.mdb));
第三步:“创建”空白数据库;
第四步:为该数据库“设置数据库密码”(本例中密码设置为:1234);
第五步:在该数据库中创建一张表,例如:TestTab(编号,姓名,性别,年龄);
第六步:表创建完成后,保存并关闭数据库,然后将该数据库文件(point32.mdb)剪切到你的VC++程序debug或release目录中,则准备工作完成。
其次,在VC++ MFC中编写对该数据库中TestTab表进行数据查询、插入、更新、删除等操作的方法:
(1)导入才用ado方式访问Access数据库所需的DLL
#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")//ado访问ACCESS<strong>数据库</strong>必需
(2)在程序的入口函数中,初始化OLE以支持应用程序
AfxOleInit();
(3)获取应用程序(EXE)所在路径
CString path;//应用程序所在路径 char filepath[256]; char* pPath; GetModuleFileName(AfxGetInstanceHandle(),filepath,256); pPath = strrchr(filepath,'\\'); *pPath = 0; path = filepath;
(4)创建数据库访问连接字符串
char* PtConnectStr;//<strong>数据库</strong>连接字符串 CString connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="; connstr += path; connstr += "\\point32.mdb"; connstr += ";Jet OLEDB:Database Password='1234'"; PtConnectStr = connstr.GetBuffer(0);
(5)查询TestTab表中数据方法实现
//<strong>查询</strong>表中数据,并显示在List Control控件中 void CPoint32Dlg::ReadUserInfo() { //select m_list.DeleteAllItems();//清空列表 _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try { m_pConnection.CreateInstance(__uuidof(Connection)); m_pConnection->Open(PtConnectStr,"","",adModeUnknown); } catch(_com_error e) { CString errormessage; errormessage.Format("<strong>数据库</strong>连接失败.\r错误信息:%s",e.ErrorMessage()); //AfxMessageBox(errormessage); MessageBox(errormessage,"连接失败",MB_ICONEXCLAMATION); if(m_pConnection->State) m_pConnection->Close(); return; } try { //获取数据,放在数据集中 CString cmd; cmd.Format("SELECT * FROM TestTab"); m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open(cmd.GetBuffer(), _variant_t((IDispatch*)m_pConnection,true), adOpenStatic, adLockOptimistic, adCmdText); //处理数据,并显示 _variant_t varbuffer; long index = 0;//注意:必须是long类型 int countItem = 0; CString str; while(!m_pRecordset->adoEOF) { index = 0; //读ID号 varbuffer = m_pRecordset->GetCollect(_variant_t(index)); if(varbuffer.vt!=VT_NULL) { str.Format("%d",varbuffer.lVal); m_list.InsertItem(countItem,str.GetBuffer()); } //读其它的信息 while(index GetCollect(_variant_t(index)); if(varbuffer.vt!=VT_NULL) { str = (LPCTSTR)(_bstr_t)varbuffer; m_list.SetItemText(countItem,index,str.GetBuffer()); } } m_pRecordset->MoveNext(); countItem++; } } catch(_com_error &e) { //AfxMessageBox(e.Description()); MessageBox(e.Description(),"<strong>数据库</strong><strong>操作</strong>失败.",MB_ICONEXCLAMATION); if(m_pRecordset->State) m_pRecordset->Close(); if(m_pConnection->State) m_pConnection->Close(); return; } if(m_pRecordset->State) m_pRecordset->Close(); if(m_pConnection->State) m_pConnection->Close(); }
(6)向TestTab表中插入数据方法实现
//向表中<strong>插入</strong>数据,并<strong>更新</strong>List Control控件中显示的数据 void CPoint32Dlg::OnBnClickedButton1() { //insert _ConnectionPtr m_pConnection; _variant_t RecordsAffected; try { m_pConnection.CreateInstance(__uuidof(Connection)); m_pConnection->Open(PtConnectStr,"","",adModeUnknown); } catch(_com_error e) { CString errormessage; errormessage.Format("<strong>数据库</strong>连接失败.\r错误信息:%s",e.ErrorMessage()); MessageBox(errormessage," 添加失败 ",MB_ICONEXCLAMATION); return; } try { CString strCmd="INSERT INTO TestTab(UName,UGender,UAge) VALUES('测试者','男','30')"; for(int i=0;iExecute(strCmd.AllocSysString(),&RecordsAffected,adCmdText); } } catch(_com_error &e) { //AfxMessageBox(e.Description()); MessageBox(e.Description()," 添加失败 ",MB_ICONEXCLAMATION); if(m_pConnection->State) m_pConnection->Close(); return; } if(m_pConnection->State) m_pConnection->Close(); //MessageBox("添加成功!","消息"); m_update.EnableWindow(TRUE); m_delete.EnableWindow(TRUE); ReadUserInfo(); }
(7)更新TestTab表中数据方法实现
//<strong>更新</strong>表中数据,并<strong>更新</strong>List Control控件的显示 void CPoint32Dlg::OnBnClickedButton3() { // update _ConnectionPtr m_pConnection; _variant_t RecordsAffected; try { m_pConnection.CreateInstance(__uuidof(Connection)); m_pConnection->Open(PtConnectStr,"","",adModeUnknown); } catch(_com_error e) { CString errormessage; errormessage.Format("<strong>数据库</strong>连接失败.\r错误信息:%s",e.ErrorMessage()); MessageBox(errormessage," 修改失败 ",MB_ICONEXCLAMATION); return; } try { CString strCmd="UPDATE TestTab SET [UGender]='女',[UAge]='20' WHERE [UName]='测试者'"; m_pConnection->Execute(strCmd.AllocSysString(),&RecordsAffected,adCmdText); } catch(_com_error &e) { //AfxMessageBox(e.Description()); MessageBox(e.Description()," 修改失败 ",MB_ICONEXCLAMATION); if(m_pConnection->State) m_pConnection->Close(); return; } if(m_pConnection->State) m_pConnection->Close(); //MessageBox("修改成功!","消息"); ReadUserInfo(); }
(8)删除TestTab表中数据及重置表中自动编号主键(key)方法现实
//<strong>删除</strong>表中数据、重置自动编号(从1开始),并<strong>更新</strong>List Control控件显示 void CPoint32Dlg::OnBnClickedButton4() { // delete _ConnectionPtr m_pConnection; _variant_t RecordsAffected; try { m_pConnection.CreateInstance(__uuidof(Connection)); m_pConnection->Open(PtConnectStr,"","",adModeUnknown); } catch(_com_error e) { CString errormessage; errormessage.Format("连接<strong>数据库</strong>失败!\r错误信息:%s",e.ErrorMessage()); MessageBox(errormessage,"<strong>删除</strong>失败",MB_ICONEXCLAMATION); return; } try { //<strong>删除</strong>表中所有数据 CString strCmd="DELETE FROM TestTab"; m_pConnection->Execute(strCmd.AllocSysString(),&RecordsAffected,adCmdText); //重置表中自动编号ID,使其从1开始增加(必须先<strong>删除</strong>表中所有数据) strCmd="ALTER TABLE TestTab ALTER COLUMN ID COUNTER(1,1)"; m_pConnection->Execute(strCmd.AllocSysString(),&RecordsAffected,adCmdText); } catch(_com_error &e) { //AfxMessageBox(e.Description()); MessageBox(e.Description(),"<strong>删除</strong>失败",MB_ICONEXCLAMATION); if(m_pConnection->State) m_pConnection->Close(); return; } if(m_pConnection->State) m_pConnection->Close(); //MessageBox("<strong>删除</strong>成功!","完成"); m_insert.EnableWindow(TRUE); m_update.EnableWindow(FALSE); m_delete.EnableWindow(FALSE); ReadUserInfo(); }

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如何在 Apache 中配置 Zend?在 Apache Web 服务器中配置 Zend Framework 的步骤如下:安装 Zend Framework 并解压到 Web 服务器目录中。创建 .htaccess 文件。创建 Zend 应用程序目录并添加 index.php 文件。配置 Zend 应用程序(application.ini)。重新启动 Apache Web 服务器。

Apache服务器是强大的Web服务器软件,充当浏览器与网站服务器间的桥梁。1. 它处理HTTP请求,根据请求返回网页内容;2. 模块化设计允许扩展功能,例如支持SSL加密和动态网页;3. 配置文件(如虚拟主机配置)需谨慎设置,避免安全漏洞,并需优化性能参数,例如线程数和超时时间,才能构建高性能、安全的Web应用。

本文将阐述如何通过分析Debian系统下的Apache日志来提升网站性能。一、日志分析基础Apache日志记录了所有HTTP请求的详细信息,包括IP地址、时间戳、请求URL、HTTP方法和响应代码等。在Debian系统中,这些日志通常位于/var/log/apache2/access.log和/var/log/apache2/error.log目录下。理解日志结构是有效分析的第一步。二、日志分析工具您可以使用多种工具分析Apache日志:命令行工具:grep、awk、sed等命令行工具可

Oracle不仅是数据库公司,还是云计算和ERP系统的领导者。1.Oracle提供从数据库到云服务和ERP系统的全面解决方案。2.OracleCloud挑战AWS和Azure,提供IaaS、PaaS和SaaS服务。3.Oracle的ERP系统如E-BusinessSuite和FusionApplications帮助企业优化运营。

Nginx性能监控与故障排查主要通过以下步骤进行:1.使用nginx-V查看版本信息,并启用stub_status模块监控活跃连接数、请求数和缓存命中率;2.利用top命令监控系统资源占用,iostat和vmstat分别监控磁盘I/O和内存使用情况;3.使用tcpdump抓包分析网络流量,排查网络连接问题;4.合理配置worker进程数,避免并发处理能力不足或进程上下文切换开销过大;5.正确配置Nginx缓存,避免缓存大小设置不当;6.通过分析Nginx日志,例如使用awk和grep命令或ELK

MySQL适合Web应用和内容管理系统,因其开源、高性能和易用性而受欢迎。1)与PostgreSQL相比,MySQL在简单查询和高并发读操作上表现更好。2)相较Oracle,MySQL因开源和低成本更受中小企业青睐。3)对比MicrosoftSQLServer,MySQL更适合跨平台应用。4)与MongoDB不同,MySQL更适用于结构化数据和事务处理。

vProcesserazrabotkiveb被固定,мнелостольностьстьс粹馏标д都LeavallySumballanceFriablanceFaumDoptoMatification,Čtookazalovnetakprosto,kakaožidal.posenesko

Nginx 限流问题可通过以下方法解决:使用 ngx_http_limit_req_module 限制请求次数;使用 ngx_http_limit_conn_module 限制连接数;使用第三方模块(ngx_http_limit_connections_module、ngx_http_limit_rate_module、ngx_http_access_module)实现更多限流策略;使用云服务(Cloudflare、Google Cloud Rate Limiting、AWS WAF)进行 DD
