900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > VC++对Access数据库的操作(查询 插入 更新 删除等)

VC++对Access数据库的操作(查询 插入 更新 删除等)

时间:2020-05-21 02:39:23

相关推荐

VC++对Access数据库的操作(查询 插入 更新 删除等)

数据库|mysql教程

VC++,Access,数据库,操作,查询,插入,更新,删除

数据库-mysql教程

通用型文章系统模板程序源码,ubuntu 卸载新内核,健康数据可以爬虫,php邮编正则,seo全站收录lzw

Microsoft Office Access是由 微软 发布的 关系 数据库 管理系统 。 Access 数据库 常应用于小型软件系统中, 比如: 生产管理 、 销售管理 、 库存管理 等各类企业管理软件,其最大的优点是:简单易学、使用灵活。 下面我们结合实例来详细说明,在VC MFC中

最小二乘法源码,vscode中文设置改不了,ubuntu 源配置,tomcat500报错,爬虫饲养冬季,php部署到nginx,山西短视频seo专业布局,阅读代码网站,克米最新模板lzw

微信小程序 电台源码,vscode过滤提交人,ubuntu 解压tar,tomcat的类加载器,sqlite数据库可以跨平台吗,网页设计报价单模板,虚拟主机的数据库,新网一元服务器,layui整合jquery插件,前端框架 内嵌浏览器设置,淡水爬虫,php 读取目录,seo骗子,springboot基础语法,css怎么把a标签居中,社交分享网站源码,网页管理平台框架,微信模板消息 php,后台管理地址,公司简介 html页面,宿舍管理系统代码设计,易语言程序检测软件lzw

Microsoft Office Access是由微软发布的关系数据库管理系统。Access数据库常应用于小型软件系统中,比如:生产管理、销售管理、库存管理等各类企业管理软件,其最大的优点是:简单易学、使用灵活。

下面我们结合实例来详细说明,在VC++ MFC中,如何使用Access数据库文件进行数据的存储,如何实现对数据库中数据的查询、插入、更新和删除等操作。

(实例可在我的CSDN资源中下载:/detail/margin1988/8235865)

首先,怎样创建一个可供VC++ MFC程序使用的Access数据库,并在该数据库中创建数据表呢?

其次,在VC++ MFC中编写对该数据库中TestTab表进行数据查询、插入、更新、删除等操作的方法:

(1)导入才用ado方式访问Access数据库所需的DLL

#import "C:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")//ado访问ACCESS数据库必需

(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;//数据库连接字符串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表中数据方法实现

//查询表中数据,并显示在List Control控件中void CPoint32Dlg::ReadUserInfo(){//selectm_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("数据库连接失败.\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(),"数据库操作失败.",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表中插入数据方法实现

//向表中插入数据,并更新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("数据库连接失败.\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表中数据方法实现

//更新表中数据,并更新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("数据库连接失败.\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)方法现实

//删除表中数据、重置自动编号(从1开始),并更新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("连接数据库失败!\r错误信息:%s",e.ErrorMessage());MessageBox(errormessage,"删除失败",MB_ICONEXCLAMATION);return;}try{//删除表中所有数据CString strCmd="DELETE FROM TestTab";m_pConnection->Execute(strCmd.AllocSysString(),&RecordsAffected,adCmdText);//重置表中自动编号ID,使其从1开始增加(必须先删除表中所有数据)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(),"删除失败",MB_ICONEXCLAMATION);if(m_pConnection->State)m_pConnection->Close();return;}if(m_pConnection->State)m_pConnection->Close();//MessageBox("删除成功!","完成");m_insert.EnableWindow(TRUE);m_update.EnableWindow(FALSE);m_delete.EnableWindow(FALSE);ReadUserInfo();}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。