OLE DB Programmer's Reference |
The code in this example shows how to create and execute a command object.
///////////////////////////////////////////////////////////////// // myCreateCommand // // This function takes an IUnknown pointer on a session object // and attempts to create a command object using the session's // IDBCreateCommand interface. Since this interface is optional, // this may fail. // ///////////////////////////////////////////////////////////////// HRESULT myCreateCommand ( IUnknown * pUnkSession, IUnknown ** ppUnkCommand ) { HRESULT hr; IDBCreateCommand * pIDBCreateCommand = NULL; // Attempt to create a command object from the session object. XCHECK_HR(hr = pUnkSession->QueryInterface( IID_IDBCreateCommand, (void**)&pIDBCreateCommand)); XCHECK_HR(hr = pIDBCreateCommand->CreateCommand( NULL, // pUnkOuter IID_ICommand, // riid ppUnkCommand)); // ppCommand CLEANUP: if( pIDBCreateCommand ) pIDBCreateCommand->Release(); return hr; } ///////////////////////////////////////////////////////////////// // myExecuteCommand // // This function takes an IUnknown pointer on a command // object and performs the following steps to create a new // rowset object: // - Sets the given properties on the command object; these // properties will be applied by the provider to any rowset // created by this command. // - Sets the given command text for the command. // - Executes the command to create a new rowset object. // ///////////////////////////////////////////////////////////////// HRESULT myExecuteCommand ( IUnknown * pUnkCommand, WCHAR * pwszCommandText, ULONG cPropSets, DBPROPSET* rgPropSets, IUnknown ** ppUnkRowset ) { HRESULT hr; ICommandText * pICommandText = NULL; ICommandProperties * vpICommandProperties = NULL; // Set the properties on the command object. XCHECK_HR(hr = pUnkCommand->QueryInterface( IID_ICommandProperties, (void**)&pICommandProperties)); XCHECK_HR(hr = pICommandProperties->SetProperties(cPropSets, rgPropSets)); // Set the text for this command, using the default command text // dialect. All providers that support commands must support this // dialect, and providers that support SQL must be able to recognize // an SQL command as SQL when this dialect is specified. XCHECK_HR(hr = pUnkCommand->QueryInterface( IID_ICommandText, (void**)&pICommandText)); XCHECK_HR(hr = pICommandText->SetCommandText( DBGUID_DEFAULT, // guidDialect pwszCommandText)); // pwszCommandText // And execute the command. Note that the user could have // entered a non-row returning command, so we will check for // that and return failure to prevent the display of the // nonexistent rowset by the caller. XCHECK_HR(hr = pICommandText->Execute( NULL, // pUnkOuter IID_IRowset, // riid NULL, // pParams NULL, // pcRowsAffected ppUnkRowset)); // ppRowset if( !*ppUnkRowset ) { printf("\nThe command executed successfully, but did not " \ "return a rowset.\nNo rowset will be displayed.\n"); hr = E_FAIL; } CLEANUP: if( pICommandText ) pICommandText->Release(); if( pICommandProperties ) pICommandProperties->Release(); return hr; }
1998-2001 Microsoft Corporation. All rights reserved.