|
|
// これは、Oracle 9i の例。昔の Oracle 8 では OIP23.TLB でした。
#import <D:\oracle\ora92\bin\oip9.tlb> \
no_namespace rename("EOF", "EndOfFile")
#include <ole2.h>
#include <stdio.h>
#include <oleauto.h>
#include <conio.h>
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
OraSessionPtr pSession = NULL;
OraDatabasePtr pDb = NULL;
OraDynasetPtr pDyn = NULL;
long OraLongRet = 0;
#define ORA_START \
CoInitialize(NULL); \
try { \
TESTHR(pSession.CreateInstance("OracleInProcServer.XOraSession"));
#define ORA_ERROR \
} \
catch (_com_error &e) {
#define ORA_END \
} \
if ( pDyn != NULL ) { \
pDyn.Release(); \
} \
if ( pDb != NULL ) { \
pDb.Release(); \
} \
if ( pSession != NULL ) { \
pSession.Release(); \
} \
CoUninitialize();
#define ORA_CONNECT(Service,User,Pass) \
{ \
_bstr_t strService(Service); \
_bstr_t strCon; \
strCon = User; \
strCon += "/"; \
strCon += Pass; \
pDb = pSession->GetOpenDatabase( Service, strCon, (LONG)0 ); \
}
#define ORA_QUERY(Query) \
{ \
if ( pDyn != NULL ) { \
pDyn.Release(); \
pDyn == NULL; \
} \
_bstr_t strQuery(Query); \
pDyn = pDb->GetCreateDynaset( strQuery, (LONG)2 ); \
if ( pDyn == NULL ) { \
_com_issue_error(E_FAIL); \
} \
}
#define ORA_QUERY_LOOP \
while( !(pDyn->GetEndOfFile()) ) {
#define ORA_QUERY_END \
pDyn->MoveNext(); \
}
#define ORA_QUERY_GET(Name,szBuff) \
{ \
OraFieldsPtr pFlds; \
OraFieldPtr pFld; \
_variant_t V1; \
V1 = (Name); \
_variant_t V2; \
pFlds = pDyn->GetFields(); \
pFld = pFlds->GetItem( V1 ); \
V2 = pFld->GetValue(); \
if ( V2.vt == VT_NULL ) { \
szBuff[0] = 0x00; \
} \
else { \
_bstr_t B1; \
B1 = V2; \
lstrcpy(szBuff, (LPTSTR)B1 ); \
} \
}
#define ORA_EXECUTE(Command) \
{ \
_bstr_t strCommand(Command); \
OraLongRet = pDb->ExecuteSQL( strCommand ); \
}
| |
|
|
|
|
#include "stdafx.h"
#include "db_oo4o.h"
int main(int argc, char* argv[])
{
ORA_START
ORA_CONNECT("lightbox","study","study")
ORA_QUERY("select * from 社員マスタ")
char szBuff[4096];
ORA_QUERY_LOOP
ORA_QUERY_GET("社員コード",szBuff)
printf( "%s,", szBuff );
ORA_QUERY_GET("氏名",szBuff)
printf( "%s\n", szBuff );
ORA_QUERY_END
ORA_EXECUTE("update 社員マスタ set 氏名 = 'AAA' where 社員コード = '0001'")
ORA_ERROR
printf( "%s\n", e.ErrorMessage() );
_bstr_t strError;
if ( pSession->LastServerErr != 0 ) {
strError.operator = ( pSession->LastServerErrText );
printf( "%s\n", (LPTSTR)strError );
}
if ( pDb != NULL ) {
if ( pDb->LastServerErr != 0 ) {
strError.operator = ( pDb->LastServerErrText );
printf( "%s\n", (LPTSTR)strError );
}
}
ORA_END
return 0;
}
| |
|
|
|