ReadSqlConsole.cpp: Master project file.
#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);
Type is defined in 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"); The corresponding SQL statement
Try
{
Clog << "Connecting to Database ..." << Endl;
M_pconnection.createinstance ("ADODB.") Connection ");//Create Connection Object
Set connection string, must be BSTR or _bstr_t type
_bstr_t strconnect = "provider=sqloledb; Server= "database instance";D atabase= "database name"; uid= "Database User"; pwd= "password"; ";
M_pconnection->open (StrConnect, "", "", admodeunknown);/server Connection
if (m_pconnection = NULL)
{
Std::cerr << "Lind data error!\n";
}
M_precordset.createinstance (__uuidof (Recordset));//Create Recordset object
Get records in a table
M_precordset->open (Bstrsql, M_pconnection.getinterfaceptr (), adopendynamic, adLockOptimistic, AdCmdText);
_variant_t Pactnumber, origcount;//the field in the corresponding library
cout << "--------------contract list--------------" << Endl;
while (!m_precordset->endoffile)
{
pactnumber = m_ Precordset->getcollect ("Pactnumber");
origcount = M_precordset->getcollect (" Origcount ");
cout << Contract No.: "<< SETW (+) < < (char*) (_bstr_t) Pactnumber;
cout << " signing Amount:" << SETW (7) << (char*) (_bstr_t) Origcount << Endl;
M_precordset->movenext ()//Next record
}
M_precordset->close ()//Close Recordset
}
catch (_com_error E)//catch exception
{
Cerr << "\nerror:" << (char*) e.description ();
}
if (m_pconnection->state)
{
M_pconnection->close ();
}
:: CoUninitialize ();
Console::readline ();
return 0;
}