/*********************************************************************
* CopyRight(C), 2012, MoGuangYan. All Rights Reserved
*
* Date Created : 2012.4.16
* FileName : AdoConnectDataBase.cpp
* Description : The Methods
* Mail : moguangyan23@163.com or 1031300437@qq.com
* Author : MoGuangYan
* Contact : 1031300437(QQ)
* Date Modified: 2013.7.19
*
* FileVersion : 1.0.0.0
*
**********************************************************************/
#include "AdoConnectDataBase.h"
void MessageLog_void(char chLog[]);
void MessageLog_void(char chLog[])
{
FILE *fp;
char chFile[100] ="\0";
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime(&rawtime);
sprintf(chFile,"ADO_Log_%4d-%02d-%02d.txt",1900+timeinfo->tm_year, 1+timeinfo->tm_mon,timeinfo->tm_mday);
fp = fopen(chFile,"at+");
if(fp == NULL)
{
return;
}
else
{
fprintf(fp,"[ADO Error]%s\n",chLog);
fclose(fp);
}
return;
}
CAdoConnectDataBase::CAdoConnectDataBase()
{
try
{
::CoInitialize(NULL); //初始化OLE/COM库环境
Connectionstring = "";
m_pConnection = NULL;
m_pRecordset = NULL;
}
catch(_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"构造函数[%s]",(char *)e.Description());
MessageLog_void(chLog);
}
}
CAdoConnectDataBase::~CAdoConnectDataBase()
{
//关闭记录集和连接
try
{
if(m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
::CoUninitialize(); //释放程序占用的COM资源
}
catch(_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"关闭连接异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
}
}
bool CAdoConnectDataBase::InitAdoConnet() // 连接数据库
{
try
{
m_pConnection.CreateInstance("ADODB.Connection");//创建Connection 对象
m_pConnection->Open(Connectionstring,"","",adModeUnknown);
}
catch(_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"连接数据库异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
return false;
}
return true;
}
bool CAdoConnectDataBase::ExecuteSQL(_bstr_t bstrSQL)//执行Insert UpDate Delete
{
try
{
//是否已经连接到数据库
if(m_pConnection == NULL)
{
if(!InitAdoConnet())
{
char ch[100] ="重连数据库失败";
MessageLog_void(ch);
return false;
}
}
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"增删改操作异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
return false;
}
}
_RecordsetPtr& CAdoConnectDataBase::GetRecordSet(_bstr_t bstrSQL) // 执行查询
{
try
{
//连接数据库 如果Connection对象为空则重新连接数据库
if(m_pConnection==NULL)
{
if(!InitAdoConnet())
{
char ch[100] ="重连数据库失败";
MessageLog_void(ch);
m_pRecordset = NULL;
return m_pRecordset;
}
}
//创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//设置静态游标方式
m_pRecordset->CursorType=adOpenStatic;
m_pRecordset->CursorLocation=adUseClient;
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockBatchOptimistic,adCmdText);
}
catch (_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"查询异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
}
return m_pRecordset;
}
void CAdoConnectDataBase::ExecuteStoredPro_Para(_CommandPtr &m_pCmd,char *StoreName,char *ReturnValue) // 执行存储过程,有一个返回值(带参数)
{
try
{
//连接数据库 如果Connection对象为空则重新连接数据库
if(m_pConnection==NULL)
{
if(!InitAdoConnet())
{
char ch[100] ="重连数据库失败";
MessageLog_void(ch);
return;
}
}
m_pConnection->CursorLocation =adUseClient; // 设置游标方式
m_pCmd->ActiveConnection = m_pConnection;
m_pCmd->CommandText = StoreName; //存储过程名称
m_pCmd->CommandType = adCmdStoredProc;
m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
_variant_t VretVal = m_pCmd->Parameters->GetItem("@ReturnValues_VarC")->GetValue(); // 获取返回值(输出参数)
char ch[300] ="\0";
sprintf(ch,"%s",(char*)(_bstr_t)(VretVal));
strcpy(ReturnValue,ch);
}
catch (_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"执行存储过程异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
strcpy(ReturnValue,chLog);
}
}
_RecordsetPtr & CAdoConnectDataBase::ExecuteStoredProcSet(char *StoreName)
{
try
{
_CommandPtr m_pCmd;
m_pCmd.CreateInstance(__uuidof(Command));
m_pRecordset = NULL;
//连接数据库 如果Connection对象为空则重新连接数据库
if(m_pConnection==NULL)
{
if(!InitAdoConnet())
{
char ch[100] ="重连数据库失败";
MessageLog_void(ch);
return m_pRecordset;
}
}
m_pConnection->CursorLocation =adUseClient; // 设置游标方式
//创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//设置静态游标方式
m_pRecordset->CursorType=adOpenStatic;
m_pRecordset->CursorLocation=adUseClient;
m_pCmd->ActiveConnection = m_pConnection;
m_pCmd->CommandText = StoreName; //存储过程名称
m_pCmd->CommandType = adCmdStoredProc;
m_pRecordset = m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
}
catch (_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"执行存储过程异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
}
return m_pRecordset;
}
void CAdoConnectDataBase::ExecuteStoredProc(char *StoreName,char *ReturnValue) // 执行存储过程,有一个返回值(不带参数)
{
try
{
//连接数据库 如果Connection对象为空则重新连接数据库
if(m_pConnection==NULL)
{
if(!InitAdoConnet())
{
char ch[100] ="重连数据库失败";
MessageLog_void(ch);
return;
}
}
_CommandPtr m_pCmd;
m_pCmd.CreateInstance(__uuidof(Command));
m_pConnection->CursorLocation =adUseClient; // 设置游标方式
m_pCmd->ActiveConnection = m_pConnection;
m_pCmd->CommandText = StoreName; //存储过程名称
m_pCmd->CommandType = adCmdStoredProc;
m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
_variant_t VretVal = m_pCmd->Parameters->GetItem("@ReturnValues_VarC")->GetValue(); // 获取返回值(输出参数)
char ch[300] ="\0";
sprintf(ch,"%s",(char*)(_bstr_t)(VretVal));
strcpy(ReturnValue,ch);
}
catch (_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"执行存储过程异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
strcpy(ReturnValue,chLog);
}
}
_RecordsetPtr & CAdoConnectDataBase::ExecuteStoredProcSet_Para(_CommandPtr &m_pCmd,char *StoreName) // 执行存储过程,返回记录集(带参数)
{
try
{
m_pRecordset = NULL;
//连接数据库 如果Connection对象为空则重新连接数据库
if(m_pConnection==NULL)
{
if(!InitAdoConnet())
{
char ch[100] ="重连数据库失败";
MessageLog_void(ch);
return m_pRecordset;
}
}
m_pConnection->CursorLocation =adUseClient; // 设置游标方式
//创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//设置静态游标方式
m_pRecordset->CursorType=adOpenStatic;
m_pRecordset->CursorLocation=adUseClient;
m_pCmd->ActiveConnection = m_pConnection;
m_pCmd->CommandText = StoreName; //存储过程名称
m_pCmd->CommandType = adCmdStoredProc;
m_pRecordset = m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
}
catch (_com_error e)
{
char chLog[300] ="\0";
sprintf(chLog,"执行存储过程异常[%s]",(char *)e.Description());
MessageLog_void(chLog);
}
return m_pRecordset;
}