这是我自己写的封装ADO的类
//cado.h
#include <windows.h>
#import "C:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
typedef struct _DatabasePros
{
_bstr_t DBName; //DatabaseName
_bstr_t DBIp; //DB Server Ip
_bstr_t DBPwd; //DB Password
_bstr_t DBUser; //DB user
}DatabasePros;
class CData
{
private:
_ConnectionPtr m_pConn;
_RecordsetPtr * m_pRs;
DatabasePros m_DBp;
public:
CData();
CData(DatabasePros &DBps);
void SetDatabasePros(DatabasePros &DBps);
bool LinkSQLDatabase();
_RecordsetPtr* ReadRecord(_bstr_t SQLName);
bool InsertRecord(_bstr_t SQLName);
bool UpdateRecord(_bstr_t SQLName);
bool DeleteRecord(_bstr_t SQLName);
bool CloseDatabase();
~CData();
};
//cado.cpp
CData::CData(DatabasePros &DBps)
{
try
{
m_pConn.CreateInstance("ADODB.Connection");
m_pRs=new _RecordsetPtr("ADODB.Recordset");
m_DBp=DBps;
}
catch(_com_error e)
{
throw e;
}
}
CData::CData()
{
try
{
m_pConn.CreateInstance("ADODB.Connection");
}
catch(_com_error e)
{
throw e;
}
}
void CData::SetDatabasePros(DatabasePros &DBps)
{
m_DBp=DBps;
}
bool CData::LinkSQLDatabase()
{
try
{
_bstr_t ConnStr="Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=";
ConnStr+=m_DBp.DBName+";Data Source="+m_DBp.DBIp;
m_pConn->Open(ConnStr,m_DBp.DBUser,m_DBp.DBPwd,adModeUnknown);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}
bool CData::InsertRecord(_bstr_t SQLName)
{
VARIANT RsAffected;
try
{
m_pConn->Execute(SQLName,&RsAffected,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}
bool CData::UpdateRecord(_bstr_t SQLName)
{
VARIANT RsAffected;
try
{
m_pConn->Execute(SQLName,&RsAffected,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}
bool CData::DeleteRecord(_bstr_t SQLName)
{
VARIANT RsAffected;
try
{
m_pConn->Execute(SQLName,&RsAffected,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}
_RecordsetPtr* CData::ReadRecord(_bstr_t SQLName)
{
try
{
(*m_pRs)->Open(SQLName,m_pConn.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
throw e;
}
return m_pRs; //调试的时候别的都正常就在这出的问题,返回的时候出出现了一个错误,忽略以后还是正常运行。
}
bool CData::CloseDatabase()
{
m_pConn->Close();
return true;
}
CData::~CData()
{
delete m_pRs;
m_pConn->Release();
}
//main.cpp
#include <iostream>
#include "Cado.h"
int main()
{
::CoInitialize(NULL);
string strName;
_RecordsetPtr* pRs;
DatabasePros ps;
ps.DBIp="127.0.0.1";
ps.DBName="CorLayoutOffice";
ps.DBPwd="aaa";
ps.DBUser="sa";
CData cd(ps);
cd.LinkSQLDatabase();
pRs=cd.ReadRecord("select UnitName from UnitInfo");
if((*pRs)->RecordCount>0)
{
_variant_t var;
for(int i=1;i<=(*pRs)->RecordCount;i++)
{
var=(*pRs)->GetCollect("UnitName");
if(var.vt!=VT_NULL)strName=(LPCSTR)_bstr_t(var);
cout<<strName<<"\n";
(*pRs)->MoveNext();
}
}
cout<<(*pRs)->RecordCount<<endl;
cd.CloseDatabase();
::CoUninitialize();
return 0;
}
??