ADO.cpp 예제.
#include "stdafx.h"
#import "C:\Program Files\Common Files\System\ado\msado15.dll"
no_namespace rename("EOF", "ADOEOF")
#include "stdio.h"
#include "io.h"
int main(int argc, char* argv[])
{
CoInitialize(NULL);
_ConnectionPtr pConn;
_RecordsetPtr pRs;
pConn.CreateInstance("ADODB.Connection");
pRs.CreateInstance("ADODB.Recordset");
// SQL, DSN의 경우에는 ODBC DSN 설정이 되어 있는 경우에 사용.
아니면, 아래처럼 동적으로 연결.
//pConn->Open("DSN=test","_system","sys",adConnectUnspecified);
pConn->Open("DRIVER={InterSystems
ODBC};SERVER=localhost;PORT=1972;DATABASE=SAMPLES;UID=_system;PWD=sys","","",NULL);
pRs->Open("select Name,Age from Sample.Person where
Age<40", pConn.GetInterfacePtr(), adOpenForwardOnly,
adLockReadOnly, adCmdText);
// Get the value
_variant_t nameT;
_variant_t ageT;
TCHAR name[25];
TCHAR age [10];
short i;
while ( !pRs->ADOEOF ) {
//------- Name
i=0;
nameT = pRs->GetCollect(i);
nameT.ChangeType(VT_BSTR);
lstrcpyn(name,(_bstr_t)nameT,20); // name[25]
//------- Age
i=1;
ageT = pRs->GetCollect(i);
ageT.ChangeType(VT_BSTR);
lstrcpyn(age,(_bstr_t)ageT,10); // age[10]
//------- Display
printf("%20s %s \n",name,age);
//------- Next
pRs->MoveNext();
}
pRs->Close();
return 0;
}
|