C++ADO读写SqlServer函数封装

因为是ADO连接,所以对SqlServer的版本是通用的,增删改查,加到项目里直接就可以用

hpp

#include <Windows.h>
#include <string>
#include <iostream>
#include <sstream>
#include <list>
#include <algorithm>
#include <afxinet.h>
#include <afxwin.h>
#include <io.h>
#include <stdio.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","EndOfFile")

using namespace std;

struct param_struct
{
    string param_name;
    string param_value;
};
typedef list<param_struct> LISTSTR;



    /**
    * @brief 连接数据库
    * @param[in] connect_str  ip地址,数据库名,用户,密码
    * @return void
    */
    _ConnectionPtr connect_database(CString connect_str);


    /**
    * @brief 执行sql语句
    * @param[in] sql_str  sql语句
    * @param[in] connect_str  ip地址,数据库名,用户,密码
    * @return void
    */
    void execute_sql(CString sql_str,CString connect_str);


    /**
    * @brief 执行sql语句
    * @param[in] sql_str  sql语句
    * @param[in] connect_str  ip地址,数据库名,用户,密码
    * @return void
    */
    LISTSTR select_data(CString sql_str,CString connect_str);

 

cpp

//------------------------------------------------------------------------------
// 连接数据库
//------------------------------------------------------------------------------
_ConnectionPtr CEdgeArtCreateDBTableDlg::connect_database(CString connect_str)
{
    ::CoInitialize(NULL);//初始化OLE/COM库环境,为访问ADO接口做准备
    _ConnectionPtr m_pConnection("ADODB.Connection");    
    m_pConnection.CreateInstance("ADODB.Connection");
    _bstr_t strConnect=connect_str;
    m_pConnection->Open(strConnect,"","",adModeUnknown);
    if (m_pConnection==NULL)
    {
        cerr<<"Lind data ERROR!\n";
    }
    return m_pConnection;
}


//------------------------------------------------------------------------------
// 执行sql语句
//------------------------------------------------------------------------------
void CEdgeArtCreateDBTableDlg::execute_sql(CString sql_str,CString connect_str)
{
    _ConnectionPtr m_pConnection("ADODB.Connection");
    _RecordsetPtr m_pRecordset("ADODB.Recordset");
    m_pConnection = connect_database(connect_str);

    m_pRecordset.CreateInstance(_uuidof(Recordset));
    _bstr_t bstrSQL(sql_str);
    m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
    //m_pRecordset->Close();
}


//------------------------------------------------------------------------------
// 查询sql语句
//------------------------------------------------------------------------------
LISTSTR CEdgeArtCreateDBTableDlg::select_data(CString sql_str,CString connect_str)
{
    _ConnectionPtr m_pConnection("ADODB.Connection");
    _RecordsetPtr m_pRecordset("ADODB.Recordset");
    m_pConnection = connect_database(connect_str);

    LISTSTR data_list;
    m_pRecordset.CreateInstance(_uuidof(Recordset));
    _bstr_t bstrSQL(sql_str);
    m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);


    int param_count = m_pRecordset->GetFields()->Count;
    param_struct params;

    while (!m_pRecordset->EndOfFile)
    {
        for(int i=0;i<param_count;i++)
        {
            string code_name;
            _variant_t vt((long)i);
            params.param_name = m_pRecordset->GetFields()->Item[vt]->Name;
            _variant_t param_value_v;
            param_value_v = m_pRecordset->GetCollect(params.param_name.c_str());
            if(param_value_v.vt==VT_NULL)
            {
                params.param_value="";
            }
            else
            {
                params.param_value  = (char*)(_bstr_t)param_value_v;
            }
            data_list.push_back(params);
        }
        m_pRecordset->MoveNext();
    }
    m_pRecordset->Close();
    return data_list;
}

 

程序员阿飞

2021年5月9日

C++ADO读写SqlServer函数封装

上一篇:06 Spark SQL 及其DataFrame的基本操作


下一篇:MySQL(一)深入理解MySQL索引底层数据结构与算法