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、运行效果如下:

应用程序利用ADO对象访问数据库相关推荐

  1. VC中利用ADO共同实现数据库的操作

    VC中利用ADO共同实现数据库的操作 ADO是应用层的编程接口,它通过OLE DB提供的COM接口访问数据,它适合于各种客户机/服务器应用系统和基于Web的应用,尤其在一些脚本语言中访问数据库操作是A ...

  2. ado.net能访问oracle,利用ADO.NET访问Oracle数据库的实现

    1.前言随着互联网的发展,人们对Web网页的要求越来越多的趋向于动态交互性,而这很大程度上依赖于Web数据库.因为动态交互需要强大的信息系统支持,信息数据以数据库形式表示更容易更新和管理.通过网页如何 ...

  3. 使用ADO.NET访问数据库

    一.ADO.NET:数据库访问的方法和技术. 二.ADO.NEt的重要组件: 1.DataSet:独立于数据源的数据访问 2-Net framework数据提供程序:用于连接到数据库执行命令和检索结果 ...

  4. 利用ADO打造Excel数据库

        知识点巩固和举一反三练习,如下: ADO是一个可以自动化数据操作的COM组建,这点相信大多数自动化测试工程师都不会陌生,在数据验证方面,ADO是一个利器.但ADO在对Excel做数据验证时同样 ...

  5. 第13章 使用ADO.NET访问数据库

    Program using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  6. 使用ADO或ADO控件访问数据库

    目录: 1.数据库的基础知识以及创建Access数据库 2.数据库的接口 3.使用ADO或ADO控件访问数据库 上篇文章,为大家讲解了数据库的接口,大家对ADO也有了较为基础的了解,这篇文章主要解决大 ...

  7. VC访问数据库学习总结

    VC连接数据库方式 目前Windows系统上常见的数据库接口包括: ODBC(开放数据库互连):访问数据库得先配置数据源 MFC(Microsoft基础类)ODBC类 DAO(数据访问对象) RDO( ...

  8. VC用ADO访问数据库全攻略  作者 相生昌

    VC用ADO访问数据库全攻略 作者 相生昌 <script language=VBScript> document.title="VC用ADO访问数据库全攻略 "&am ...

  9. Java程序员从笨鸟到菜鸟之(七十八)细谈Spring(七)spring之JDBC访问数据库及配置详解

    利用spring访问数据库是我们ssh程序中必不可少的步骤,在没有hibernate之前,我们一般都用jdbc访问数据库,所以用jdbc访问数据库必不可少的要进行一些配置,spring中为我们提供了访 ...

最新文章

  1. 步入2021,大热的GNN会在哪些应用领域大展拳脚呢?
  2. python查询天气
  3. mysql编辑前200行_巧妙使用SQL Server编辑前200行功能
  4. Secret Code(原题和变形题)
  5. linux微信公众号报警,zabbix报警媒介,微信报警,邮件报警
  6. html知识收集(一)
  7. poj3279 反转 挑战程序设计竞赛
  8. ES6环境搭建及react-router学习
  9. QTdesigner使用--待更新
  10. 内插滤波器 matlab,多相分解实现内插滤波器.ppt
  11. 哈萨克--阿拉木图之行
  12. google play电子市场和gmail如何安装在国产手机、三星手机、摩托手机里
  13. 如何从GitHub上将代码下载到本地
  14. 黑苹果安装教程 (Mojave 10.14.5) ThinkPad T450
  15. 文都教育2022考研真题解析暨复试备考指导直播峰会圆满落幕
  16. 数据库 1---初识数据库系统
  17. 杰里695N系列(soundbox)之 2.1-APP消息管理
  18. 贵州省毕节市谷歌卫星地图下载
  19. 使用私服管理jar时,下载jar出现 lastUpdated问题 maven
  20. win7计算机 我的文档,Win7系统优化:如何移动Win7我的文档保存位置

热门文章

  1. 全球仅3000人通过的TensorFlow开发人员认证到底有多香!
  2. ESLint 在中大型团队的应用实践
  3. 连载 | 知识图谱发展报告 2018 -- 前言
  4. 深入理解K-Means聚类算法
  5. 一文搞懂HMM(隐马尔可夫模型)-Viterbi algorithm
  6. Android官方开发文档Training系列课程中文版:连接无线设备之通过WIFI创建P2P连接
  7. day32 并发编程之锁
  8. Zookeeper常用命令行及API
  9. Android 为应用添加默认加载页
  10. ligerDialog的使用