#import "C:Program FilesCommon FilesSystemadomsado15.dll" no_namespace rename("EOF","rsEOF")
#include<iostream>
using namespace std;
//数据库基本操作类
class DataAccess
{
private:
//申明数据库对象
_ConnectionPtr pConn;
_RecordsetPtr pRst;
_CommandPtr pCmd;
//连接标识符
bool flag;
public:
DataAccess()
{
//初始化连接对象
CoInitialize(NULL);
//创建ADO连接实例
pConn.CreateInstance(__uuidof(Connection));
//创建ADO记录集实例
pRst.CreateInstance(__uuidof(Recordset));
//创建ADO命令实例
pCmd.CreateInstance(__uuidof(Command));
//定义连接字符
pConn->ConnectionString="Provider=SQLOLEDB;Server=(local);Database=Book; uid=sa; pwd=;";
//标识符默认为false
flag=false;
}
~DataAccess()
{
//记录集不为空,释放记录集
if(pRst!=NULL)
{
pRst->Close();
pRst.Release();
}
//Cmd类不为空,释放Cmd
if(pCmd!=NULL)
{
pCmd.Release();
}
//连接不为空,释放连接
if(pConn!=NULL)
{
pConn->Close();
pConn.Release();
}
}
//打开数据库,成功返回TRUE,失败返回FALSE
BOOL openConn()
{
try
{
this->pConn->Open("","","",adConnectUnspecified);
flag=true;
return flag;
}
catch(_com_error &e)
{
return FALSE;
}
}
//执行SQL语句,成功返回_Recordset,失败返回NULL
_RecordsetPtr searchSql(_bstr_t sql)
{
try
{
if(flag==false)
{
this->openConn();
}
pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
pCmd->CommandText=sql;
this->pRst->Open(sql,(IDispatch*)this->pConn,adOpenDynamic,adLockOptimistic,adCmdText);
//this->pRst=pCmd->Execute(NULL,NULL,adCmdText);
return pRst;
}
catch(_com_error &e)
{
return NULL;
}
}
};
#include<iostream>
using namespace std;
//数据库基本操作类
class DataAccess
{
private:
//申明数据库对象
_ConnectionPtr pConn;
_RecordsetPtr pRst;
_CommandPtr pCmd;
//连接标识符
bool flag;
public:
DataAccess()
{
//初始化连接对象
CoInitialize(NULL);
//创建ADO连接实例
pConn.CreateInstance(__uuidof(Connection));
//创建ADO记录集实例
pRst.CreateInstance(__uuidof(Recordset));
//创建ADO命令实例
pCmd.CreateInstance(__uuidof(Command));
//定义连接字符
pConn->ConnectionString="Provider=SQLOLEDB;Server=(local);Database=Book; uid=sa; pwd=;";
//标识符默认为false
flag=false;
}
~DataAccess()
{
//记录集不为空,释放记录集
if(pRst!=NULL)
{
pRst->Close();
pRst.Release();
}
//Cmd类不为空,释放Cmd
if(pCmd!=NULL)
{
pCmd.Release();
}
//连接不为空,释放连接
if(pConn!=NULL)
{
pConn->Close();
pConn.Release();
}
}
//打开数据库,成功返回TRUE,失败返回FALSE
BOOL openConn()
{
try
{
this->pConn->Open("","","",adConnectUnspecified);
flag=true;
return flag;
}
catch(_com_error &e)
{
return FALSE;
}
}
//执行SQL语句,成功返回_Recordset,失败返回NULL
_RecordsetPtr searchSql(_bstr_t sql)
{
try
{
if(flag==false)
{
this->openConn();
}
pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
pCmd->CommandText=sql;
this->pRst->Open(sql,(IDispatch*)this->pConn,adOpenDynamic,adLockOptimistic,adCmdText);
//this->pRst=pCmd->Execute(NULL,NULL,adCmdText);
return pRst;
}
catch(_com_error &e)
{
return NULL;
}
}
};
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1784682
[收藏到我的网摘] [发送Trackback] kissyougoodby发表于 2007年09月14日 09:54:06