Question

I have to execute multiple INSERT queries and I wish to reuse one Command object instead to make new one for every query I have.

To illustrate this better here is a pseudo-code :

ADODB::_CommandPtr pCmd("ADODB.Command");

pCmd->ActiveConnection = pConn;

pCmd->CommandText = L"INSERT INTO MyTable ( field1, field2 ) VALUES (?,?);";

pCmd->Parameters->Append( pCmd->CreateParameter( "?", ADODB::adVarWChar,
    ADODB::adParamInput, wcslen( myString ), myString ) );

pCmd->Parameters->Append( pCmd->CreateParameter( "?", ADODB::adDouble,
    ADODB::adParamInput, sizeof(double), 
    _wtof_l( myDouble, _get_current_locale() ) ) );

pCmd->Execute( NULL, NULL, ADODB::adCmdText );

// empty parameters collection->the problematic part

/**** execute second query **************/

pCmd->CommandText = //some other query

// and do basically the same as above:
// append some parameters and execute query

I do not know how to delete all parameters from Parameters collection so I can reuse the Command object for another SQL query.

Being inexperienced, I have tried finding the solution on the internet and found some suggestions on how to do it. Here are my tries:

do
{
    pCmd->Parameters->Delete(0);
}
while( pCmd->Parameters->GetCount() > 0 );

but failed. I have also tried this:

for( long i = 0; i < pCmd->Parameters->GetCount(); i++ )
    pCmd->Parameters->Delete(i);

but that failed too.

How can I empty Parameters collection properly so I can reuse the Command object?

Thank you.

Best regards.

Was it helpful?

Solution

Looking better at the samples from the Internet I managed to figure this out. The point is to delete parameters from end towards the beginning like this:

for( long i = pCmd->Parameters->GetCount()-1; i >= 0; i-- ) 
    pCmd->Parameters->Delete(i);

Or like this:

do
{ 
    pCmd->Parameters->Delete( pCmd->Parameters->GetCount()-1 ); 
}
while( pCmd->Parameters->GetCount()-1 >= 0 );

I will still follow the advice given by member WhozCraig and create new Command object for every query.

Thanks again WhozCraig and best regards until next time!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top