代码改变世界

MFC VC++下ADO方式访问连接MySQL数据库 添加 删除 修改数据库

2012-04-25 08:46  Batys  阅读(5778)  评论(1编辑  收藏  举报

这都是以前为了做那个项目测试ADO连接MySQL数据功能的,这里发文分享一下。

实际运行如下图所示,具有列表显示数据库内容,添加,删除,修改功能,点击列表框的任一栏就可以就行修改或者删除,功能算是有点小全面吧。


我们可以从MySQL自带的MySQL workbench看看源数据库里的数据,如图所示。如果没有接触过的可以尝试一下在这里建立数据库,添加删除数据等,操作成功后我们再用VC进行操作,看看我们写的程序可不可以删除修改等操作,关于MySQL workbench这里不介绍。
我们首先建立一个VC工程,选择对话框,添加资源如下图所示


注意列表框VIEW要选择report模式)

在stdafx.h文件中要添加如下代码,因为要包含数据库相关的函数,以及ADO操作的DLL动态链接库

#include <afxdb.h>        // MFC Automation classes
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")

对于列表框我们要在对话框的初始化函数里进行操作,如添加第一行项目以及扩展风格修改

 int i;
 CString listonetable[]={_T("姓名"),\
  _T("性别"),\
  _T("账户"),\
  _T("密码"),\
  _T("班级")};
 for( i=0;i<5;i++)
 {
  m_listone.InsertColumn(i,listonetable[i],LVCFMT_CENTER,90);//添加项目栏
 }

 DWORD dwStyle = m_listone.GetExtendedStyle(); 
 dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮(只适用与report风格的listctrl) 
 dwStyle |= LVS_EX_GRIDLINES;//网格线(只适用与report风格的listctrl) 
 m_listone.SetExtendedStyle(dwStyle); //设置扩展风格

 

我们要把数据从MySQL数据库读取出来,然后显示到列表框中,该操作大致函数如下,我们应该在对话框的初始化函数中调用,让程序一运行就把数据列表显示出来。

 CAdoConnect m_ADO;
 m_ADO.OnInitADOConn();
 //设置SELECT语句
 _bstr_t vSQL="select * from TestSql";
 _RecordsetPtr m_pRecordset;
 m_pRecordset=m_ADO.GetRecordSet(vSQL);
 if(!m_pRecordset->adoBOF)
 {
  m_pRecordset->MoveFirst();
 }
 _variant_t valuetl;
 i=0;
 try
 {
  while(!m_pRecordset->adoEOF)
  {
   CString str;
   valuetl=m_pRecordset->GetFields()->GetItem("name")->Value;
   str=(LPCTSTR)(_bstr_t)valuetl;
   m_listone.InsertItem(i,str);

   valuetl=m_pRecordset->GetFields()->GetItem("sex")->Value;
   str=(LPCTSTR)(_bstr_t)valuetl;
   m_listone.SetItemText(i,1,str);
   valuetl=m_pRecordset->GetFields()->GetItem("loginid")->Value;
   str=(LPCTSTR)(_bstr_t)valuetl;
   m_listone.SetItemText(i,2,str);
   valuetl=m_pRecordset->GetFields()->GetItem("loginpw")->Value;
   str=(LPCTSTR)(_bstr_t)valuetl;
   m_listone.SetItemText(i,3,str);
   valuetl=m_pRecordset->GetFields()->GetItem("class")->Value;
   str=(LPCTSTR)(_bstr_t)valuetl;
   m_listone.SetItemText(i,4,str);
   i++;
   m_pRecordset->MoveNext();
  } 
 }
 catch(_com_error e)
 {
  AfxMessageBox(e.Description());
 }
 m_ADO.ExitConnect();
 m_pRecordset=NULL;

然后我们要对列表框单击消息进行处理,使得单击后列表框下面一排的编辑框能够实时显示选择的一行的信息,正如第一张截图所示,也为了修改信息做准备。进入MFC ClassWizard对列表框添加NM_CLICK消息,并编辑代码,如图所示

编辑代码如下

void CListmysqlDlg::OnClickList2(NMHDR* pNMHDR, LRESULT* pResult) 
{
 // TODO: Add your control notification handler code here
 int i=m_listone.GetNextItem(-1, LVIS_SELECTED);//得到选中的行

 if(i>=0)//有选中
 {
  m_name = m_listone.GetItemText(i,0);
  m_sex = m_listone.GetItemText(i,1);
  m_ID = m_listone.GetItemText(i,2);
  m_PW = m_listone.GetItemText(i,3);
  m_class = m_listone.GetItemText(i,4);//获取列表框信息
  UpdateData(FALSE);//更新数据到编辑框
 }
 *pResult = 0;
}

再分析一下单击添加按钮,首先读取要添加的编辑框里的内容,然后和数据库内容作比较,如果数据库已经存在该数据,那么表示重复,是不允许添加的,防止添加重复无效的数据。

void CListmysqlDlg::OnButtonAdd() 
{
 // TODO: Add your control notification handler code here
 UpdateData(TRUE);

 CAdoConnect m_ADO;
 m_ADO.OnInitADOConn();
 //设置SELECT语句
 _bstr_t vSQL;
 CString Cstrsql;
 Cstrsql.Format(_T("select * from testsql where name = '%s'"),m_name);
 vSQL=  _bstr_t((LPCTSTR)Cstrsql);

 _RecordsetPtr m_pRecordsetCheck(__uuidof(Recordset));
 m_pRecordsetCheck->CursorLocation = adUseClient;
 m_pRecordsetCheck->Open(vSQL,m_ADO.m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
 int i=m_pRecordsetCheck->RecordCount;

 if(i>0)
 {
  m_pRecordsetCheck->Close();
  m_ADO.ExitConnect();
  ::MessageBox(NULL,_T("记录已存在,请检查!"),_T("添加失败"),MB_OK|MB_ICONWARNING);
  return;
 }
 else
 {
  m_pRecordsetCheck->Close();
  m_pRecordsetCheck=NULL;
  vSQL="select * from testsql";
  _RecordsetPtr m_pRecordset;
  m_pRecordset=m_ADO.GetRecordSet(vSQL);

  try
  {
   m_pRecordset->AddNew();
   m_pRecordset->PutCollect("name",_variant_t(m_name));
   m_pRecordset->PutCollect("sex",_variant_t(m_sex));
   m_pRecordset->PutCollect("loginid",_variant_t(m_ID));
   m_pRecordset->PutCollect("loginpw",_variant_t(m_PW));
   m_pRecordset->PutCollect("class",_variant_t(m_class));
   m_pRecordset->Update();
   m_pRecordset->MoveLast();
  }
  catch(_com_error e)
  {
   AfxMessageBox(e.Description());
  }
  m_ADO.ExitConnect();
  m_pRecordset=NULL;

  i= m_listone.GetItemCount();
  m_listone.InsertItem(i,m_name);
  m_listone.SetItemText(i,1,m_sex);
  m_listone.SetItemText(i,2,m_ID);
  m_listone.SetItemText(i,3,m_PW);
  m_listone.SetItemText(i,4,m_class);

  AfxMessageBox("添加记录成功!");
 
 } 
}

 

 

附:ADO连接MySQL,这里有几个比较重要的函数,把它们封装到一起,便于使用

#include "stdafx.h"
#include "listmysql.h"
#include "AdoConnect.h"

CAdoConnect::CAdoConnect()
{
}

CAdoConnect::~CAdoConnect()
{
}


void CAdoConnect::OnInitADOConn()
{
 // 初始化OLE/COM库环境 
    ::CoInitialize(NULL);
 try
  {
  // 创建Connection对象      
   m_pConnection.CreateInstance("ADODB.Connection");
   m_pConnection->Open("DSN=Testmysqllist;Server= localhost;Database=test","root","root",adModeUnknown);
  } 
  // 捕捉异常
  catch(_com_error e)
  {
   // 显示错误信息
   AfxMessageBox(e.Description());
  }


}


_RecordsetPtr&  CAdoConnect::GetRecordSet(_bstr_t bstrSQL)
{
 try
 {
  // 连接数据库,如果Connection对象为空,则重新连接数据库
  if(m_pConnection==NULL)
   OnInitADOConn();
  // 创建记录集对象
  m_pRecordset.CreateInstance(__uuidof(Recordset));
  // 取得表中的记录
  m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
 }
 // 捕捉异常
 catch(_com_error e)
 {
  // 显示错误信息
  AfxMessageBox(e.Description());
 }
 // 返回记录集
 return m_pRecordset;
}
// 执行SQL语句,Insert Update _variant_t
BOOL CAdoConnect::ExecuteSQL(_bstr_t bstrSQL)
{
 //    _variant_t RecordsAffected;
 try
 {
  // 是否已经连接数据库
  if(m_pConnection == NULL)
   OnInitADOConn();
  // Connection对象的Execute方法:(_bstr_t CommandText, 
  // VARIANT * RecordsAffected, long Options ) 
  // 其中CommandText是命令字串,通常是SQL命令。
  // 参数RecordsAffected是操作完成后所影响的行数, 
  // 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
  // adCmdProc-存储过程;adCmdUnknown-未知
  m_pConnection->Execute(bstrSQL,NULL,adCmdText);
  return true;
 }
 catch(_com_error e)
 {
  AfxMessageBox(e.Description());
  return false;
 }
}

void CAdoConnect::ExitConnect()
{
 // 关闭记录集和连接
 if (m_pRecordset != NULL)
  m_pRecordset->Close();
 m_pConnection->Close();
 m_pRecordset=NULL;
 m_pConnection=NULL;
 // 释放环境
 ::CoUninitialize();
}

整个工程代码下载:http://www.kuaipan.com.cn/file/id_47425243630796802.htm

用到的数据库下载:http://www.kuaipan.com.cn/file/id_47425243630796803.htm

注意:本程序需要MySQL数据库才可以运行,将下载的数据库导入并建立数据源,注意数据源必须和下图一致,不能填错,用户名密码都设为root,该操作请参考上一篇文章:windows下MySQL数据库安装 导入 导出以及数据源建立详细教程