ObjectARX中调用ADO封装类实现数据库查询读取写入数据的一个类实例
//#include "dbSimpleClass.h"
#pragma once
#include "stdafx.h"
//
#include "Convert.h"
#include "CAdoDb.h"
#ifndef _dbSimpleClass_H_
#define _dbSimpleClass_H_
//
class dbSimpleClass
{
public:
Convert cvt;
CAdoDb adodb;
//ado连接数据库的方法 函数
void pdbvalue_mdb2();
void pdbvalue_mdb();
//写入
void pdbvalue_write();
};
//-----只引用一次头文件才可以把定义类和实现类功能放在一个文件中
void dbSimpleClass::pdbvalue_mdb2()
{
try
{
//CoInitialize(NULL); //初始化COM,必须的
//
adodb.LoadCOM();
//mdb连接
adodb.SetConnStr_mdb("d:\\test.mdb","admin","");
//
adodb.ConnPtr=adodb.GetConnection();
//
//int* res;
//acedGetInt(_T("输入一个整数:"),&res);
if(adodb.Open()==false)
{
/*ACHAR* tmp=(ACHAR*)adoOp->ErrorMessage;
acutPrintf(tmp);*/
return;
}
_bstr_t sql="select * from student";
_RecordsetPtr rs=adodb.Execute(sql);
if(!rs->adoEOF)
{
rs->MoveFirst();
}
else
{
acutPrintf(ACRX_T("表内数据为空"));
return;
}
_variant_t var;
string strName;
ACHAR* ptmp;
char* line;
while(!rs->adoEOF)
{
var = rs->GetCollect("STDName");
//var =rs->GetFields()->GetItem("STDName")->GetValue();
if(var.vt != VT_NULL)
{
strName = (LPCSTR)_bstr_t(var);
line=(char *)strName.c_str();
ptmp=cvt.ConvertCharPtrToAcharPtr2(line);
acutPrintf(ptmp);acutPrintf(ACRX_T("\n"));
}
//
rs->MoveNext();
}
//rs->Close();
//rs->Release();
//adodb.Dispose();
adodb.UnLoadCOM();
}
catch(_com_error &e)
{
acutPrintf(_T("数据库操作错误! "));
}
}
//OK
void dbSimpleClass::pdbvalue_mdb()
{
CoInitialize(NULL); //初始化COM,必须的
//LoadCOM();
_ConnectionPtr m_pConnection; //创建智能指针,必须的
//m_pConnection.CreateInstance(__uuidof(Connection)); //或
m_pConnection.CreateInstance("ADODB.Connection");
try
{
//连接字符串,Student为数据库名
_bstr_t constr;
//mdb数据库连接字符串
_bstr_t mdbpath="d:\\test.mdb";
constr="Provider=Microsoft.Jet.OLEDB.4.0;";
constr += "Data Source=" + mdbpath + ";";
constr += "user id=Admin;";
constr += "password=;";
constr += "Persist Security Info=True;";
//SQL Server数据库连接字符串
//constr="Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=Student;Data Source=hsg\\hsg";
//Oracle数据库连接字符串
//打开连接方法1
m_pConnection->ConnectionString=constr;
ACHAR* ptmp=cvt.GetAcharPtr(constr);
acutPrintf(ptmp);acutPrintf(ACRX_T("\n"));
//调用Open方法
//m_pConnection->Open("","","",adModeUnknown);
//1
//m_pConnection->Open(constr,"","",adModeUnknown);
//2
m_pConnection->Open(constr,"","",adConnectUnspecified);
acutPrintf(ACRX_T("数据库初始化成功!"));acutPrintf(ACRX_T("\n"));
}
catch(_com_error &e) //捕获连接异常
{
acutPrintf(ACRX_T("数据库初始化错误! ")); acutPrintf(ACRX_T("\n"));
//const TCHAR *msg;
//msg=e.ErrorMessage();//_T("Error Description=");
//cout<<"ErrorMessage="<<msg<<endl;
//
return;
}
acutPrintf(ACRX_T("连接成功!"));acutPrintf(ACRX_T("\n"));
_RecordsetPtr pRst(__uuidof(Recordset));
pRst=m_pConnection->Execute("select * from student",NULL,adCmdText);//使用Execute方法,student为表名
if(!pRst->adoEOF)
{
pRst->MoveFirst();
}
else
{
acutPrintf(ACRX_T("表内数据为空"));
return;
}
// 读入库中各字段并加入列表框中
_variant_t var;
string strName;
//CString strName;
//LPCTSTR strName;
char* line;
ACHAR* ptmp;
while(!pRst->adoEOF)
{
var = pRst->GetCollect("STDName");
//var =pRst->GetFields()->GetItem("STDName")->GetValue();
if(var.vt != VT_NULL)
{
strName = (LPCSTR)_bstr_t(var);
//strName =(char *)_bstr_t(var);
}
line=(char *)strName.c_str();
ptmp=cvt.ConvertCharPtrToAcharPtr2(line);
acutPrintf(ptmp);acutPrintf(ACRX_T("\n"));
pRst->MoveNext();
}
try
{
//关闭
pRst->Close();
m_pConnection->Close();
//Release
pRst.Release();
//m_pConnection->Release(); //这个操作必须去掉
}
catch(_com_error &r)
{
}
//CoUninitialize(); //释放COM
//UnLoadCOM();
}
void dbSimpleClass::pdbvalue_write()
{
adodb.LoadCOM();
//mdb连接
adodb.SetConnStr_mdb("d:\\test.mdb","admin","");
//
adodb.ConnPtr=adodb.GetConnection();
//
if(adodb.Open()==false)
{
/*ACHAR* tmp=(ACHAR*)adoOp->ErrorMessage;
acutPrintf(tmp);*/
return;
}
_bstr_t sql="insert into student(stdid,stdName) values('111','11111')";
bool rbc=adodb.ExecuteNonQuery(sql,adodb.ConnPtr);
sql="insert into student(stdid,stdName) values('666','66666')";
rbc=adodb.ExecuteNonQuery(sql);
//
adodb.Dispose();
adodb.UnLoadCOM();
}
#endif