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; }
///////////////////////////////////////////////////////////////// // HRESULT CRow::CreateCommand // ///////////////////////////////////////////////////////////////// HRESULT CRow::CreateCommand(CAggregate* pCAggregate, REFIID riid, IUnknown** ppIUnknown) { HRESULT hr = S_OK; //Obtain the IDBCreateCommand Interface IDBCreateCommand* pIDBCreateCommand = SOURCE_GETINTERFACE(this, IDBCreateCommand); if(pIDBCreateCommand) { //CreateCommand XTEST(hr = pIDBCreateCommand->CreateCommand(pCAggregate, riid, ppIUnknown)); TESTC(TRACE_METHOD(hr, L"IDBCreateCommand::CreateCommand(0x%p, %s, &0x%p)", pCAggregate, GetInterfaceName(riid), ppIUnknown ? *ppIUnknown : NULL)); //Handle Aggregation if(pCAggregate) TESTC(hr = pCAggregate->HandleAggregation(riid, ppIUnknown)); } CLEANUP: return hr; }
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; }
/******************************************************************** SqlSessionExecuteQuery - executes a query and returns the results if desired NOTE: ppirs and pcRoes and pbstrErrorDescription are optional ********************************************************************/ extern "C" HRESULT DAPI SqlSessionExecuteQuery( __in IDBCreateSession* pidbSession, __in __sql_command LPCWSTR wzSql, __out_opt IRowset** ppirs, __out_opt DBROWCOUNT* pcRows, __out_opt BSTR* pbstrErrorDescription ) { Assert(pidbSession); HRESULT hr = S_OK; IDBCreateCommand* pidbCommand = NULL; ICommandText* picmdText = NULL; ICommand* picmd = NULL; DBROWCOUNT cRows = 0; if (pcRows) { *pcRows = NULL; } // // create the command // hr = pidbSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&pidbCommand); ExitOnFailure(hr, "failed to create database session"); hr = pidbCommand->CreateCommand(NULL, IID_ICommand, (IUnknown**)&picmd); ExitOnFailure(hr, "failed to create command to execute session"); // // set the sql text into the command // hr = picmd->QueryInterface(IID_ICommandText, (LPVOID*)&picmdText); ExitOnFailure(hr, "failed to get command text object for command"); hr = picmdText->SetCommandText(DBGUID_DEFAULT , wzSql); ExitOnFailure1(hr, "failed to set SQL string: %ls", wzSql); // // execute the command // hr = picmd->Execute(NULL, (ppirs) ? IID_IRowset : IID_NULL, NULL, &cRows, reinterpret_cast<IUnknown**>(ppirs)); ExitOnFailure1(hr, "failed to execute SQL string: %ls", wzSql); if (DB_S_ERRORSOCCURRED == hr) { hr = E_FAIL; } if (pcRows) { *pcRows = cRows; } LExit: if (FAILED(hr) && picmd && pbstrErrorDescription) { HRESULT hrGetErrors = SqlGetErrorInfo(picmd, IID_ICommandText, 0x409, NULL, pbstrErrorDescription); // TODO: use current locale instead of always American-English if (FAILED(hrGetErrors)) { ReleaseBSTR(*pbstrErrorDescription); } } ReleaseObject(picmd); ReleaseObject(picmdText); ReleaseObject(pidbCommand); return hr; }