应用程序利用ADO对象访问数据库

1、已创建MySQL数据库employeedb,数据库中有如下所示的employeetb表。


2、在使用ADO对象之前,必须在工程的Stdafx.h文件里用直接引入符号#import引入

ADO库文件,以使编译器能够正确编译。代码如下:

#import "D:\Program Files\Common Files\System\ado\msado15.dll"no_namespace rename("EOF","adoEOF")  //我的系统是装在D盘


3、程序代码如下:

// ADO_OBJECT.cpp : Defines the class behaviors for the application.
//#include "stdafx.h"
#include "ADO_OBJECT.h"#include "MainFrm.h"
#include "ADO_OBJECTDoc.h"
#include "ADO_OBJECTView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CADO_OBJECTAppBEGIN_MESSAGE_MAP(CADO_OBJECTApp, CWinApp)//{{AFX_MSG_MAP(CADO_OBJECTApp)ON_COMMAND(ID_APP_ABOUT, OnAppAbout)// NOTE - the ClassWizard will add and remove mapping macros here.//    DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAP// Standard file based document commandsON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)// Standard print setup commandON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()/
// CADO_OBJECTApp constructionCADO_OBJECTApp::CADO_OBJECTApp()
{// TODO: add construction code here,// Place all significant initialization in InitInstance
}/
// The one and only CADO_OBJECTApp objectCADO_OBJECTApp theApp;/
// CADO_OBJECTApp initializationBOOL CADO_OBJECTApp::InitInstance()
{AfxEnableControlContainer();// 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 _AFXDLLEnable3dControls();			// Call this when using MFC in a shared DLL
#elseEnable3dControlsStatic();	// Call this when linking to MFC statically
#endif// Change the registry key under which our settings are stored.// TODO: You should modify this string to be something appropriate// such as the name of your company or organization.SetRegistryKey(_T("Local AppWizard-Generated Applications"));LoadStdProfileSettings();  // Load standard INI file options (including MRU)// Register the application's document templates.  Document templates//  serve as the connection between documents, frame windows and views.CSingleDocTemplate* pDocTemplate;pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,RUNTIME_CLASS(CADO_OBJECTDoc),RUNTIME_CLASS(CMainFrame),       // main SDI frame windowRUNTIME_CLASS(CADO_OBJECTView));AddDocTemplate(pDocTemplate);// Parse command line for standard shell commands, DDE, file openCCommandLineInfo cmdInfo;ParseCommandLine(cmdInfo);// Dispatch commands specified on the command lineif (!ProcessShellCommand(cmdInfo))return FALSE;// The one and only window has been initialized, so show and update it.AfxOleInit();//初始化COM环境m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();return TRUE;
}/
// CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
{
public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected://{{AFX_MSG(CAboutDlg)// No message handlers//}}AFX_MSGDECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAP
END_MESSAGE_MAP()// App command to run the dialog
void CADO_OBJECTApp::OnAppAbout()
{CAboutDlg aboutDlg;aboutDlg.DoModal();
}/
// CADO_OBJECTApp message handlersint CADO_OBJECTApp::ExitInstance() 
{// TODO: Add your specialized code here and/or call the base class::CoUninitialize();//释放对象return CWinApp::ExitInstance();
}


// ADO_OBJECTView.cpp : implementation of the CADO_OBJECTView class
//#include "stdafx.h"
#include "ADO_OBJECT.h"#include "ADO_OBJECTDoc.h"
#include "ADO_OBJECTView.h"
#include "AdoDll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CADO_OBJECTViewIMPLEMENT_DYNCREATE(CADO_OBJECTView, CView)BEGIN_MESSAGE_MAP(CADO_OBJECTView, CView)//{{AFX_MSG_MAP(CADO_OBJECTView)ON_COMMAND(ID_DLL, OnDll)//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()/
// CADO_OBJECTView construction/destructionCADO_OBJECTView::CADO_OBJECTView()
{// TODO: add construction code here}CADO_OBJECTView::~CADO_OBJECTView()
{
}BOOL CADO_OBJECTView::PreCreateWindow(CREATESTRUCT& cs)
{// TODO: Modify the Window class or styles here by modifying//  the CREATESTRUCT csreturn CView::PreCreateWindow(cs);
}/
// CADO_OBJECTView drawingvoid CADO_OBJECTView::OnDraw(CDC* pDC)
{CADO_OBJECTDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here
}/
// CADO_OBJECTView printingBOOL CADO_OBJECTView::OnPreparePrinting(CPrintInfo* pInfo)
{// default preparationreturn DoPreparePrinting(pInfo);
}void CADO_OBJECTView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add extra initialization before printing
}void CADO_OBJECTView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add cleanup after printing
}/
// CADO_OBJECTView diagnostics#ifdef _DEBUG
void CADO_OBJECTView::AssertValid() const
{CView::AssertValid();
}void CADO_OBJECTView::Dump(CDumpContext& dc) const
{CView::Dump(dc);
}CADO_OBJECTDoc* CADO_OBJECTView::GetDocument() // non-debug version is inline
{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CADO_OBJECTDoc)));return (CADO_OBJECTDoc*)m_pDocument;
}
#endif //_DEBUG/
// CADO_OBJECTView message handlersvoid CADO_OBJECTView::OnDll() 
{// TODO: Add your command handler code hereCAdoDll dlg;dlg.DoModal();}

// AdoDll.cpp : implementation file
//#include "stdafx.h"
#include "ADO_OBJECT.h"
#include "AdoDll.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CAdoDll dialogCAdoDll::CAdoDll(CWnd* pParent /*=NULL*/): CDialog(CAdoDll::IDD, pParent)
{//{{AFX_DATA_INIT(CAdoDll)m_num = _T("");m_sala = 0;m_name = _T("");m_dep = _T("");//}}AFX_DATA_INIT
}void CAdoDll::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAdoDll)DDX_Control(pDX, IDC_LIST1, m_list);DDX_Text(pDX, IDC_EDIT_NUM, m_num);DDX_Text(pDX, IDC_EDIT_SALA, m_sala);DDX_Text(pDX, IDC_EDIT_NAME, m_name);DDX_Text(pDX, IDC_EDIT_DEP, m_dep);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAdoDll, CDialog)//{{AFX_MSG_MAP(CAdoDll)ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad)ON_BN_CLICKED(IDC_BUTTON_MOD, OnButtonMod)ON_WM_DESTROY()ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CAdoDll message handlersvoid CAdoDll::OnButtonAdd() 
{// TODO: Add your control notification handler code hereUpdateData(TRUE);if(m_list.GetCount()==0||m_num==""||m_name==""){AfxMessageBox("the input data is not complete!");return;}try{m_pRecordset->AddNew();m_pRecordset->PutCollect("Fnumber",(_variant_t)m_num);m_pRecordset->PutCollect("Fname",(_variant_t)m_name);m_pRecordset->PutCollect("Fdepartment",(_variant_t)m_dep);m_pRecordset->PutCollect("Fsalary",(_variant_t)(long)m_sala);//_variant_t 结构里int 型定义为long 型m_pRecordset->Update();AfxMessageBox("add successfully!");OnButtonLoad() ;// refresh the list}catch(_com_error * e){AfxMessageBox(e->ErrorMessage());}	}void CAdoDll::OnButtonDel() 
{// TODO: Add your control notification handler code hereif(m_list.GetCount()==0){AfxMessageBox("no data to delete!");return;}elseif(m_list.GetCurSel()<0||m_list.GetCurSel()>m_list.GetCount())m_list.SetCurSel(0);try{m_pRecordset->Delete(adAffectCurrent);m_pRecordset->Update();int n=m_list.GetCurSel();m_list.DeleteString(n); //delete the current valueif(n==0&&(m_list.GetCount()!=0))m_list.SetCurSel(n);else if(m_list.GetCount()!=0)m_list.SetCurSel(n-1);OnSelchangeList1();//OnSelChangeList1();// move the pointer}catch(_com_error * e){AfxMessageBox(e->ErrorMessage());}	}void CAdoDll::OnButtonLoad() 
{// TODO: Add your control notification handler code here_variant_t num;m_list.ResetContent();try{if(!m_pRecordset->BOF)m_pRecordset->MoveFirst();else{AfxMessageBox("no data in the table");return;}while(!m_pRecordset->adoEOF){num=m_pRecordset->GetCollect("Fnumber");if(num.vt !=VT_NULL)m_list.AddString((LPCSTR)_bstr_t(num));m_pRecordset->MoveNext();}m_list.SetCurSel(0);  //第一项OnSelchangeList1();}catch(_com_error * e){AfxMessageBox(e->ErrorMessage());}	}void CAdoDll::OnButtonMod() 
{// TODO: Add your control notification handler code hereUpdateData(TRUE);//UpdateBatch(adAffectAll);if(m_list.GetCount()==0||m_num==""||m_name==""){AfxMessageBox("the input data is not complete!");return;}elseif(m_list.GetCurSel()<0||m_list.GetCurSel()>m_list.GetCount())m_list.SetCurSel(0);try{m_pRecordset->PutCollect("Fnumber",_variant_t(m_num));m_pRecordset->PutCollect("Fname",_variant_t(m_name));m_pRecordset->PutCollect("Fdepartment",_variant_t(m_dep));m_pRecordset->PutCollect("Fsalary",(_variant_t)(long)m_sala);m_pRecordset->Update();int n=m_list.GetCurSel();OnButtonLoad() ;m_list.SetCurSel(n);OnSelchangeList1();//OnSelChangeList1();// move the pointer}catch(_com_error * e){AfxMessageBox(e->ErrorMessage());}	}BOOL CAdoDll::OnInitDialog() 
{CDialog::OnInitDialog();// TODO: Add extra initialization here_variant_t value;m_list.ResetContent();m_pConnection.CreateInstance(_uuidof(Connection));m_pRecordset.CreateInstance(_uuidof(Recordset));try{CString strConnect=	"DRIVER={MySQL ODBC 5.3 ANSI Driver};SERVER=127.0.0.1;UID=root;PWD=226;DATABASE=employeedb;CharSet=gbk;";m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);m_pRecordset->Open("select * from employeetb",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);}catch(_com_error * e){AfxMessageBox(e->ErrorMessage());}return TRUE;  // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE
}void CAdoDll::OnDestroy() 
{CDialog::OnDestroy();// TODO: Add your message handler code hereif(m_pRecordset!=NULL)m_pRecordset->Close();m_pConnection->Close();m_pRecordset=NULL;m_pConnection=NULL;	}void CAdoDll::OnSelchangeList1() 
{// TODO: Add your control notification handler code hereint curSel=m_list.GetCurSel();_variant_t va,vaIndex;if(curSel<0)  return;try{m_pRecordset->MoveFirst();m_pRecordset->Move(long(curSel));va=m_pRecordset->GetCollect("Fnumber");if(va.vt!=VT_NULL)m_num=(LPCSTR)_bstr_t(va);va=m_pRecordset->GetCollect("Fname");if(va.vt!=VT_NULL)m_name=(LPCSTR)_bstr_t(va);va=m_pRecordset->GetCollect("Fdepartment");if(va.vt!=VT_NULL)m_dep=(LPCSTR)_bstr_t(va);va=m_pRecordset->GetCollect("Fsalary");m_sala=(short)(va);UpdateData(false);}catch(_com_error *  e){AfxMessageBox(e->ErrorMessage());}	}

3、运行效果如下:



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/492419.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

人工智能让育种“物美价廉”

预测二元化基因表达量的卷积神经网络模型建立来源&#xff1a;中国科学报自从作物被驯化以来&#xff0c;培育集抗性强、优质、高产等性状为一体的作物品种一直是育种家的梦想。DNA分子结构模型的发现推动了分子生物学的发展&#xff0c;让育种家们能够从基因和分子水平上解码作…

Socket编程应用——开发聊天软件

1、客户端应用程序开发 建立一个基于对话框的MFC应用程序&#xff0c;创建的时候记得勾选【Windows Sockets】&#xff0c;其 他的默认就行。 &#xff08;1&#xff09;、对话框如图所示&#xff1a; &#xff08;2&#xff09;代码如下&#xff1a; // ChatClientDlg.cpp …

MFC中给单文档程序添加背景图片

1、在OnDraw函数中修改如下&#xff1a; void CBitmapView::OnDraw(CDC* pDC) {CBitmapDoc* pDoc GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCBitmap bitmap; //位图类对象bitmap.LoadBitmap(IDB_BITMAP1); //从资源中装载入位图CDC dc…

详解|清华大学100页PPT:工业机器人技术详解

来源&#xff1a;清华大学未来智能实验室是人工智能学家与科学院相关机构联合成立的人工智能&#xff0c;互联网和脑科学交叉研究机构。未来智能实验室的主要工作包括&#xff1a;建立AI智能系统智商评测体系&#xff0c;开展世界人工智能智商评测&#xff1b;开展互联网&#…

2019年,中国要推进这70个工程项目

来源&#xff1a;人民日报客户端摘要&#xff1a;近日&#xff0c;《关于2018年国民经济和社会发展计划执行情况与2019年国民经济和社会发展计划草案的报告》正式发布。报告详尽地对2019年我国经济社会的发展做出了安排。围绕基础设施建设、创新发展、社会民生、生态治理、文化…

判断101-200之间有多少个素数,并输出所有素数。

1、代码如下&#xff1a; // test.cpp : Defines the entry point for the console application. // /* 判断101-200之间有多少个素数&#xff0c;并输出所有素数。*/ #include "stdafx.h" #include <iostream> #include <cmath> using namespace std;in…

输入一个十进制数,转化为二进制

1、代码如下&#xff1a; // test.cpp : Defines the entry point for the console application. // /* 输入一个十进制数&#xff0c;转化为二进制。*/ #include "stdafx.h" #include <iostream> using namespace std;int main(int argc, char* argv[]) {cout…

超越“机器人三定律” 人工智能期待新伦理

来源&#xff1a;新华网人工智能的伦理原则近来备受关注。联合国教科文组织总干事阿祖莱在3月初举行的“推动人性化人工智能全球会议”上就表示&#xff0c;目前还没有适用于所有人工智能开发和应用的国际伦理规范框架。对于科幻作家阿西莫夫上世纪设计、防止机器人失控的著名“…

求5阶矩阵其对角线上所有元素之和

1、代码如下&#xff1a; // test.cpp : Defines the entry point for the console application. // /* 输入一个5*5的矩阵&#xff0c;然后输出其对角线上所有元素之和。 当求N阶矩阵其对角线上所有元素之和时&#xff0c;只要把以下程序中所有的5改成N,4改成N-1即可。*/ #inc…

DARPA“终身学习机器”项目取得重大进展

来源&#xff1a;DARPA网站2019年3月&#xff0c;美国防高级研究计划局&#xff08;DARPA&#xff09;“终身学习机器”&#xff08;L2M&#xff09;项目研究人员在《自然机器智能》杂志发表了其有关人工智能算法的研究结果&#xff0c;介绍了一种由类似动物肌腱驱动的人工智能…

《自然》,工程学突破!仿生物细胞群体机器人问世

来源&#xff1a;科技日报摘要&#xff1a;北京3月20日&#xff0c;英国《自然》杂志20日发表了一项工程学最新突破&#xff1a;美国科学家团队研发了一种能模拟生物细胞集体迁移的机器人&#xff0c;可实现移动、搬运物体及向光刺激移动。北京3月20日&#xff0c;英国《自然》…

白宫启动AI.GOV计划,呼吁各界携手共同推进AI发展

来源&#xff1a;网络大数据摘要&#xff1a;近日&#xff0c;白宫启动了 ai.gov 计划&#xff0c;列出了特朗普政府与美国联邦机构采取的一系列人工智能举措&#xff0c;如美国国立卫生研究院(NIH)利用 AI 展开的生物医学研究项目以及美国交通部近期发布的关于自动驾驶汽车的报…

Qt连接MySQL数据库

1、将MySQL安装目录下的libmysql.dll拷贝到Qt安装目录下的bin目录中。 2、准备数据库和数据表如下&#xff1a; 3、编写如下代码&#xff1a; #------------------------------------------------- # # Project created by QtCreator 2016-07-15T17:56:50 # #----------------…

边缘计算不再“边缘”

来源&#xff1a;中国科学报摘要&#xff1a;5G商用时代来临&#xff0c;数据量将更加巨大、复杂&#xff0c;对计算提出更高要求&#xff0c;同时也为发展人工智能、边缘计算带来了新机遇。5G商用时代来临&#xff0c;数据量将更加巨大、复杂&#xff0c;对计算提出更高要求&a…

Qt中修改应用程序和标题栏的图标

一、修改应用程序图标 1.新建一个my.txt文件&#xff0c;打开后在其中加一句 “IDI_ICON1 ICON DISCARDABLE "应用程 序图标.ico"”。&#xff08;“应用程序图标.ico”是要添加的图片名&#xff0c;图片格式一定要是.ico), 然后保存并退出&#xff0c;将文件格式改为…

人类“第六感”首次被证实,研究发现人脑具有磁场感应能力

新证据表明&#xff0c;人类磁感可以让大脑感应到地球磁场来源&#xff1a;神经科技摘要&#xff1a;科学界已经知道鸟类可以利用地磁场进行导航&#xff0c;除此之外&#xff0c;科学家在自然界许多物种中都发现了磁感应能力&#xff0c;生物的磁感受能力也一直在业内被称作生…

用S-函数编写Simulink中的正弦模块

1、用S-函数实现一个正弦波信号源。要求其幅度、频率和初始相位参数可由外部设置&#xff0c;并将这个信号源进行封装。 S-函数程序代码如下&#xff1a; function [sys,x0,str,ts] ch2example17Sfun(t,x,u,flag,Amp,Freq,Phase) % 正弦波信号源 switch flag, case 0 …

2019计算与系统神经科学大会Cosyne 前沿研究汇总

来源&#xff1a;混沌巡洋舰摘要&#xff1a;计算神经科学是一门超级跨学科的新兴学科&#xff0c;几乎综合信息科学&#xff0c;物理学&#xff0c; 数学&#xff0c;生物学&#xff0c;认知心理学等众多领域的最新成果。关注的是神经系统的可塑性与记忆&#xff0c;抑制神经元…

MATLAB中的S-Function的用法(C语言)

1. S-Function简介 S-Function是system-function的缩写。说得简单&#xff0c;S-Function就是用MATLAB所提供的模型不能完全满足用户&#xff0c;而提供给用户自己编写程序来满足自己要求模型的接口。 2. MEX函数与M文件的区别 第一&#xff0c; MEX 函数能实现的回调函数比…

一文读懂民航客机飞控系统

来源&#xff1a;传感器技术摘要&#xff1a;埃塞俄比亚航空公司波音737 MAX 8型客机当地时间10日坠毁&#xff0c;这是时隔不到5个月&#xff0c;波音同一型号飞机发生的第二起空难。鉴于两起事故具有明显的相似性&#xff0c;越来越多的将目标指向了该型号的设计缺陷——飞控…