ADO 读取 ACCESS

 #pragma once
#include <vector>
// 加入ADO支持库,
#import "C:\Program Files\Common Files\System\ado\msado15.dll" \
no_namespace \
rename ("EOF", "adoEOF") template<typename stDBDATA>
class CADO
{
public:
CADO();
virtual ~CADO();
public:
static void dump_com_error(_com_error &e);
bool Init();
bool Start();
void Uninit();
public:
void OnWrite(char* name, int age);
void OnRead();
void OnDelete();
void OnModify();
void OnExcute(); private: _ConnectionPtr m_pConnection; _CommandPtr m_pCommand;
_RecordsetPtr m_pRecordset;
_RecordsetPtr m_pRecordset1;
char* m_strCommand; }; template<class stDBDATA>
CADO<stDBDATA>::CADO()
{
Init();
} template<class stDBDATA>
CADO<stDBDATA>::~CADO()
{
Uninit();
} template<class stDBDATA>
void CADO<stDBDATA>::dump_com_error(_com_error &e)
{
char ErrorStr[MAX_PATH]; _bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
sprintf_s(ErrorStr, "\n\tADO Error\n\tCode = %08lx\n\tCode meaning = %s\n\tSource = %s\n\tDescription = %s\n\n",
e.Error(), e.ErrorMessage(), (LPCTSTR)bstrSource, (LPCTSTR)bstrDescription);
printf(ErrorStr);
} template<class stDBDATA>
bool CADO<stDBDATA>::Init()
{
// 初始化COM,创建ADO连接等操作
int S_Result = OleInitialize(NULL);
HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection));
/*HRESULT hr = m_pConnection.CreateInstance("ADODB.Connection");*/
if(FAILED(hr))
{
printf("m_pConnection.CreateInstance调用失败\n");
return false;
}
try
{
// 打开本地Access库Demo.mdb
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Demo.mdb","","",adModeUnknown);
}
catch(_com_error& e)
{
dump_com_error(e);
return false;
} return true;
} template<class stDBDATA>
bool CADO<stDBDATA>::Start()
{
// 使用ADO创建数据库记录集
m_pRecordset.CreateInstance(__uuidof(Recordset));
try
{
m_pRecordset->Open("SELECT * FROM USERTABLE", // 查询User表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error& e)
{
dump_com_error(e);
return false;
} return true;
} template<class stDBDATA>
void CADO<stDBDATA>::Uninit()
{
// 关闭ADO连接状态
if(m_pConnection->State)
{
m_pConnection->Close();
m_pConnection= NULL;
} if(m_pRecordset->State)
{
m_pRecordset->Close();
m_pRecordset.Release();
m_pRecordset = NULL;
}
} template<class stDBDATA>
void CADO<stDBDATA>::OnWrite(char* name, int age)
{
long age_l = age;
try
{
// 写入各字段值
m_pRecordset->AddNew();
m_pRecordset->PutCollect("Name", LPSTR(name));
m_pRecordset->PutCollect("Age", age_l);
m_pRecordset->Update();
}
catch(_com_error& e)
{
dump_com_error(e);
}
} template<class stDBDATA>
void CADO<stDBDATA>::OnRead()
{
_variant_t var; char strName[MAX_PATH];
int age; try
{
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
else
{
printf("表内数据为空\n");
return;
} while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("Name");
if(var.vt != VT_NULL)
{
strcpy_s(strName, _bstr_t(var));
}
var = m_pRecordset->GetCollect("Age");
if(var.vt != VT_NULL)
{
age = atol(_bstr_t(var));
} stDBDATA data(strName, age);
data.print(); m_pRecordset->MoveNext();
}
}
catch(_com_error& e)
{
dump_com_error(e);
}
} template<class stDBDATA>
void CADO<stDBDATA>::OnDelete()
{
try
{
m_pRecordset->MovePrevious();
// 删除当前行记录
m_pRecordset->Delete(adAffectCurrent);
m_pRecordset->Update(); }
catch(_com_error& e)
{
dump_com_error(e);
}
} template<class stDBDATA>
void CADO<stDBDATA>::OnModify()
{
char m_Name[MAX_PATH] = "fffff";
long m_Age = ; // 修改当前记录的字段值
try
{
m_pRecordset->PutCollect("Name", _variant_t(m_Name));
m_pRecordset->PutCollect("Age", m_Age);
m_pRecordset->Update();
}
catch(_com_error& e)
{
dump_com_error(e);
}
} template<class stDBDATA>
void CADO<stDBDATA>::OnExcute()
{
_CommandPtr m_pCommand;
m_strCommand = "delete from USERTABLE where Name = 'fffff'"; m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->ActiveConnection = m_pConnection; // 将库连接赋于它
m_pCommand->CommandText = _bstr_t((LPCTSTR)m_strCommand); // SQL语句
try
{
// 执行SQL语句,返回记录集,此记录不能做插入操作
// 故为了不和m_pRecordset相冲突, 放入新定义的m_pRecordset1
m_pRecordset1 = m_pCommand->Execute( NULL, NULL, adCmdText);
}
catch(_com_error& e)
{
dump_com_error(e);
}
}

 

 #include "ado.h"

 struct stDB
{
char name[MAX_PATH];
int age; stDB()
{ } stDB(char* _name, int _age)
{
strcpy_s(name,MAX_PATH, _name);
age = _age;
} void print()
{
printf("Name:%s------Age:%d\n", name, age);
}
}; int main()
{
CADO<stDB> ado;
ado.Start();
ado.OnRead();
ado.OnWrite("q1", );
ado.OnModify();
ado.OnDelete();
ado.OnExcute();
ado.OnRead();
system("pause");
return ;
}
上一篇:Flask学习记录之MarkDown编辑文本


下一篇:Java学习——Number类、Character类