C++中利用ADO方式进行数据库操作的类,主要用来执行存储过程
来源:互联网 发布:北京大学网络教育平台 编辑:程序博客网 时间:2024/06/11 19:32
class SQLService
{
public:
SQLService(string procName);
~SQLService(void);
void AddPara(string paraName,_variant_t val);
void AddParaOut(string paraName,_variant_t& val);
void Insert();
void Delete();
void Update();
void RefreshCmdPara();
_RecordsetPtr& Query();
void ModifyPro(string& newProName);
private:
void ExitConnect();
void OnInitADOConn();
bool ExecuteProc();
private:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCmd;
string m_ProcName;
};
#include "StdAfx.h"
#include "SQLService.h"
#import "c:program filescommon filessystemadomsado15.dll" no_namespace, rename("EOF", "adoEOF")
SQLService::SQLService(string procName)
{
m_pCmd=NULL;
m_pConnection=NULL;
m_pRecordset=NULL;
m_ProcName=procName;
}
SQLService::~SQLService(void)
{
ExitConnect();
}
void SQLService::OnInitADOConn()
{
::CoInitialize(NULL);
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance("ADODB.Connection");//
if(SUCCEEDED(hr))
{
_bstr_t strConnect="Provider=SQLOLEDB;Server=.;Database=SURF;UID=sa;PWD=123;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
m_pConnection->CursorLocation=adUseClient;
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("打开数据库失败"),MB_OK);
m_pConnection->Close();
m_pConnection.Release();
m_pConnection=NULL;
return ;
}
try
{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
if(SUCCEEDED(hr))
{
m_pRecordset->CursorType=adOpenKeyset;
m_pRecordset->LockType=adLockOptimistic;
m_pRecordset->PutActiveConnection(m_pConnection.GetInterfacePtr());
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("记录集创建失败"),MB_OK);
m_pConnection->Close();
m_pRecordset->Close();
m_pConnection.Release();
m_pRecordset.Release();
m_pConnection=NULL;
m_pRecordset=NULL;
return ;
}
try
{
hr=m_pCmd.CreateInstance(_uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCmd->ActiveConnection=m_pConnection;
m_pCmd->CommandType=adCmdStoredProc;
m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("命令创建失败"),MB_OK);
m_pConnection->Close();
m_pRecordset->Close();
m_pConnection->Release();
m_pRecordset->Release();
m_pCmd.Release();
m_pConnection=NULL;
m_pRecordset=NULL;
m_pCmd=NULL;
return ;
}
}
bool SQLService::ExecuteProc()//执行增删改操作
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
}
return true;
}
void SQLService::Insert()//执行增 操作
{
ExecuteProc();
}
void SQLService::Delete()//执行 删 操作
{
ExecuteProc();
}
void SQLService::Update()//执行 改 操作
{
ExecuteProc();
}
_RecordsetPtr& SQLService::Query()
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
m_pRecordset=m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
}
return m_pRecordset;
}
void SQLService::RefreshCmdPara()
{
if(m_pCmd!=NULL)
{
m_pCmd.Release();
m_pCmd=NULL;
}
try
{
HRESULT hr=m_pCmd.CreateInstance(_uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCmd->ActiveConnection=m_pConnection;
m_pCmd->CommandType=adCmdStoredProc;
m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("命令创建失败"),MB_OK);
m_pConnection->Close();
m_pRecordset->Close();
m_pConnection->Release();
m_pRecordset->Release();
m_pCmd.Release();
m_pConnection=NULL;
m_pRecordset=NULL;
m_pCmd=NULL;
return ;
}
}
void SQLService::ExitConnect()
{
if(m_pRecordset!=NULL&&m_pRecordset->State)
{
m_pRecordset->Close();
m_pRecordset.Release();
m_pRecordset=NULL;
}
if(m_pConnection!=NULL&&m_pConnection->State)
{
m_pConnection->Close();
m_pConnection.Release();
m_pConnection=NULL;
}
if(m_pCmd!=NULL)
{
m_pCmd.Release();
m_pCmd=NULL;
}
::CoUninitialize();
}
void SQLService::AddPara(string paraName,_variant_t val)
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
_ParameterPtr pPara=m_pCmd->CreateParameter(_bstr_t(paraName.c_str()),adBSTR,adParamInput,255,val);
m_pCmd->Parameters->Append(pPara);
}
}
void SQLService::AddParaOut(string paraName,_variant_t& val)
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
_ParameterPtr pPara=m_pCmd->CreateParameter(_bstr_t(paraName.c_str()),adBSTR,adParamOutput,255,val);
m_pCmd->Parameters->Append(pPara);
}
}
void SQLService::ModifyPro(string& newProName)
{
m_ProcName=newProName;
m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}
{
public:
SQLService(string procName);
~SQLService(void);
void AddPara(string paraName,_variant_t val);
void AddParaOut(string paraName,_variant_t& val);
void Insert();
void Delete();
void Update();
void RefreshCmdPara();
_RecordsetPtr& Query();
void ModifyPro(string& newProName);
private:
void ExitConnect();
void OnInitADOConn();
bool ExecuteProc();
private:
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
_CommandPtr m_pCmd;
string m_ProcName;
};
#include "StdAfx.h"
#include "SQLService.h"
#import "c:program filescommon filessystemadomsado15.dll" no_namespace, rename("EOF", "adoEOF")
SQLService::SQLService(string procName)
{
m_pCmd=NULL;
m_pConnection=NULL;
m_pRecordset=NULL;
m_ProcName=procName;
}
SQLService::~SQLService(void)
{
ExitConnect();
}
void SQLService::OnInitADOConn()
{
::CoInitialize(NULL);
HRESULT hr;
try
{
hr=m_pConnection.CreateInstance("ADODB.Connection");//
if(SUCCEEDED(hr))
{
_bstr_t strConnect="Provider=SQLOLEDB;Server=.;Database=SURF;UID=sa;PWD=123;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
m_pConnection->CursorLocation=adUseClient;
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("打开数据库失败"),MB_OK);
m_pConnection->Close();
m_pConnection.Release();
m_pConnection=NULL;
return ;
}
try
{
hr=m_pRecordset.CreateInstance("ADODB.Recordset");
if(SUCCEEDED(hr))
{
m_pRecordset->CursorType=adOpenKeyset;
m_pRecordset->LockType=adLockOptimistic;
m_pRecordset->PutActiveConnection(m_pConnection.GetInterfacePtr());
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("记录集创建失败"),MB_OK);
m_pConnection->Close();
m_pRecordset->Close();
m_pConnection.Release();
m_pRecordset.Release();
m_pConnection=NULL;
m_pRecordset=NULL;
return ;
}
try
{
hr=m_pCmd.CreateInstance(_uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCmd->ActiveConnection=m_pConnection;
m_pCmd->CommandType=adCmdStoredProc;
m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("命令创建失败"),MB_OK);
m_pConnection->Close();
m_pRecordset->Close();
m_pConnection->Release();
m_pRecordset->Release();
m_pCmd.Release();
m_pConnection=NULL;
m_pRecordset=NULL;
m_pCmd=NULL;
return ;
}
}
bool SQLService::ExecuteProc()//执行增删改操作
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
}
return true;
}
void SQLService::Insert()//执行增 操作
{
ExecuteProc();
}
void SQLService::Delete()//执行 删 操作
{
ExecuteProc();
}
void SQLService::Update()//执行 改 操作
{
ExecuteProc();
}
_RecordsetPtr& SQLService::Query()
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
m_pRecordset=m_pCmd->Execute(NULL,NULL,adCmdStoredProc);
}
return m_pRecordset;
}
void SQLService::RefreshCmdPara()
{
if(m_pCmd!=NULL)
{
m_pCmd.Release();
m_pCmd=NULL;
}
try
{
HRESULT hr=m_pCmd.CreateInstance(_uuidof(Command));
if(SUCCEEDED(hr))
{
m_pCmd->ActiveConnection=m_pConnection;
m_pCmd->CommandType=adCmdStoredProc;
m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}
}
catch(_com_error& e)
{
MessageBox(NULL,(LPCWSTR)e.Description(),_T("命令创建失败"),MB_OK);
m_pConnection->Close();
m_pRecordset->Close();
m_pConnection->Release();
m_pRecordset->Release();
m_pCmd.Release();
m_pConnection=NULL;
m_pRecordset=NULL;
m_pCmd=NULL;
return ;
}
}
void SQLService::ExitConnect()
{
if(m_pRecordset!=NULL&&m_pRecordset->State)
{
m_pRecordset->Close();
m_pRecordset.Release();
m_pRecordset=NULL;
}
if(m_pConnection!=NULL&&m_pConnection->State)
{
m_pConnection->Close();
m_pConnection.Release();
m_pConnection=NULL;
}
if(m_pCmd!=NULL)
{
m_pCmd.Release();
m_pCmd=NULL;
}
::CoUninitialize();
}
void SQLService::AddPara(string paraName,_variant_t val)
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
_ParameterPtr pPara=m_pCmd->CreateParameter(_bstr_t(paraName.c_str()),adBSTR,adParamInput,255,val);
m_pCmd->Parameters->Append(pPara);
}
}
void SQLService::AddParaOut(string paraName,_variant_t& val)
{
if(m_pConnection==NULL)
OnInitADOConn();
if(m_pCmd!=NULL)
{
_ParameterPtr pPara=m_pCmd->CreateParameter(_bstr_t(paraName.c_str()),adBSTR,adParamOutput,255,val);
m_pCmd->Parameters->Append(pPara);
}
}
void SQLService::ModifyPro(string& newProName)
{
m_ProcName=newProName;
m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}
- C++中利用ADO方式进行数据库操作的类,主要用来执行存储过程
- MFC中利用ADO对数据库进行简单操作的实例
- ado 利用智能指针进行数据库操作
- 用来备份数据库的存储过程
- ADO执行存储过程的问题
- ado方式操作数据库类
- ado方式操作数据库类
- ado方式操作数据库类
- ado方式操作数据库类
- VC中利用ADO共同实现数据库的操作
- VC中利用ADO共同实现数据库的操作
- VC中使用ADO进行数据库操作
- VC中使用ADO进行数据库操作
- VC中使用ADO进行数据库操作
- SpringBoot集成MyBatis(主要用来方便的进行自定义一些sql查询,主要的简单的数据库操作还是依赖于自身提供的JPA)
- ORACLE中UTL_FILE ,DBMS-SQL进行数据库与文件互操作的存储过程
- ADO对Sql的存储过程操作
- MFC 中ADO方式操作ACCESS数据库
- 詹姆斯·高斯林-JAVA之父
- ubuntu下的异步网络通信实例
- Windows Socket Client端程序
- linux awk 内置函数实例
- 类模板 Mat_<float> sal
- C++中利用ADO方式进行数据库操作的类,主要用来执行存储过程
- Django入门
- IOS自定义view------checkbox
- 【流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
- cheer up2
- 数组赋值给vector的快速方法
- 输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字 时间复杂度O(NlogN)
- 邮件发送
- AVR I/O口寄存器说明