首页 #VC++中链接数据库并实现(添加修改删除清空)功能

#VC++中链接数据库并实现(添加修改删除清空)功能

举报
开通vip

#VC++中链接数据库并实现(添加修改删除清空)功能VC++中用ADO编程方法链接数据库并实现添加、修改、删除、清空等功能1、数据库为MicrosoftOfficeAccess2003为例2、数据库名为DataBase.mdb,表名employees,添加编号、学号、姓名字段,类型为文本。数据库放在p5根目录下。3、建立新建项目MFCAppWizard[exe]名称为p5。选择基本对话框。然后添加控件,一个列表控件,三个编辑框,五个按钮。如下在列表控件,右键属性。设置如下:1/24然后建立类向导,在Member...

#VC++中链接数据库并实现(添加修改删除清空)功能
VC++中用ADO编程方法链接数据库并实现添加、修改、删除、清空等功能1、数据库为MicrosoftOfficeAccess2003为例2、数据库名为DataBase.mdb,表名employees,添加编号、学号、姓名字段,类型为文本。数据库放在p5根目录下。3、建立新建项目MFCAppWizard[exe]名称为p5。选择基本对话框。然后添加控件,一个列表控件,三个编辑框,五个按钮。如下在列表控件,右键属性。设置如下:1/24然后建立类向导,在MemberVariables视图中为3个编辑框和1个列表控件添加变量名称和类型如图:五个按钮的ID和名字改为:IDC_BUTADD,添加;IDC_BUTMOD,修改;IDC_BUTDEL,删除;IDC_BUTCLICK,清空;IDC_BUTTON1,退出。4、为项目加类ADO类,在ClassView中视图中在p5classes上面点右键新建类,类的类型选GenericClass,名称为ADO,点确定。同时在StdAfx.h头文件中加入ADO动态链接库,代码如下:/24#import"C:\ProgramFiles\CommonFiles\System\ado\msado15.dll"rename("EOF","adoEOF">rename("BOF","adoBOF">//导入ADO动态链接库在ClassView视图中的CP5Dlg中加入自定义函数AddToGrid和OnButclear。这两个函数是手动添加的。5、在ADO.app中程序为//ADO.cpp:implementationoftheADOclass.////////////////////////////////////////////////////////////////////////#include"stdafx.h"#include"p5.h"#include"ADO.h"#ifdef_DEBUG#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__。#definenewDEBUG_NEW#endif////////////////////////////////////////////////////////////////////////Construction/Destruction///////////////////////////////////////////////////////////////////////24ADO::ADO(>{}ADO::~ADO(>{}voidADO::OnInitADOConn(>{::CoInitialize(NULL>。try{m_pConnection.CreateInstance("ADODB.Connection">。//创建连接对象实例_bstr_tstrConnect="DRIVER={MicrosoftAccessDriver(*.mdb>}。uid=。pwd=。DBQ=DataBase.mdb。"。m_pConnection->Open(strConnect,"","",adModeUnknown>。//打开数据库}catch(_com_errore>{AfxMessageBox(e.Description(>>。//弹出错误处理/24_RecordsetPtr&ADO::OpenRecordset(CStringsql>{ASSERT(!sql.IsEmpty(>>。//SQL语句不能为空try{m_pRecordset.CreateInstance(__uuidof(Recordset>。>//创建记录集对象实例m_pRecordset->Open(_bstr_t(sql>,m_pConnection.GetInterfacePtr(>,adOpenDynamic,adLockOptimistic,adCmdText>。//执行SQL得到记录集}catch(_com_errore>//捕获可能的异常{AfxMessageBox(e.Description(>>。}returnm_pRecordse。t/24voidADO::CloseRecordset(>{adStateOpen>if(m_pRecordset->GetState(>==//判断当前的记录集状态m_pRecordset->Close(>。//关闭记录集}voidADO::CloseConn(>{m_pConnection->Close(>。//关闭数据库连接::CoUninitialize(>。//释放COM环境}UINTADO::GetRecordCount(_RecordsetPtrpRecordset>{intnCount=0。//声明保存记录数的变量try{pRecordset->MoveFirst(>。//将记录集指针移动到第一条记录/24catch(...>//捕捉可能出现的错误{return0。//产生错误时返回0}if(pRecordset->adoEOF>//判断记录集中是否没有记录return0。//无记录时返回0while(!pRecordset->adoEOF>//当记录集指针没有指向最后时{pRecordset->MoveNext(>。//将记录集指针移动到下一条记录nCount=nCount+1。//记录个数的变量加1}pRecordset->MoveFirst(>。//将记录集指针移动到第一条记录returnnCount。//返回记录数/24}6、在ADO.h头文件中程序为//ADO.h:interfacefortheADOclass.////////////////////////////////////////////////////////////////////////#if!defined(AFX_ADO_H__E0F8432E_F797_4979_8FB0_EAC60CA6C7F9__INCLUDED_>#defineAFX_ADO_H__E0F8432E_F797_4979_8FB0_EAC60CA6C7F9__INCLUDED_#if_MSC_VER>1000#pragmaonce#endif//_MSC_VER>1000classADO{public:_ConnectionPtrm_pConnection。//连接对象指针_RecordsetPtrm_pRecordse。t//记录集对象指针public:ADO(>。/24virtual~ADO(>。voidOnInitADOConn(>。//连接数据库_RecordsetPtr&OpenRecordset(CStringsql>。//打开记录集voidCloseRecordset(>。//关闭记录集voidCloseConn(>。//关闭数据库连接UINTGetRecordCount(_RecordsetPtrpRecordset>。//获得记录数}。#endif//!defined(AFX_ADO_H__E0F8432E_F797_4979_8FB0_EAC60CA6C7F9__INCLUDED_>这是一个封装的连接数据库文件。7、在p5Dlg.cpp文件中为添加、修改、删除、清空、退出和列表控件添加函数。p5Dlg.cpp全部程序如下://p5Dlg.cpp:implementationfile//#include"stdafx.h"#include"p5.h"/24#include"p5Dlg.h"#include"ADO.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__。#endif///////////////////////////////////////////////////////////////////////////////CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialog{public:CAboutDlg(>。//DialogData//{{AFX_DATA(CAboutDlg>enum{IDD=IDD_ABOUTBOX}。//}}AFX_DATA//ClassWizardgeneratedvirtualfunctionoverrides//{{AFX_VIRTUAL(CAboutDlg>protected:virtualvoidDoDataExchange(CDataExchange*pDX>。//DDX/DDVsupport/24//}}AFX_VIRTUAL//Implementationprotected://{{AFX_MSG(CAboutDlg>//}}AFX_MSGDECLARE_MESSAGE_MAP(>}。CAboutDlg::CAboutDlg(>:CDialog(CAboutDlg::IDD>{//{{AFX_DATA_INIT(CAboutDlg>//}}AFX_DATA_INIT}voidCAboutDlg::DoDataExchange(CDataExchange*pDX>{CDialog::DoDataExchange(pDX>。//{{AFX_DATA_MAP(CAboutDlg>//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CAboutDlg,CDialog>//{{AFX_MSG_MAP(CAboutDlg>//Nomessagehandlers//}}AFX_MSG_MAP/24END_MESSAGE_MAP(>///////////////////////////////////////////////////////////////////////////////CP5DlgdialogCP5Dlg::CP5Dlg(CWnd*pParent/*=NULL*/>:CDialog(CP5Dlg::IDD,pParent>{//{{AFX_DATA_INIT(CP5Dlg>m_ID=_T("">。m_Name=_T("">。m_Culture=_T("">。//}}AFX_DATA_INIT//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32m_hIcon=AfxGetApp(>->LoadIcon(IDR_MAINFRAME>。}voidCP5Dlg::DoDataExchange(CDataExchange*pDX>{CDialog::DoDataExchange(pDX>。//{{AFX_DATA_MAP(CP5Dlg>DDX_Control(pDX,IDC_LIST3,m_Grid>。DDX_Text(pDX,IDC_EDIT1,m_ID>。DDX_Text(pDX,IDC_EDIT2,m_Name>。/24DDX_Text(pDX,IDC_EDIT3,m_Culture>。//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CP5Dlg,CDialog>//{{AFX_MSG_MAP(CP5Dlg>ON_WM_SYSCOMMAND(>ON_WM_PAINT(>ON_WM_QUERYDRAGICON(>ON_BN_CLICKED(IDC_BUTADD,OnButadd>ON_BN_CLICKED(IDC_BUTMOD,OnButmod>ON_BN_CLICKED(IDC_BUTDEL,OnButdel>ON_BN_CLICKED(IDC_BUTCLICK,OnButclick>ON_NOTIFY(NM_CLICK,IDC_LIST3,OnClickList3>ON_BN_CLICKED(IDC_BUTTON1,OnButton1>//}}AFX_MSG_MAPEND_MESSAGE_MAP(>///////////////////////////////////////////////////////////////////////////////CP5DlgmessagehandlersBOOLCP5Dlg::OnInitDialog(>{CDialog::OnInitDialog(>。//Add"About..."menuitemtosystemmenu./240xFFF0>//IDM_ABOUTBOXmustbeinthesystemcommandrange.ASSERT((IDM_ABOUTBOXIDM_ABOUTBOX>。ASSERT(IDM_ABOUTBOX<0xF000>。CMenu*pSysMenu=GetSystemMenu(FALSE>。if(pSysMenu!=NULL>{CStringstrAboutMenu。strAboutMenu.LoadString(IDS_ABOUTBOX>。if(!strAboutMenu.IsEmpty(>>{pSysMenu->AppendMenu(MF_SEPARATOR>。pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu>。}}//Settheiconforthisdialog.Theframeworkdoesthisautomatically//whentheapplication'smainwindowisnotadialogSetIcon(m_hIcon,TRUE>。//SetbigiconSetIcon(m_hIcon,FALSE>。//Setsmallicon//TODO:Addextrainitializationhere/24m_Grid.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES>。m_Grid.InsertColumn(0,"编号",LVCFMT_LEFT,120,0>。m_Grid.InsertColumn(1,"姓名",LVCFMT_LEFT,120,1>。m_Grid.InsertColumn(2,"学历",LVCFMT_LEFT,120,2>。AddToGrid(>。returnTRUE。//returnTRUEunlessyousetthefocustoacontrol}voidCP5Dlg::OnSysCommand(UINTnID,LPARAMlParam>{if((nID&0xFFF0>==IDM_ABOUTBOX>{CAboutDlgdlgAbout。dlgAbout.DoModal(>。}else{CDialog::OnSysCommand(nID,lParam>。/24//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,//thisisautomaticallydoneforyoubytheframework.voidCP5Dlg::OnPaint(>{if(IsIconic(>>{CPaintDCdc(this>。//devicecontextforpaintingSendMessage(WM_ICONERASEBKGND,(WPARAM>dc.GetSafeHdc(>,0>。//CentericoninclientrectangleintcxIcon=GetSystemMetrics(SM_CXICON>。intcyIcon=GetSystemMetrics(SM_CYICON>。CRectrect。GetClientRect(&rect>。intx=(rect.Width(>-cxIcon+1>/2。inty=(rect.Height(>-cyIcon+1>/2。//Drawtheicon/24dc.DrawIcon(x,y,m_hIcon>。else{CDialog::OnPaint(>。}}//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags//theminimizedwindow.HCURSORCP5Dlg::OnQueryDragIcon(>{return(HCURSOR>m_hIcon。}voidCP5Dlg::OnButadd(>{//TODO:AddyourcontrolnotificationhandlercodehereUpdateData(TRUE>。if(m_ID.IsEmpty(>||m_Name.IsEmpty(>||m_Culture.IsEmpty(>>{MessageBox("基础信息不能为空!">。return。/24ADOm_Ado。m_Ado.OnInitADOConn(>。CStringsql="select*fromemployees"。m_Ado.m_pRecordset=m_Ado.OpenRecordset(sql>。try{m_Ado.m_pRecordset->AddNew(>。//添加新行m_Ado.m_pRecordset->PutCollect("编号",(_bstr_t>m_ID>。m_Ado.m_pRecordset->PutCollect("姓名",(_bstr_t>m_Name>。m_Ado.m_pRecordset->PutCollect("学历",(_bstr_t>m_Culture>。m_Ado.m_pRecordset->Update(>。//更新数据表记录m_Ado.CloseRecordset(>。m_Ado.CloseConn(>。}catch(...>{MessageBox("操作失败">。return。/24}MessageBox("添加成功">。m_Grid.DeleteAllItems(>。//删除列表控件AddToGrid(>。}voidCP5Dlg::AddToGrid(>{ADOm_Ado。m_Ado.OnInitADOConn(>。//连接数据库CStringSQL="select*fromemployeesorderby编号desc"。//设置查询字符串m_Ado.m_pRecordsetm_Ado.OpenRecordset(SQL>。//打开记录集while(!m_Ado.m_pRecordset->adoEOF>{m_Grid.InsertItem(0,"">。m_Grid.SetItemText(0,0,(char*>(_bstr_t>m_Ado.m_pRecordset->GetCollect("编号">>。m_Grid.SetItemText(0,1,(char*>(_bstr_t>m_Ado.m_pRecordset->GetCollect("姓名">>。/24m_Grid.SetItemText(0,2,(char*>(_bstr_t>m_Ado.m_pRecordset->GetCollect("学历">>。m_Ado.m_pRecordset->MoveNext(>。//将记录集指针移动到下一条记录}m_Ado.CloseRecordset(>。m_Ado.CloseConn(>。//断开数据库连接}voidCP5Dlg::OnButmod(>{UpdateData(TRUE>。if(m_ID.IsEmpty(>||m_Name.IsEmpty(>||m_Culture.IsEmpty(>>{MessageBox("基础信息不能为空!">。return。}intpos=m_Grid.GetSelectionMark(>。ADOm_Ado。m_Ado.OnInitADOConn(>。CStringsql="select*fromemployees"。/24m_Ado.m_pRecordset=m_Ado.OpenRecordset(sql>。try{m_Ado.m_pRecordset->Move((long>pos,vtMissing>。m_Ado.m_pRecordset->PutCollect("编号",(_bstr_t>m_ID>。m_Ado.m_pRecordset->PutCollect("姓名",(_bstr_t>m_Name>。m_Ado.m_pRecordset->PutCollect("学历",(_bstr_t>m_Culture>。m_Ado.m_pRecordset->Update(>。m_Ado.CloseRecordset(>。m_Ado.CloseConn(>。}catch(...>{MessageBox("操作失败">。return。}MessageBox("修改成功">。m_Grid.DeleteAllItems(>。AddToGrid(>。//TODO:Addyourcontrolnotificationhandler/24codehere}voidCP5Dlg::OnButdel(>{//TODO:Addyourcontrolnotificationhandlercodehereintpos=m_Grid.GetSelectionMark(>。ADOm_Ado。m_Ado.OnInitADOConn(>。CStringsql="select*fromemployees"。m_Ado.m_pRecordset=m_Ado.OpenRecordset(sql>。try{m_Ado.m_pRecordset->Move(pos,vtMissing>。m_Ado.m_pRecordset->Delete(adAffectCurrent>。m_Ado.m_pRecordset->Update(>。m_Ado.CloseRecordset(>。m_Ado.CloseConn(>。}catch(...>{MessageBox("操作失败">。return。/24}MessageBox("删除成功">。OnButclear(>。m_Grid.DeleteAllItems(>。AddToGrid(>。}voidCP5Dlg::OnButclick(>{//TODO:Addyourcontrolnotificationhandlercodeherem_ID=""。m_Name=""。m_Culture=""。UpdateData(FALSE>。}voidCP5Dlg::OnButclear(>{m_ID=""。m_Name=""。m_Culture=""。UpdateData(FALSE>。}voidCP5Dlg::OnClickList3(NMHDR*pNMHDR,LRESULT*pResult>/24{//TODO:Addyourcontrolnotificationhandlercodehereintpos=m_Grid.GetSelectionMark(>。m_ID=m_Grid.GetItemText(pos,0>。m_Name=m_Grid.GetItemText(pos,1>。m_Culture=m_Grid.GetItemText(pos,2>。UpdateData(FALSE>。*pResult=0。}voidCP5Dlg::OnButton1(>{//TODO:AddyourcontrolnotificationhandlercodehereOnOK(>。//exit(0>。和上面OnOK(>。功能一样。}8、其中函数AddToGrid和OnButclear是自己添加的函数。在ClassView视图中的CP5Dlg中加入自定义函数AddToGrid和OnButclear,程序代码以上程序中已写了。这两个函数是手动添加的。/24
本文档为【#VC++中链接数据库并实现(添加修改删除清空)功能】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
王淇
热爱文库,热爱新浪。
格式:doc
大小:93KB
软件:Word
页数:31
分类:
上传时间:2022-08-18
浏览量:0