c++讀取sqlserver示例分享
// ReadSqlConsole.cpp: 主項(xiàng)目文件。
#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"); //對(duì)應(yīng)的sql語(yǔ)句
try
{
clog << "正在連接數(shù)據(jù)庫(kù)..." << endl;
m_pConnection.CreateInstance("ADODB.Connection");//創(chuàng)建Connection對(duì)象
//設(shè)置連接字符串,必須是BSTR型或者_(dá)bstr_t類型
_bstr_t strConnect = "Provider=SQLOLEDB; Server=【數(shù)據(jù)庫(kù)實(shí)例】;Database=【數(shù)據(jù)庫(kù)名】; uid=【數(shù)據(jù)庫(kù)用戶】; pwd=【密碼】;";
m_pConnection->Open(strConnect, "", "", adModeUnknown);//服務(wù)器連接
if (m_pConnection == NULL)
{
std::cerr << "Lind data ERROR!\n";
}
m_pRecordset.CreateInstance(__uuidof(Recordset));//創(chuàng)建記錄集對(duì)象
//取得表中的記錄
m_pRecordset->Open(bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
_variant_t PactNumber, OrigCount;//對(duì)應(yīng)庫(kù)中的字段
cout << "--------------合同清單--------------" << endl;
while (!m_pRecordset->EndOfFile)
{
PactNumber = m_pRecordset->GetCollect("Pactnumber");
OrigCount = m_pRecordset->GetCollect("Origcount");
cout << "合同號(hào):" << setw(10) << (char*)(_bstr_t)PactNumber;
cout << " 簽單額:" << setw(7) << (char*)(_bstr_t)OrigCount << endl;
m_pRecordset->MoveNext();//下一條記錄
}
m_pRecordset->Close();//關(guān)閉記錄集
}
catch (_com_error e)//捕捉異常
{
cerr << "\nERROR:" << (char*)e.Description();
}
if (m_pConnection->State)
{
m_pConnection->Close();
}
::CoUninitialize();
Console::ReadLine();
return 0;
}
相關(guān)文章
簡(jiǎn)要對(duì)比C語(yǔ)言中的setgid()函數(shù)和setregid()函數(shù)
這篇文章主要介紹了C語(yǔ)言中的setgid()函數(shù)和setregid()函數(shù)的簡(jiǎn)要對(duì)比,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08使用c++實(shí)現(xiàn)OpenCV繪制圓端矩形
這篇文章主要介紹了使用c++實(shí)現(xiàn)OpenCV繪制圓端矩形,其中著重的講解了OpenCV使用過(guò)程中需要注意的一些小細(xì)節(jié),避免浪費(fèi)大家在開發(fā)過(guò)程中浪費(fèi)多余的時(shí)間2021-08-08淺談Qt實(shí)現(xiàn)HTTP的Get/Post請(qǐng)求
本文主要介紹了淺談Qt實(shí)現(xiàn)HTTP的Get/Post請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05c++中std::hash以及萬(wàn)能hash的使用方式
這篇文章主要介紹了c++中std::hash以及萬(wàn)能hash的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08C++實(shí)現(xiàn)LeetCode(228.總結(jié)區(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(228.總結(jié)區(qū)間),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07