随手写写,有待加强。
YTAdo.h
//**************************************************************************************************************************************************************** // 文件: YTAdo类 // 作者: huanghaifeng // 日期: 20100520 // 作用: 简化ADO调用SQL Server 2000的过程 // // 用法: // 1、初始化COM库 // 2、实例化YTAdo类 // 3、调用YTAdo类对象成员方法initDB // 4、调用addReturnParam/addIntOutParam/addIntInParam/addVCharOutParam/addVCharInParam传参给存储过程,如有return值,return参数必须是第一个传进的参数 // 5、调用execProcNonQuery/execProcQuery执行存储过程,如调用execProcQuery,请用getRecordSet获取记录集 // 6、调用getIntOutParam/getVCharOutParam/getReturnValue获取output参数或return参数,获取这些值无需先执行步骤5 // 7、调用closeConnection和closeRecordset(可选,析构函数会自动调用) // 8、释放COM库 // // 说明: 成员函数getIntOutParam/getVCharOutParam/getReturnValue的addName参数是 // addReturnParam/addIntOutParam/addIntInParam/addVCharOutParam/addVCharInParam传入的name参数 //**************************************************************************************************************************************************************** #pragma once #import "C:/program files/common files/system/ado/msado15.dll" no_namespace rename ("EOF", "adoEOF") class YTAdo { private: _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordSet; _CommandPtr m_pCommand; public: YTAdo(void); ~YTAdo(void); public: //初始化connection并打开 void initConnection(char* ip,char* dbname,char* name,char* pwd); //初始化command void initCommand(); //初始化database void initDB(char* ip,char* dbname,char* name,char* pwd); //关闭connection void closeConnection(); //关闭recordset void closeRecordset(); //添加int传出型参数 void addIntOutParam(char* name,int* ret); //添加int传入型参数 void addIntInParam(char* name,int value); //添加varchar传出型参数 void addVCharOutParam(char* name,unsigned int size,char* ret); //添加varchar传入型参数 void addVCharInParam(char* name,unsigned int size,char* value); //添加用于获取存储过程return值的参数 void addReturnParam(char* name,int* ret); //执行无返回recordset的存储过程 int execProcNonQuery(char* procName); //执行有返回recordset的存储过程 void execProcQuery(char* procName); //返回recordset _RecordsetPtr getRecordSet(); //返回command _CommandPtr getCommand(); //返回某存储过程int传出型参数 int getIntOutParam(char* procName,char* addName); //返回某存储过程varchar传出型参数 _bstr_t getVCharOutParam(char* procName,char* addName); //返回某存储过程return参数 int getReturnValue(char* procName,char* addName); };
YTAdo.cpp
#include "StdAfx.h" #include "YTAdo.h" YTAdo::YTAdo() { m_pConnection=NULL; m_pRecordSet=NULL; m_pCommand=NULL; try { m_pRecordSet.CreateInstance("ADODB.Recordset"); } catch(...) { WriteErrLog("YTAdo: create recordset failed!/n"); } } YTAdo::~YTAdo() { closeRecordset(); closeConnection(); } void YTAdo::initConnection(char* ip,char* dbname,char* name,char* pwd) { try { m_pConnection.CreateInstance(__uuidof(Connection)); char* part1="Driver={SQL Server};Server="; char* part2=";Database="; char* conn; conn=(char*)malloc(strlen(part1) + strlen(ip) + strlen(part2) + strlen(dbname) + 1); memset(conn,0,strlen(part1) + strlen(ip) + strlen(part2) + strlen(dbname) +1); strcat(conn,part1); strcat(conn,ip); strcat(conn,part2); strcat(conn,dbname); m_pConnection->Open(conn, name, pwd, adConnectUnspecified); free(conn); } catch(...) { WriteErrLog("initConnection: [ip=%s] [dbname=%s] [name=%s] [pwd=%s] failed!/n",ip,dbname,name,pwd); } } void YTAdo::initCommand() { try { m_pCommand.CreateInstance(__uuidof(Command)); m_pCommand->ActiveConnection=m_pConnection; m_pCommand->CommandType=adCmdStoredProc; } catch(...) { WriteErrLog("initCommand: create command failed!/n"); } } void YTAdo::initDB(char* ip,char* dbname,char* name,char* pwd) { initConnection(ip,dbname,name,pwd); initCommand(); } void YTAdo::closeConnection() { try { if(m_pConnection && m_pConnection->State) { m_pConnection->Close(); m_pConnection=NULL; } } catch(...) { WriteErrLog("closeConnection: close connection failed!/n"); } } void YTAdo::closeRecordset() { try { if(m_pRecordSet && m_pRecordSet->State) { m_pRecordSet->Close(); m_pRecordSet=NULL; } } catch(...) { WriteErrLog("closeRecordset: close recordset failed!/n"); } } void YTAdo::addReturnParam(char *name, int *ret) { try { _ParameterPtr m_pParam; m_pParam.CreateInstance("ADODB.Parameter"); m_pParam=m_pCommand->CreateParameter(name,adInteger,adParamReturnValue,-1,(_variant_t)ret); m_pCommand->Parameters->Append(m_pParam); } catch(...) { WriteErrLog("addReturnParam: [name=%s] failed!/n",name); } } void YTAdo::addIntInParam(char *name, int value) { try { char aValue[6]; memset(aValue,0,6); itoa(value,aValue,10); _ParameterPtr m_pParam; m_pParam.CreateInstance("ADODB.Parameter"); m_pParam=m_pCommand->CreateParameter(name,adInteger,adParamInput,-1,(_variant_t)aValue); m_pCommand->Parameters->Append(m_pParam); } catch(...) { WriteErrLog("addIntInParam: [name=%s] [value=%d] failed!/n",name,value); } } void YTAdo::addIntOutParam(char *name, int *ret) { try { _ParameterPtr m_pParam; m_pParam.CreateInstance("ADODB.Parameter"); m_pParam=m_pCommand->CreateParameter(name,adInteger,adParamOutput,-1,(_variant_t)ret); m_pCommand->Parameters->Append(m_pParam); } catch(...) { WriteErrLog("addIntOutParam: [name=%s] failed!/n",name); } } void YTAdo::addVCharInParam(char *name, unsigned int size, char *value) { try { _ParameterPtr m_pParam; m_pParam.CreateInstance("ADODB.Parameter"); m_pParam=m_pCommand->CreateParameter(name,adVarChar,adParamInput,size,(_variant_t)value); m_pCommand->Parameters->Append(m_pParam); } catch(...) { WriteErrLog("addVCharInParam: [name=%s] [value=%s] failed!/n",name,value); } } void YTAdo::addVCharOutParam(char *name, unsigned int size, char *ret) { try { _ParameterPtr m_pParam; m_pParam.CreateInstance("ADODB.Parameter"); m_pParam=m_pCommand->CreateParameter(name,adVarChar,adParamOutput,size,(_variant_t)ret); m_pCommand->Parameters->Append(m_pParam); } catch(...) { WriteErrLog("addVCharOutParam: [name=%s] failed!/n",name); } } void YTAdo::execProcQuery(char *procName) { try { m_pCommand->CommandText=_bstr_t(procName); m_pRecordSet=m_pCommand->Execute(NULL,NULL,adCmdStoredProc); } catch(...) { WriteErrLog("execProcQuery: [procName=%s] failed!/n",procName); } } int YTAdo::execProcNonQuery(char *procName) { int ret; try { m_pCommand->CommandText=_bstr_t(procName); ret=m_pCommand->Execute(NULL,NULL,adCmdStoredProc); } catch(...) { ret=0; WriteErrLog("execProcNonQuery: [procName=%s] failed!/n",procName); } return ret; } _RecordsetPtr YTAdo::getRecordSet() { return m_pRecordSet; } _CommandPtr YTAdo::getCommand() { return m_pCommand; } int YTAdo::getIntOutParam(char *procName, char *addName) { int r; try { execProcNonQuery(procName); r=(int)m_pCommand->Parameters->GetItem(addName)->GetValue(); } catch(...) { WriteErrLog("getIntOutParam: [procName=%s] [addName=%s] failed!/n",procName,addName); } return r; } _bstr_t YTAdo::getVCharOutParam(char *procName, char *addName) { _bstr_t s; try { execProcNonQuery(procName); s=_bstr_t(m_pCommand->Parameters->GetItem(addName)->GetValue()); } catch(...) { WriteErrLog("getVCharOutParam: [procName=%s] [addName=%s] failed!/n",procName,addName); } return s; } int YTAdo::getReturnValue(char *procName, char *addName) { int r; try { execProcNonQuery(procName); r=(int)m_pCommand->Parameters->GetItem(addName)->GetValue(); } catch(...) { WriteErrLog("getReturnValue: [procName=%s] [addName=%s] failed!/n",procName,addName); } return r; }