• c++连接mssql


    # include <iostream>
    //# include "stdafx.h"
    # include <iomanip>
    # import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
    int main()
    {
        ::CoInitialize(NULL);
    
        try
        {
            _ConnectionPtr pConn("ADODB.Connection");
            _RecordsetPtr m_pRecordset("ADODB.Recordset");
            //连接字符串,此处为MSSQL的连接字符串,其他数据库的摆渡一下就知道
            _bstr_t strConnect="Provider=sqloledb;Network Library=DBMSSOCN;Initial Catalog=WDBuyDB;User ID=admin;Password=admin$192;Data Source=192.168.2.199,1433";
            //步骤1--打开连接
            pConn->Open(strConnect,"","",adModeUnknown);
            //步骤2--创建命令
            //步骤3--执行命令
            m_pRecordset=pConn->Execute("SELECT * FROM [WDBuyDB].[dbo].[Pro_ProductAttribute]",NULL,adCmdText);
            //步骤4--操作数据
            //操作数据可以选择其它的删除,修改,插入等这里只是读出数据库中表的数据
            _variant_t ProductAttributeKey,AttributeName,AttributeValue;
            std::cout<<"ProductAttributeKey                      AttributeName             AttributeValue";
            //std::cout<<"n---------------------n";
            while(!m_pRecordset->EndOfFile)
            {
                ProductAttributeKey=m_pRecordset->GetCollect(_variant_t((long)0));
                AttributeName=m_pRecordset->GetCollect(_variant_t((long)1));
                AttributeValue=m_pRecordset->GetCollect(_variant_t((long)2));
                //给字段编号或者是字段名
                //id=m_pRecordset->GetCollect("ProductAttributeKey");
                //name=m_pRecordset->GetCollect("AttributeName");
    
                if(ProductAttributeKey.vt!=VT_NULL&&AttributeName.vt!=VT_NULL)
                {
                    printf("\n");
                    //std::cout.setf(ios::left);
                    std::cout<<std::setw(18)<<(char*)(_bstr_t)ProductAttributeKey;
                    std::cout<<std::setw(10)<<(char*)(_bstr_t)AttributeName;
                    std::cout<<std::setw(20)<<(char*)(_bstr_t)AttributeValue;
                }
    
                //移动到下一条记录
                m_pRecordset->MoveNext();
    
                //关闭记录集
                m_pRecordset->Close();
            }
        }
        catch(_com_error e)
        {
            //显示错误信息
            //std::cerr << "This is a test message\n";
            std::cerr << (char*)e.Description();
            //std::cerr<<"nERROR:"<<(char*)e.Description();
        }
        getchar();
        ::CoUninitialize();
        return 0;
    
        //测试部分
        /*
        std::cout << "Hello, world!" << std::endl;
        getchar();
        return 0;
        */
    }
  • 相关阅读:
    MYSQL常用DDL
    MYSQL字符串与数字比较出现的异常
    目 录
    动态规划
    初级排序和高级排序
    ACMer计划2(非原创)
    1、常用C++STL集合
    1.2常见C++STL 映射表
    「CEOI2021」 Newspapers 题解 翻译
    Border Theory 学习笔记
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/2688681.html
Copyright © 2020-2023  润新知