Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Executing Commands

You can execute simple commands by using the Execute() method of the ADOConnection object, whose prototype is shown here:

HRESULT Execute(
    BSTR CommandText,
    VARIANT *RecordsAffected,
    long Options,
    ADORecordset *pSet);

The text of the command, which is generally an SQL command, is passed in the CommandText parameter. You can also pass the address of a VARIANT in RecordsAffected. Upon completion, the long value in the VARIANT at RecordsAffected contains the number of rows that were affected by the command. In addition to SQL commands, you can pass a table name or stored procedure in CommandText. The Options parameter is used to specify the type of command that is passed in CommandText. Options can be passed one of the following values:

For commands that generate a result set, Execute() stores a pointer to a recordset containing the results of the command in the pSet parameter. You can use this recordset to retrieve the results of the command.

The following executeSQL function shows how to execute a simple command using the Execute() method of the ADOConnection object:

CComPtr<ADORecordset> executeSQL(BSTR SQL) {
    //Execute function written by Chuck Wood
    //Executes SQL and returns any recordset that's formed
    CComPtr<ADORecordset> pSet;
    VARIANT recAffected;
    m_pConn->Execute(SQL, &recAffected, adCmdText, &pSet);
    return pSet;
}
					

Share ThisShare This

Informit Network