|
#include "stdafx.h"
#include "resource.h"
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
// 接続文字列
char *ConnectionString =
"Provider=MSDASQL"
";DSN=MySQL"
";DATABASE=lightbox"
";UID=root"
";PWD="
";";
// エラー処理用
ErrorPtr pErr = NULL;
void QuerySample( )
{
printf("QuerySample\n");
_ConnectionPtr pCn;
_RecordsetPtr pRs;
_bstr_t ConString;
_bstr_t BBuff;
_variant_t VBuff;
try {
// 接続インスタンス作成
TESTHR(pCn.CreateInstance(__uuidof(Connection)));
ConString.operator = ( ConnectionString );
printf( "接続文字列は[%s]です\n", (LPTSTR)ConString );
// 接続
pCn->Open(ConString, "", "", adConnectUnspecified);
// レコードセットインスタンス作成
TESTHR(pRs.CreateInstance(__uuidof(Recordset)));
// SQL
VBuff.operator = ("select * from 商品分類マスタ");
// レコードセット取得
pRs->Open(
VBuff,
_variant_t((IDispatch *)pCn,true),
adOpenKeyset,
adLockOptimistic,
adCmdText
);
// フィールドデータ取得
while( !(pRs->EndOfFile) ) {
VBuff = pRs->Fields->GetItem("商品分類")->Value;
BBuff.operator = (VBuff);
printf( "商品分類 = %s,", (LPTSTR)BBuff );
VBuff = pRs->Fields->GetItem("作成日")->Value;
BBuff.operator = (VBuff);
printf( "作成日 = %s\n", (LPTSTR)BBuff );
// 次のレコード
pRs->MoveNext();
}
// 終了処理
pRs->Close();
pCn->Close();
}
catch ( _com_error &e ) {
if( (pCn->Errors->Count) > 0) {
printf("ProviderError\n");
long i,nCount;
nCount = pCn->Errors->Count;
for( i = 0; i < nCount; i++ ) {
pErr = pCn->Errors->GetItem(i);
printf("\tError number: %x\n", pErr->Number );
printf("\tDescription: %s\n", (LPTSTR)pErr->Description);
}
}
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("ComError\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPTSTR)bstrSource);
printf("\tDescription = %s\n", (LPTSTR)bstrDescription);
}
}
| |