本文主要是介绍ADO数据库如何使用?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
下面是我写的一个简单ADO数据库使用的例子,欢迎大家借鉴。
为了使用方便,我封装成了一个函数。
头文件:
//.H//
#pragma once
class ADO
{
public:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
public:
ADO(void);
~ADO(void);
void OnInitADOConn(char* dataBaseName,char * dataSourceType);
_RecordsetPtr& OpenRecordset(CString sql);
void CloseRecordset(void);
void CloseConn(void);
UINT GetRecordCount(_RecordsetPtr pRecordset);
};
/.CPP/
#include "stdafx.h"
#include "ADO.h"
bool oleInit=false;
ADO::ADO(void)
{
}
ADO::~ADO(void)
{
CloseRecordset();
CloseConn();
}
void ADO::OnInitADOConn(char* dataBaseName,char * dataSourceType)
{
ASSERT(dataBaseName);
char string[200];
memset(string,0,200);
if (CString(dataSourceType)==CString("ACCESS"))
{
//access 2003
strcat(string,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
strcat(string,dataBaseName);
strcat(string,";Persist Security Info=False");
}
if (CString(dataSourceType)==CString("SQL"))
{
//sql server 2000
strcat(string,"Provider=SQLOLEDB.1;Password=u99sj;Persist Security Info=True;User ID=sa;Initial Catalog=DataLink;Data Source=");
strcat(string,dataBaseName);
}
::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect=string;
//_bstr_t strConnect=dataBaseName;
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
}
}
_RecordsetPtr& ADO::OpenRecordset(CString sql)
{
ASSERT(!sql.IsEmpty());
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(_bstr_t(sql),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
}
m_pRecordset->MoveFirst();
return m_pRecordset;
}
void ADO::CloseRecordset()
{
if (m_pRecordset->GetState()==adStateOpen)
{
m_pRecordset->Close();
}
}
void ADO::CloseConn()
{
m_pConnection->Close();
::CoUninitialize();
}
UINT ADO::GetRecordCount(_RecordsetPtr pRecordset)
{
int nCount=0;
try
{
pRecordset->MoveFirst();
}
catch (_com_error e)
{
AfxMessageBox(e.Description());
return 0;
}
if (pRecordset->ADOEof)
{
return 0;
}
while(!pRecordset->ADOEof)
{
pRecordset->MoveNext();
nCount++;
}
pRecordset->MoveFirst();
return nCount;
}
/***************************************stdafx.h**************************************************
//导入ADO库
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","ADOEof")
***************************************************************************************************/
/***************************************更改数据库**************************************************
ADO m_ado;
m_ado.OnInitADOConn("DATA_LINK.mdb","ACCESS");
CString sql=_T("select * from mp_net_port");
m_ado.m_pRecordset=m_ado.OpenRecordset(sql);
while(!m_ado.m_pRecordset->ADOEof)
{
m_ado.m_pRecordset->PutCollect("mp_port_ip","192.168.1.102");
m_ado.m_pRecordset->PutCollect("mp_port_enable",true);
m_ado.m_pRecordset->MoveNext();
}
m_ado.CloseRecordset();
***************************************************************************************************/
/***************************************读数据库**************************************************
ADO m_ado;
m_ado.OnInitADOConn("DATA_LINK.mdb","ACCESS");
CString sql=_T("select * from mp_net_port");
m_ado.m_pRecordset=m_ado.OpenRecordset(sql);
while(!m_ado.m_pRecordset->ADOEof)
{
CString str=(LPCTSTR)(_bstr_t)m_ado.m_pRecordset->GetCollect("mp_port_ip");
bool bl=(bool)m_ado.m_pRecordset->GetCollect("mp_port_enable");
m_ado.m_pRecordset->MoveNext();
}
m_ado.CloseRecordset();
***************************************************************************************************/
/***************************************添加数据库**************************************************
ADO m_ado;
m_ado.OnInitADOConn("DATA_LINK.mdb","ACCESS");
CString sql=_T("select * from mp_net_port");
m_ado.m_pRecordset=m_ado.OpenRecordset(sql);
while(!m_ado.m_pRecordset->ADOEof)
{
m_ado.m_pRecordset->AddNew();
m_ado.m_pRecordset->PutCollect("mp_port_ip","192.168.1.102");
m_ado.m_pRecordset->PutCollect("mp_port_enable",true);
m_ado.m_pRecordset->Update();
}
m_ado.CloseRecordset();
***************************************************************************************************/
/***************************************删除数据库**************************************************
ADO m_ado;
m_ado.OnInitADOConn("DATA_LINK.mdb","ACCESS");
CString sql=_T("select * from mp_net_port");
m_ado.m_pRecordset=m_ado.OpenRecordset(sql);
while(!m_ado.m_pRecordset->ADOEof)
{
m_ado.m_pRecordset->Move(0,vtMissing);
m_ado.m_pRecordset->Delete(adAffectCurrent);
m_ado.m_pRecordset->Update();
}
m_ado.CloseRecordset();
***************************************************************************************************/
这篇关于ADO数据库如何使用?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!