best way to update through c++/ado calls

  • I'm new to the ADO calls through C++, and it seems like there are many ways to

    perform a given operation. In this case I would like to update a table - and

    have seen different ways to do it: one with parameters and one directly through

    the connection execute method.

    Question(s):

    -What are the pros and cons of each, and what is the preferrable method?

    -If I use example #2, do I need a parameter object for each variable (as I show in the

    example) or can I reuse a single parameter for both append calls?

    -Any good books on ADO with C++ (majority of books and examples are VB specific)?

    Code has been simplified. Thanks for any tips on this topic!

    John

    -----------------------------------------------------------

    Update Example #1 (using connection execute method):

    NOTE: objectName id a char * and objectID is an int (variables passed into the routine)

    char sqlBuffer[400];

    _bstr_t updateSQL = "";

    _ConnectionPtr connPtr;

    sprintf (sqlBuffer, "update dbo.object_table set object_name='%s' WHERE object_id=%d",

    objectName, objectID);

    updateSQL = _bstr_t(sqlBuffer);

    try {

    ConnPtr->Open( ... ); // basic open statement (details left out)

    connPtr->Execute (updateSQL, NULL, NULL);

    }

    catch( _com_error &e ) {

    // error code goes here

    }

    -----------------------------------------------------------

    Update Example #2 (using a command and two parameters):

    NOTE: objectName id a char * and objectID is an int (variables passed into the routine)

    _ConnectionPtr connPtr;

    _CommandPtr spCmd;

    _ParameterPtr spPrm1;

    _ParameterPtr spPrm2;

    try {

    CREATEiNSTANCE(ConnPtr,Connection);

    ConnPtr->Open( ... ); // basic open statement (details left out)

    CREATEiNSTANCE(spCmd,Command)

    spCmd->ActiveConnection = ConnPtr;

    spCmd->CommandText = "UPDATE dbo.object_table SET object_name=? WHERE object_id=?";

    spCmd->CommandType = adCmdText;

    spCmd->CommandTimeout = 15;

    _bstr_t bsObjName = objectName;

    spPrm1 = spCmd->CreateParameter(_bstr_t(""), // Name

    adBSTR, // Type

    adParamInput, // Direction

    bsObjName.length(), // Size

    _variant_t(bsObjName)); // Value

    spCmd->Parameters->Append(spPrm1);

    spPrm2 = spCmd->CreateParameter(_bstr_t(""), // Name

    adInteger, // Type

    adParamInput, // Direction

    -1, // Size

    _variant_t((long)objectID)); // Value

    spCmd->Parameters->Append(spPrm2);

    // execute the command.

    spCmd->Execute(NULL,NULL,adCmdText);

    }

    catch( _com_error &e ) {

    // error code goes here

    }

  • I will try to give more details tomorrow. But in my personal preference I would move even beyond Option 2 and create a SQL Stored Procedure then use adCmdStoredProc as the command type, which has many extra options and the reusable execution plans are a major plus. (Note you can still do wih option 1 but I believe I have a document comparing somewhere).

    As for books, everything in C++ I have learned with ADO and DBs has come from the MSDN library (http://msdn.microsoft.com online version). Most all the books I have come across deal with CDatabase objects and RDO instead. I am sure there is something out there but nothing I have found myself.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply