DB2Connectivity -OLE DB
Q:Visual C++ を使用して AS/400への接続をどのようにイニシエイトしますか?
A: サンプルコードは:
///////////////////////////////////////////////////////////////////////////////
// OLE400 Test Sample – 07-07-2000
//
// Standard sample for testing HiT OLEDB400 Provider.
// Initialization phases can be used in any other connect exercise.
//
///////////////////////////////////////////////////////////////////////////////
#include
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include
#include
#include “atldb.h”
#include
#include
#include #define SAFE_RELEASE(pv) if(pv) { (pv)->Release(); (pv) = NULL; } WCHAR* A2WSTR(LPCSTR lp, int nLen) { USES_CONVERSION; int nConvertedLen = MultiByteToWideChar(_acp, str = ::SysAllocStringLen(NULL, nConvertedLen); if ( str != NULL ) { LPSTR WSTR2A(LPSTR cBuf, LPCWSTR lpw, LPCSTR lpDef) { USES_CONVERSION; int nConvertedLen = WideCharToMultiByte(_acp, if ( cBuf != NULL ) { return cBuf; CComPtr // MAIN TEST FUNCTION void main () IDBInitialize* pIDBInitialize = NULL; hr = CoInitialize(NULL); // retrieve pMalloc interface hr = CoCreateInstance(CLSID_MSDAINITIALIZE, /////////////////////////////////////////////////////// memset (lpItemText, 0, 200); printf (“Open connection to HiT OLE DB Provider for DB2\r\n”); printf (“UDL File\t: “); printf (“User ID \t: “); printf (“Password\t: “); /////////////////////////////////////////////////////// hr = pIDataInitialize->LoadStringFromStorage(pwszTemp, &pwszInitString); SysFreeString (pwszTemp); /////////////////////////////////////////////////////// hr = pDataSource->QueryInterface(IID_IDBInitialize, (void**)&pIDBInitialize); if (pIDBProperties == NULL) hr = pIDBProperties->GetProperties(0, NULL, &cPropSets, &prgPropSets); // Set edit data link properties rgProps[0].dwPropertyID = DBPROP_INIT_PROMPT; rgProps[1].dwPropertyID = DBPROP_AUTH_USERID; rgProps[2].dwPropertyID = DBPROP_AUTH_PASSWORD; PropSet.cProperties = 3; hr = pIDBProperties->SetProperties (1, &PropSet); // Free memory // free all properties // now free the property set /////////////////////////////////////////////////////// hr = pIDBInitialize->Initialize(); printf (“CONNECTED\r\n\r\n”); // to do: put any code here /////////////////////////////////////////////////////// // Release any references and continue. SAFE_RELEASE(pIDBInitialize); SAFE_RELEASE (pIDBProperties); SAFE_RELEASE (pIDataInitialize); CoUninitialize(); SAFE_RELEASE (pIDBInitialize);
#define TESTC(hr) { if(FAILED(hr)) goto CLEANUP; }
BSTR str = NULL;
0,
lp,
nLen,
NULL,
NULL) – 1;
MultiByteToWideChar(_acp,
0,
lp,
-1,
str,
nConvertedLen);
}
return str;
}
0,
lpw,
-1,
NULL,
0,
lpDef,
NULL) – 1;
if (nConvertedLen>0) {
WideCharToMultiByte(_acp,
0,
lpw,
-1,
cBuf,
nConvertedLen,
lpDef,
NULL);
}
}
}
MEMORYSTATUS lpBuffer;
///////////////////////////////////////////////////////////////////////////////
{
IDataInitialize* pIDataInitialize = NULL;
DWORD dwCLSCTX = CLSCTX_INPROC_SERVER;
DBPROPSET* prgPropSets = NULL;
ULONG cPropSets;
IUnknown* pDataSource = NULL;
IDBProperties* pIDBProperties = NULL;
WCHAR* pwszTemp;
WCHAR* pwszInitString;
HRESULT hr = S_OK;
if (FAILED(hr))
return;
hr = ::CoGetMalloc(1, (LPMALLOC *) &pMalloc);
if (FAILED(hr))
return;
NULL,
CLSCTX_INPROC_SERVER,
IID_IDataInitialize,
(void**)&pIDataInitialize);
// Prompt data
char lpItemText [200];
char lpUDL [50];
char lpUserID [30];
char lpPassword [30];
int nLen = -1;
memset (lpUDL, 0, 50);
memset (lpUserID, 0, 30);
memset (lpPassword, 0, 30);
printf (“==================================================\r\n”);
scanf (“%s”, lpUDL);
sprintf (lpItemText, “C:\\Program Files\\Common Files\\System\\OLE DB\\Data Links\\%s”, lpUDL);
printf (“\r\n”);
scanf (“%s”, lpUserID);
printf (“\r\n”);
scanf (“%s”, lpPassword);
printf (“\r\n”);
// Get connection string from Data Link file
nLen = strlen(lpItemText);
pwszTemp = A2WSTR(lpItemText, nLen + 1 );
// Get Data Source object
hr = pIDataInitialize->GetDataSource(NULL,
dwCLSCTX,
pwszInitString,
IID_IDBInitialize,
(IUnknown**)&pDataSource);
hr = pIDBInitialize->QueryInterface (IID_IDBProperties,
(void**)&pIDBProperties);
DBPROP rgProps[3];
DBPROPSET PropSet;
rgProps[0].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProps[0].vValue.vt = VT_I2;
rgProps[0].vValue.iVal = DBPROMPT_NOPROMPT;
rgProps[1].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProps[1].vValue.vt = VT_BSTR;
nLen = strlen(lpUserID);
pwszTemp = A2WSTR(lpUserID, nLen + 1 );
V_BSTR(&(rgProps[1].vValue))= SysAllocStringLen (pwszTemp, wcslen(pwszTemp));
rgProps[2].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProps[2].vValue.vt = VT_BSTR;
nLen = strlen(lpPassword);
pwszTemp = A2WSTR(lpPassword, nLen + 1 );
rgProps[2].vValue.bstrVal = SysAllocStringLen (pwszTemp, wcslen(pwszTemp));
PropSet.rgProperties = rgProps;
PropSet.guidPropertySet = DBPROPSET_DBINIT;
SysFreeString (rgProps[1].vValue.bstrVal);
SysFreeString (rgProps[2].vValue.bstrVal);
pMalloc->Free (prgPropSets->rgProperties);
pMalloc->Free (prgPropSets);
// Initialize connection
printf (“Connecting…\r\n\r\n”);
if (FAILED(hr))
return;
// …
// Disconnect
printf (“Disconnecting…\r\n”);
hr = pIDBInitialize->Uninitialize ();
SAFE_RELEASE (pDataSource);
}