Executing statements directly

The ICommandText::Execute() function prepares and executes a SQL statement. The code samples in this section describe how to execute a statement without parameters. Optionally, the statement can include parameters.

StepsTo execute a statement without parameters

  1. Obtain a Command object from the session:

    ICommandText* pICommandText;
    hr = pIDBCreateCommand->CreateCommand(
         NULL, IID_ICommandText,
         (IUnknown**)&pICommandText);
    
  2. Set the SQL statement the command will execute:

    hr = pICommandText->SetCommandText(
         DBGUID_DBSQL,
         L"DELETE FROM publishers where pub_id = '7777' ");
    
  3. Execute the command. The cRowsAffected contain the number of rows inserted, deleted, or updated by the command. The pIRowset is assigned to the Rowset object created by the command, as shown:

    DBROWCOUNT cRowsAffected;
    IRowset* pIRowset;
    hr = pICommandText->Execute(
         NULL, IID_IRowset, NULL,
         &cRowsAffected, (IUnknown**)&pIRowset);