工控编程吧
标题:
上位机通过ADO实现数据库操作完整例程
[打印本页]
作者:
qq263946146
时间:
2017-6-3 18:48
标题:
上位机通过ADO实现数据库操作完整例程
(, 下载次数: 14)
上传
点击文件名下载附件
数据库在WINCC等一些工控产品上会涉及。
当前例程实现数据库常用功能 :
动态创建数据源。
添加数据。
修改数据
删除数据
查找数据
下面是例程中关键代码,完整例程项目工程, 可以帖子尾部下载
void CMy123View::ShowAll(_bstr_t table)//按条件显示数据
{
CListCtrl*pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
_RecordsetPtr record = m_ado.GetRecordset(table);
if(NULL == record)
return;
pList->DeleteAllItems();
if(record->adoBOF)
return;
record->MoveFirst();
while(!record->adoEOF)
{
int i= pList->GetItemCount();
pList->InsertItem(i, (_bstr_t)record->GetCollect("索引"));
pList->SetItemText(i,1,(_bstr_t)record->GetCollect("网站"));
pList->SetItemText(i,2,(_bstr_t)record->GetCollect("网址"));
pList->SetItemText(i,3,(_bstr_t)record->GetCollect("站长"));
record->MoveNext();
}
}
void CMy123View::OnButton1() //向数据表添加内容
{
CListCtrl*pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
CString sIndex,sWebsite,sWebaddr,sWebadmin;
int nIndex = pList->GetItemCount();
sIndex.Format("%d",nIndex);
GetDlgItemText(IDC_EDIT1,sWebsite);
GetDlgItemText(IDC_EDIT2,sWebaddr);
GetDlgItemText(IDC_EDIT3,sWebadmin);
_bstr_t vSQL = "insert into website values('"+sIndex+"','"+sWebsite+"','"+sWebaddr+"','"+sWebadmin+"')";
if(!m_ado.ExecuteSQL(vSQL) )
return;
ShowAll(_bstr_t("select * from website"));
}
void CMy123View::OnButton2()//向数据表删除内容
{
CListCtrl*pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
POSITION pos = pList->GetFirstSelectedItemPosition();
if(NULL == pos)
return;
CString sIndex = pList->GetItemText(pList->GetNextSelectedItem(pos),0);
_bstr_t vSQL = "delete from website where 索引="+sIndex+"";
if(!m_ado.ExecuteSQL(vSQL) )
return;
ShowAll(_bstr_t("select * from website"));
}
void CMy123View::OnButton3() //向数据表修改内容
{
CListCtrl*pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
POSITION pos = pList->GetFirstSelectedItemPosition();
if(NULL == pos)
return;
int nIndex = pList->GetNextSelectedItem(pos);
CString sWebsite,sWebaddr,sWebadmin,sIndex = pList->GetItemText(nIndex,0);
GetDlgItemText(IDC_EDIT1,sWebsite);
GetDlgItemText(IDC_EDIT2,sWebaddr);
GetDlgItemText(IDC_EDIT3,sWebadmin);
_bstr_t vSQL ="Update website SET 网站='"+sWebsite+"',网址='"+sWebaddr+"', 站长='"+sWebadmin+"' where 索引="+sIndex+"";
if(!m_ado.ExecuteSQL(vSQL) )
return;
ShowAll(_bstr_t("select * from website"));
}
void CMy123View::OnButton4() //按条件查找
{
CString sContent;
GetDlgItemText(IDC_EDIT4,sContent);
if(sContent.IsEmpty())
return;
CComboBox*pCom = (CComboBox*)GetDlgItem(IDC_COMBO1);
CString sText;
pCom->GetLBText(pCom->GetCurSel(),sText);
_bstr_t vSQL;
if("索引" == sText)
vSQL = "select * from website where "+sText+" = "+sContent+"";
else
vSQL = "select * from website where "+sText+" = '"+sContent+"'";
ShowAll(vSQL);
}
void CMy123View::OnSelchangeCombo1() //查找组合框更改内容时
{
ShowAll(_bstr_t("select * from website"));
}
void CMy123View::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
CListCtrl*pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
POSITION pos = pList->GetFirstSelectedItemPosition();
if(NULL == pos)
return;
int nInext = pList->GetNextSelectedItem(pos);
SetDlgItemText(IDC_EDIT1,pList->GetItemText(nInext,1));
SetDlgItemText(IDC_EDIT2,pList->GetItemText(nInext,2));
SetDlgItemText(IDC_EDIT3,pList->GetItemText(nInext,3));
*pResult = 0;
}
复制代码
下面是集成类代码
#if !defined(AFX_ADO_H__41E97289_18A5_48A9_9016_6C604EA024E0__INCLUDED_)
#define AFX_ADO_H__41E97289_18A5_48A9_9016_6C604EA024E0__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#import "C:\\Program Files\\Common Files\\System\\ado\\msadox.dll" \
rename("EOF","adoEOF") rename("BOF","adoBOF")
#import "C:\\Program Files\\Common Files\\System\\ado\\msado15.dll"no_namespace \
rename("EOF","adoEOF") rename("BOF","adoBOF")
#include <io.h>
class CADO : public CObject
{
private:
_ConnectionPtr m_pConnect;
_RecordsetPtr m_pRecord;
private:
void Connect();
void DisConnect();
public:
BOOL ExecuteSQL(_bstr_t bstrSQL);
_RecordsetPtr GetRecordset(_bstr_t bstrSQL);
public:
CADO();
virtual ~CADO();
};
#endif // !defined(AFX_ADO_H__41E97289_18A5_48A9_9016_6C604EA024E0__INCLUDED_)
复制代码
// ADO.cpp: implementation of the CADO class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "123.h"
#include "ADO.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
void CADO::Connect()
{
DisConnect();
char cPath[MAX_PATH];
GetModuleFileName(NULL,cPath,MAX_PATH);
CString sPath(cPath);
sPath=sPath.Left(sPath.ReverseFind('\\'));
sPath +="\\data\\Test.mdb";
if(-1 == _access(sPath,0))//数据源文件不存在
{
//创建MDB文件
strcpy(cPath,sPath);
sPath.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source=%s",cPath);
_bstr_t strcnn = sPath;
ADOX::_CatalogPtr pCat;
pCat.CreateInstance(__uuidof(ADOX::Catalog));
pCat->Create(strcnn);
////在MDB文件内创建表格
ADOX::_TablePtr pTable;
pTable.CreateInstance(__uuidof(ADOX::Table));
pTable->PutName("website");
pTable->Columns->Append("索引",ADOX::adInteger,0);
pTable->Columns->Append("网站",ADOX::adVarWChar,50);
pTable->Columns->Append("网址",ADOX::adVarWChar,50);
pTable->Columns->Append("站长",ADOX::adVarWChar,20);
pCat->Tables->Append( pTable.GetInterfacePtr() );
pTable =NULL;
pCat =NULL;
}
strcpy(cPath,sPath);
sPath.Format("Provider=Microsoft.JET.OLEDB.4.0;Data Source=%s",cPath);
try {
m_pConnect.CreateInstance(__uuidof(Connection));
m_pConnect->Open(_bstr_t(sPath),"","",adModeUnknown);
m_pRecord.CreateInstance(__uuidof(Recordset));
}
catch (_com_error e){
AfxMessageBox(e.ErrorMessage());
}
}
void CADO::DisConnect()
{
if(NULL!=m_pRecord&&m_pRecord->State)
m_pRecord->Close();
m_pRecord = NULL;
if(NULL!=m_pConnect&&m_pConnect->State)
m_pConnect->Close();
m_pConnect= NULL;
}
CADO::CADO()
{
::CoInitialize(NULL);
m_pConnect = NULL;
m_pRecord = NULL;
Connect();
}
CADO::~CADO()
{
DisConnect();
::CoUninitialize();
}
BOOL CADO::ExecuteSQL(_bstr_t bstrSQL)
{
try
{ m_pConnect->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e){
AfxMessageBox(e.ErrorMessage());
return false;
}
}
_RecordsetPtr CADO::GetRecordset(_bstr_t bstrSQL)
{
if(NULL!=m_pRecord&&m_pRecord->State)
m_pRecord->Close();
try{
m_pRecord->Open(bstrSQL,m_pConnect.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e){
AfxMessageBox(e.ErrorMessage());
return NULL;
}
return m_pRecord;
}
复制代码
[weixinlianxi]1[/weixinlianxi]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4