tags:

views:

69

answers:

2

OK, this should be simple. I've just started using ADO in C++, and I'm trying to figure out the best way to insert a record.

At the moment I'm creating a new Recordset and opening it using Open(), but it seems strange to use ADODB::adCmdTable, because it's my understanding that it does a select *. Is there a better option here?

Also, it seems strange to have to pass in the connection string, rather than the connection object that I already have laying around ...

ADODB::_RecordsetPtr prs = NULL;
HRESULT hr = prs.CreateInstance(__uuidof(ADODB::Recordset));
if(!FAILED(hr))
{
  const _variant_t vconn = acpAdoConnection->ConnectionString;
  prs->Open(
    table.c_str(), 
    vconn, 
    ADODB::adOpenUnspecified, 
    ADODB::adLockOptimistic, 
    ADODB::adCmdTable);
  prs->AddNew(fields, values);
}

So what's a better way to insert a single record using ADO?

A: 

What about a simple command:

   Conn1.CreateInstance( __uuidof( ADODB::Connection ) );
   Conn1->ConnectionString = ....;
   Conn1->Open( .... );


   // Drop procedure, if it exists & recreate it.
   Conn1->Execute( "put update query here", ADODB::adCmdText);

Here's an example: http://support.microsoft.com/kb/185125

Preet Sangha
Well, I figured it would be better (faster and less database-dependent) to hand variants to the ADO library rather than stringifying all my values. I think there were another couple of reasons, but they're not coming to mind at the moment.
Matthew Lowe
A: 

Apparently it doesn't do a select * ... at least the record count on the recordset is 0.

Instead of passing in a connection string, you can actually pass the connection object as an IDispatch*, which gets turned into a variant. So a better way of doing things is probably this (skipping the check of the HRESULT):

ADODB::_RecordsetPtr prs = NULL;
HRESULT hr = prs.CreateInstance(__uuidof(ADODB::Recordset));
prs->Open(
    table.c_str(), 
    _variant_t((IDispatch *) acpAdoConnection),
    ADODB::adOpenUnspecified, 
    ADODB::adLockOptimistic, 
    ADODB::adCmdText);
prs->AddNew(fields, values);
Matthew Lowe