// ReadSqlConsole.cpp: 主項目文件。#include "stdafx.h"#include #include#include//for setw()#include"windows.h"#import "C:\Program Files\Common Files\System\...
// 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; }