C++ ADO データーベースアクセス

  db.h



  
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
    no_namespace rename("EOF", "EndOfFile")

inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};

_ConnectionPtr pCn = NULL;
_RecordsetPtr pRs = NULL;

void PrintProviderError(_ConnectionPtr pConnection)
{
	ErrorPtr  pErr = NULL;

	if( (pConnection->Errors->Count) > 0)
	{
		printf("ProviderError\n");
		long nCount = pConnection->Errors->Count;
		for(long i = 0; i < nCount; i++)
		{
			pErr = pConnection->Errors->GetItem(i);
			printf("\tError number: %x\n", pErr->Number );
			printf("\tDescription: %s\n", (LPTSTR)pErr->Description);
		}
	}
}

void PrintComError(_com_error &e)
{
	_bstr_t bstrSource(e.Source());
	_bstr_t bstrDescription(e.Description());

	// Print Com errors.
	printf("ComError\n");
	printf("\tCode = %08lx\n", e.Error());
	printf("\tCode meaning = %s\n", e.ErrorMessage());
	printf("\tSource = %s\n", (LPCSTR) bstrSource);
	printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}

#define ADO_MAIN(constring) \
int main( ) \
{ \
	CoInitialize(NULL); \
	try { \
		TESTHR(pCn.CreateInstance(__uuidof(Connection))); \
		_bstr_t ConString; \
		ConString.operator = ( constring ); \
		printf( "接続文字列は[%s]です\n", (LPTSTR)ConString ); \
		pCn->Open(ConString, "", "", adConnectUnspecified); \
		TESTHR(pRs.CreateInstance(__uuidof(Recordset)));

#define ADO_END pRs.Release(); pCn->Close(); pCn.Release(); } catch (_com_error &e) \
	{ \
		PrintProviderError(pCn); \
		PrintComError(e); \
	} \
	CoUninitialize(); \
	return 0; \
}

#define ADO_QUERY(Query) \
	pRs->Open( \
		Query, \
		_variant_t((IDispatch *)pCn,true), \
		adOpenKeyset, \
		adLockOptimistic, \
		adCmdText);

#define QUERY_LOOP \
	while( !(pRs->EndOfFile) ) {

#define QUERY_END \
		pRs->MoveNext(); \
	}

#define QUERY_GET(Name,szBuff) \
{ \
	_variant_t V1; \
	_variant_t V2; \
	V1 = Name; \
	V2 = pRs->Fields->GetItem(Name)->Value; \
	if ( V2.vt == VT_NULL ) { \
		szBuff[0] = 0x00; \
	} \
	else { \
		_bstr_t B1; \
		B1 = V2; \
		lstrcpy(szBuff, (LPTSTR)B1 ); \
	} \
}

#define ADO_EXECUTE(Command) \
	pCn->Execute(Command, NULL, adExecuteNoRecords);

#define ADO_CONNECT(constring) \
{ \
		pCn->Close(); \
		_bstr_t ConString; \
		ConString.operator = ( constring ); \
		printf( "接続文字列は[%s]です\n", (LPTSTR)ConString ); \
		pCn->Open(ConString, "", "", adConnectUnspecified); \
}

  



  データ参照



  
#include "stdafx.h"
#include "db.h"

char *ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\Documents and Settings\\lightbox\\My Documents\\販売管理B.mdb;";

ADO_MAIN(ConnectionString)

	char szBuff[4096];

	_bstr_t BBuff;
	_variant_t VBuff;

	VBuff = "select * from 商品マスタ";

	ADO_QUERY(VBuff)

	QUERY_LOOP

		QUERY_GET("商品コード",szBuff)
		printf( "%s,", szBuff );
		QUERY_GET("商品名",szBuff)
		printf( "%s\n", szBuff );

	QUERY_END

ADO_END
  



  フィールド数と番号でデータを取得

stdafx.h
  
#include <afx.h>
  
※ プロジェクト設定で 「MFC のスタティックライブラリを使用」 に設定する

  
#include "stdafx.h"
#include "db.h"

char *ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\Documents and Settings\\lightbox\\My Documents\\販売管理B.mdb;";

ADO_MAIN(ConnectionString)

	CString strBuffer;
	LPTSTR szBuff = strBuffer.GetBufferSetLength( 4096 );
	CString strLine;

	_bstr_t BBuff;
	_variant_t VBuff;

	VBuff = "select * from 商品マスタ";

	ADO_QUERY(VBuff)

	int nField;
	nField = pRs->Fields->Count;
	printf( "フィールド数 = %d\n", nField );

	int i;
	QUERY_LOOP

		QUERY_GET(((LONG)0),szBuff)
		strLine = szBuff;

		for( i = 1; i < nField; i++ ) {
			QUERY_GET(((LONG)i),szBuff)
			strLine += ",";
			strLine += szBuff;
		}
		strLine += "\n";

		printf( "%s", (LPCTSTR)strLine );

	QUERY_END

	strBuffer.ReleaseBuffer();

ADO_END
  



  テーブル一覧

  
#include "stdafx.h"
#include "db.h"

char *ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\Documents and Settings\\lightbox\\My Documents\\販売管理B.mdb;";

ADO_MAIN(ConnectionString)

	_bstr_t BBuff;
	_variant_t VBuff;

	// テーブル一覧の開始
	_variant_t vValue;
	_bstr_t StringBuffer;
	int nIndex;

	pRs = pCn->OpenSchema( adSchemaTables );

	while( !(pRs->EndOfFile) ) {

			vValue = pRs->Fields->GetItem("TABLE_NAME")->Value;
			StringBuffer.operator = (vValue);
			printf( "%s\n", (LPTSTR)StringBuffer );
			pRs->MoveNext();
			nIndex++;

	}

	pRs->Close();

ADO_END
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加




SQLの窓  天気  IT用語辞典
Yahoo!ニュース  マルチ辞書
PHP マニュアル  Google URL短縮 


フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ