ICommandText* CClientDB::CreateCommand( IDBInitialize* pIDBInitialize ,LPCWSTR wCmdString ) { IDBCreateSession* pIDBCreateSession = NULL; IDBCreateCommand* pIDBCreateCommand = NULL; ICommandText* pICommandText = NULL; if( pIDBInitialize == NULL || wCmdString == NULL ) { return NULL; } //Create a new activity from the data source object. if(FAILED(pIDBInitialize->QueryInterface( IID_IDBCreateSession, (void**) &pIDBCreateSession))) { return NULL; } if(FAILED(pIDBCreateSession->CreateSession( NULL, IID_IDBCreateCommand, (IUnknown**) &pIDBCreateCommand))) { return NULL; } pIDBCreateSession->Release(); //Create a Command object. if(FAILED(pIDBCreateCommand->CreateCommand( NULL, IID_ICommandText, (IUnknown**) &pICommandText))) { pIDBCreateCommand->Release(); return NULL; } pIDBCreateCommand->Release(); //Set the command text. if(FAILED(pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString ))) { pICommandText->Release(); return NULL; } return pICommandText; }
DWORD CTextildb::Open() { SetCursor(LoadCursor(NULL,IDC_WAIT)); DWORD dr; DWORD rc; HRESULT hr; IDataInitialize * pIDataInitialize = NULL; IDBInitialize * pIDBInitialize = NULL; IDBCreateSession* pIDBCreateSession = NULL; IUnknown * pUnkSession = NULL; ICommandText* pICommandText = NULL; IDBCreateCommand* pICreateCommand = NULL; char cConnectionStr[500]; strcpy(cConnectionStr,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bd1.mdb;Persist Security Info=False"); db_CharToWChar(cConnectionStr); for(;;) { dr = ERR_OLEDBFAIL; hr = CoCreateInstance(CLSID_MSDAINITIALIZE,NULL,CLSCTX_INPROC_SERVER,IID_IDataInitialize,(VOID **)&pIDataInitialize); if (hr != S_OK) break; hr = pIDataInitialize->GetDataSource(NULL,CLSCTX_INPROC_SERVER,(LPCOLESTR)cConnectionStr,IID_IDBInitialize,(IUnknown **)&pIDBInitialize); if (hr != S_OK) break; hr = pIDBInitialize->Initialize(); if (hr != S_OK) break; hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pIDBCreateSession); if (hr != S_OK) break; hr = pIDBCreateSession->CreateSession(NULL,IID_IOpenRowset,&pUnkSession ); if (hr != S_OK) break; hr = pUnkSession->QueryInterface(IID_IDBCreateCommand,(void**)&pICreateCommand); if (hr != S_OK) break; hr = pICreateCommand->CreateCommand(NULL,IID_ICommand,(IUnknown**)&m_lpICommand); if (hr != S_OK) break; dr = ERR_NONE; break; }; if (pIDataInitialize) rc = pIDataInitialize->Release(); if (pIDBInitialize) rc = pIDBInitialize->Release(); if (pIDBCreateSession) rc = pIDBCreateSession->Release(); if (pUnkSession) rc = pUnkSession->Release(); if (pICreateCommand) rc = pICreateCommand->Release(); if (hr != S_OK) { if (m_lpICommand) rc = m_lpICommand->Release(); m_lpICommand = NULL; } SetCursor(LoadCursor(NULL,IDC_ARROW)); return ERR_OLEDBFAIL; }