加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++通过ADO操作Sql Server数据库的代码演示

发布时间:2020-12-16 07:44:24 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 class SQLService{public: SQLService(string procName); ~SQLService(void); void AddPara(string paraName,_variant_t val); void AddParaOut(strin

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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,_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,_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,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,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()),adParamOutput,val);
        m_pCmd->Parameters->Append(pPara);
    }
}
  
void SQLService::ModifyPro(string& newProName)
{
    m_ProcName=newProName;
    m_pCmd->CommandText=_bstr_t(m_ProcName.c_str());
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读