C++通過ADO操作Sql Server數據庫的代碼演示

kdloeki 9年前發布 | 2K 次閱讀 C/C++

C++通過ADO操作Sql Server數據庫的代碼演示

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()); }</pre>

 本文由用戶 kdloeki 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!