亲宝软件园·资讯

展开

c++读取sqlserver c++读取sqlserver示例分享

人气:5
想了解c++读取sqlserver示例讲解的相关内容吗,在本文为您仔细讲解c++读取sqlserver的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c++,sqlserver,下面大家一起来学习吧。

复制代码 代码如下:

// ReadSqlConsole.cpp: 主项目文件。

#include "stdafx.h"
#include <iostream>
#include<iostream>
#include<iomanip>//for setw()
#include"windows.h"

#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","EndOfFile")

using namespace System;
using namespace std;

int main(array<System::String ^> ^args)
{
    ::CoInitialize(NULL);

    //类型在msado15.dll中已定义
    _RecordsetPtr m_pRecordset("ADODB.Recordset");
    _ConnectionPtr m_pConnection("ADODB.Connection");

    _bstr_t bstrSQL("select * from er_order where pactnumber like '13010000%' order by pactnumber"); //对应的sql语句

    try
    {
        clog << "正在连接数据库..." << endl;
        m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
        //设置连接字符串,必须是BSTR型或者_bstr_t类型
        _bstr_t strConnect = "Provider=SQLOLEDB; Server=【数据库实例】;Database=【数据库名】; uid=【数据库用户】; pwd=【密码】;";
        m_pConnection->Open(strConnect, "", "", adModeUnknown);//服务器连接

        if (m_pConnection == NULL)
        {
            std::cerr << "Lind data ERROR!\n";
        }

        m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集对象

        //取得表中的记录
        m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);

        _variant_t PactNumber, OrigCount;//对应库中的字段

        cout << "--------------合同清单--------------" << endl;
        while (!m_pRecordset->EndOfFile)
        {
            PactNumber = m_pRecordset->GetCollect("Pactnumber");
            OrigCount = m_pRecordset->GetCollect("Origcount");
            cout << "合同号:" << setw(10) << (char*)(_bstr_t)PactNumber;
            cout << "   签单额:" << setw(7) << (char*)(_bstr_t)OrigCount << endl;

            m_pRecordset->MoveNext();//下一条记录
        }
        m_pRecordset->Close();//关闭记录集
    }

    catch (_com_error e)//捕捉异常
    {
        cerr << "\nERROR:" << (char*)e.Description();
    }

    if (m_pConnection->State)
    {
        m_pConnection->Close();
    }

    ::CoUninitialize();

    Console::ReadLine();

    return 0;
}

加载全部内容

相关教程
猜你喜欢
用户评论