adoConn类

2019-03-19  本文已影响0人  ccc1111

adoConn.h

#pragma once
class adoConn
{
public:
    _ConnectionPtr m_pConn;
    _RecordsetPtr m_pRset;
    bool m_bActived;
    _RecordsetPtr ExecuteSQL(CString vSQL);
    adoConn();
    ~adoConn();
};

adoConn.cpp

#include "stdafx.h"
#include "adoConn.h"


adoConn::adoConn()
{
    m_bActived = false;
    _bstr_t bconn = _T("Provider=sqloledb;Data Source=DESKTOP-SBR2279;Initial Catalog=c;User ID=sa;Password=12345678");
    try
    {
        m_pConn.CreateInstance(__uuidof(Connection));
        m_pConn->ConnectionString = bconn;
        m_pConn->ConnectionTimeout = 20;
        m_pConn->Open("", "", "", adConnectUnspecified);
        m_pRset.CreateInstance(__uuidof(Recordset));
        AfxMessageBox(_T("连接数据库成功"));
    }
    catch (_com_error *e)
    {
        AfxMessageBox(_T("连接数据库出错"));
    }
}

_RecordsetPtr adoConn::ExecuteSQL(CString vSQL)
{
    try
    {
        if (m_bActived)
        {
            m_pRset->Close();
            m_bActived = false;
        }
        m_pRset->Open(_variant_t(vSQL), m_pConn.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
        m_bActived = true;
        return m_pRset;
    }
    catch(_com_error *e)
    {
        AfxMessageBox(_T("执行SQL数据库出错"));
    }
}


adoConn::~adoConn()
{
}

上一篇下一篇

猜你喜欢

热点阅读