Archive for 3月 28th, 2012
-
Hello, DAO(C++) World!
Posted on 3月 28th, 2012 by cx20
DAO(C++)
DAO(Data Access Object)は、マイクロソフト社が開発した COM ベースの DBMS 接続用 API である。
ソースコード(VC + DAO3.6 + Jet データベース)
#include <stdio.h> #include <tchar.h> #import "dao360.dll" no_namespace rename("EOF", "daoEOF") int _tmain( int argc, TCHAR* argv[] ) { CoInitialize(NULL); _DBEnginePtr dbe("DAO.DBEngine.36"); DatabasePtr db = dbe->OpenDatabase( _T("hello.mdb") ); RecordsetPtr rs = db->OpenRecordset( _T("SELECT 'Hello, DAO(C++) World!' AS Message") ); while ( !rs->daoEOF ) { _variant_t var = rs->Fields->GetItem( 0L )->Value; _tprintf( _T("%sn"), (LPCTSTR)(_bstr_t)var ); rs->MoveNext(); } rs->Close(); db->Close(); rs = NULL; db = NULL; dbe = NULL; CoUninitialize(); }
ソースコード(VC + DAO12.0 + ACE データベース)
#include <stdio.h> #include <tchar.h> #import "acedao.dll" no_namespace rename("EOF", "aceEOF") int _tmain( int argc, TCHAR* argv[] ) { CoInitialize(NULL); _DBEnginePtr dbe("DAO.DBEngine.120"); DatabasePtr db = dbe->OpenDatabase( _T("hello.accdb") ); RecordsetPtr rs = db->OpenRecordset( _T("SELECT 'Hello, DAO(C++) World!' AS Message") ); while ( !rs->aceEOF ) { _variant_t var = rs->Fields->GetItem( 0L )->Value; _tprintf( _T("%sn"), (LPCTSTR)(_bstr_t)var ); rs->MoveNext(); } rs->Close(); db->Close(); rs = NULL; db = NULL; dbe = NULL; CoUninitialize(); }
ソースコード(VC + DAO3.6 + ODBC + SQL Server)
#include <stdio.h> #include <tchar.h> #import "dao360.dll" no_namespace rename("EOF", "daoEOF") int _tmain( int argc, TCHAR* argv[] ) { CoInitialize(NULL); _DBEnginePtr dbe("DAO.DBEngine.36"); DatabasePtr db = dbe->OpenDatabase( _T(""), FALSE, FALSE, _T("ODBC;Driver={SQL Server};SERVER=(local);DATABASE=master;") _T("UID=sa;PWD=P@assW0rd;") ); RecordsetPtr rs = db->OpenRecordset( _T("SELECT 'Hello, DAO(C++) World!' AS Message") ); while ( !rs->daoEOF ) { _variant_t var = rs->Fields->GetItem( 0L )->Value; _tprintf( _T("%sn"), (LPCTSTR)(_bstr_t)var ); rs->MoveNext(); } rs->Close(); db->Close(); rs = NULL; db = NULL; dbe = NULL; CoUninitialize(); }
ソースコード(VC + DAO3.6 + ODBC + Oracle)
#include <stdio.h> #include <tchar.h> #import "dao360.dll" no_namespace rename("EOF", "daoEOF") int _tmain( int argc, TCHAR* argv[] ) { CoInitialize(NULL); _DBEnginePtr dbe("DAO.DBEngine.36"); DatabasePtr db = dbe->OpenDatabase( _T(""), FALSE, FALSE, _T("ODBC;Driver={Oracle in OraDb11g_home1};") _T("DBQ=ORCL;") _T("UID=scott;PWD=tiger;") ); RecordsetPtr rs = db->OpenRecordset( _T("SELECT 'Hello, DAO(C++) World!' AS Message") ); // パススルークエリの例(データベース側に依存する SQL を実行する場合) //RecordsetPtr rs = db->OpenRecordset( _T("SELECT 'Hello, DAO(C++) World!' AS Message FROM DUAL"), dbOpenSnapshot, dbSQLPassThrough ); while ( !rs->daoEOF ) { _variant_t var = rs->Fields->GetItem( 0L )->Value; _tprintf( _T("%sn"), (LPCTSTR)(_bstr_t)var ); rs->MoveNext(); } rs->Close(); db->Close(); rs = NULL; db = NULL; dbe = NULL; CoUninitialize(); }
コンパイル方法
C:¥> cl hello.cpp
実行結果
Hello, DAO(C++) World!