Faqs

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; }
#define TESTC(hr) { if(FAILED(hr)) goto CLEANUP; }

WCHAR* A2WSTR(LPCSTR lp, int nLen) {

USES_CONVERSION;
BSTR str = NULL;

int nConvertedLen = MultiByteToWideChar(_acp,
0,
lp,
nLen,
NULL,
NULL) – 1;

str = ::SysAllocStringLen(NULL, nConvertedLen);

if ( str != NULL ) {
MultiByteToWideChar(_acp,
0,
lp,
-1,
str,
nConvertedLen);
}
return str;
}

LPSTR WSTR2A(LPSTR cBuf, LPCWSTR lpw, LPCSTR lpDef) {

USES_CONVERSION;

int nConvertedLen = WideCharToMultiByte(_acp,
0,
lpw,
-1,
NULL,
0,
lpDef,
NULL) – 1;
if (nConvertedLen>0) {

if ( cBuf != NULL ) {
WideCharToMultiByte(_acp,
0,
lpw,
-1,
cBuf,
nConvertedLen,
lpDef,
NULL);
}
}

return cBuf;
}

CComPtr pMalloc;
MEMORYSTATUS lpBuffer;

// MAIN TEST FUNCTION
///////////////////////////////////////////////////////////////////////////////

void main ()
{

IDBInitialize* pIDBInitialize = NULL;
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;

hr = CoInitialize(NULL);
if (FAILED(hr))
return;

// retrieve pMalloc interface
hr = ::CoGetMalloc(1, (LPMALLOC *) &pMalloc);
if (FAILED(hr))
return;

hr = CoCreateInstance(CLSID_MSDAINITIALIZE,
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 (lpItemText, 0, 200);
memset (lpUDL, 0, 50);
memset (lpUserID, 0, 30);
memset (lpPassword, 0, 30);

printf (“Open connection to HiT OLE DB Provider for DB2\r\n”);
printf (“==================================================\r\n”);

printf (“UDL File\t: “);
scanf (“%s”, lpUDL);
sprintf (lpItemText, “C:\\Program Files\\Common Files\\System\\OLE DB\\Data Links\\%s”, lpUDL);
printf (“\r\n”);

printf (“User ID \t: “);
scanf (“%s”, lpUserID);
printf (“\r\n”);

printf (“Password\t: “);
scanf (“%s”, lpPassword);
printf (“\r\n”);

///////////////////////////////////////////////////////
// Get connection string from Data Link file
nLen = strlen(lpItemText);
pwszTemp = A2WSTR(lpItemText, nLen + 1 );

hr = pIDataInitialize->LoadStringFromStorage(pwszTemp, &pwszInitString);

SysFreeString (pwszTemp);

///////////////////////////////////////////////////////
// Get Data Source object
hr = pIDataInitialize->GetDataSource(NULL,
dwCLSCTX,
pwszInitString,
IID_IDBInitialize,
(IUnknown**)&pDataSource);

hr = pDataSource->QueryInterface(IID_IDBInitialize, (void**)&pIDBInitialize);

if (pIDBProperties == NULL)
hr = pIDBInitialize->QueryInterface (IID_IDBProperties,
(void**)&pIDBProperties);

hr = pIDBProperties->GetProperties(0, NULL, &cPropSets, &prgPropSets);

// Set edit data link properties
DBPROP rgProps[3];
DBPROPSET PropSet;

rgProps[0].dwPropertyID = DBPROP_INIT_PROMPT;
rgProps[0].dwOptions = DBPROPOPTIONS_REQUIRED;
rgProps[0].vValue.vt = VT_I2;
rgProps[0].vValue.iVal = DBPROMPT_NOPROMPT;

rgProps[1].dwPropertyID = DBPROP_AUTH_USERID;
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].dwPropertyID = DBPROP_AUTH_PASSWORD;
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.cProperties = 3;
PropSet.rgProperties = rgProps;
PropSet.guidPropertySet = DBPROPSET_DBINIT;

hr = pIDBProperties->SetProperties (1, &PropSet);

// Free memory
SysFreeString (rgProps[1].vValue.bstrVal);
SysFreeString (rgProps[2].vValue.bstrVal);

// free all properties
pMalloc->Free (prgPropSets->rgProperties);

// now free the property set
pMalloc->Free (prgPropSets);

///////////////////////////////////////////////////////
// Initialize connection
printf (“Connecting…\r\n\r\n”);

hr = pIDBInitialize->Initialize();
if (FAILED(hr))
return;

printf (“CONNECTED\r\n\r\n”);

// to do: put any code here
// …

///////////////////////////////////////////////////////
// Disconnect
printf (“Disconnecting…\r\n”);

// Release any references and continue.
hr = pIDBInitialize->Uninitialize ();

SAFE_RELEASE(pIDBInitialize);

SAFE_RELEASE (pIDBProperties);
SAFE_RELEASE (pDataSource);

SAFE_RELEASE (pIDataInitialize);

CoUninitialize();

SAFE_RELEASE (pIDBInitialize);
}