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

本文由美高梅网址发布于关于美高梅,转载请注明出处:Server数据库的代码演示

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文