🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: microsoft.public.data.ado
Article #3

Updating db by connecting a disconnected recordset in ADO

#3
From: noirracoon
Date: Fri, 07 Jul 2000 00:00
99 lines
3747 bytes
Hello,

I have two questions.
1) How (in c++) do you disconnect and reconnect via ActiveConnection in
   ADO.

The code I tried follows.  I also tried variations of this code
and I didn't get any errors, but it did not update the database
with the new row.

    _RecordsetPtr   pRs("ADODB.Recordset");
    pRs->CursorLocation = adUseClient;
    hResult = pRs->Open((variant_t)"VEH", m_spConnDispPtr,
        adOpenStatic, adLockBatchOptimistic, adCmdTable);

    //* disconnect
    hResult = pRs->putref_ActiveConnection(NULL);

    hResult = pRs->AddNew(vtMissing, vtMissing);

    set all fields ...

    hResult = pRs->Update();

    //* reconnect
    hResult = pRs->putref_ActiveConnection(m_spConnDispPtr);

    //*
    hResult = pRs->UpdateBatch(adAffectAll);
    hResult = pRs->Close();


2) How can you create a recordset from scratch without a connection
   and then connect to a database and Update() a table as long as
   all the field definitions are the same.

   The following is what I tried.  It didn't cause an error,
   but the database did not get updated.

   //* Open a recordset that is connected to the database
   //* so we can copy the filed definitions into the disconnected
   //* recordset.
   _RecordsetPtr   pRsDb("ADODB.Recordset");
   _RecordsetPtr   pRs("ADODB.Recordset");
   hRetVal = pRsDb->Open((variant_t)szTableName,
       m_spConnDispPtr, adOpenStatic, adLockReadOnly, adCmdTable);

    if (SUCCEEDED(hRetVal))
    {
        FieldsPtr   pFields = pRsDb->Fields;
        FieldPtr    pField = NULL;
        variant_t   vtIndex;

        //* Loop through each field, appending a copy to the
        //*  output recordset.
        for (INT i = 0; (i < pFields->Count) && (SUCCEEDED(hRetVal));
i++)
        {
            vtIndex = (LONG)i;
            pField = pFields->Item[vtIndex];
            hRetVal = pRs->Fields->Append(pField->Name, pField->Type,
                      pField->DefinedSize,
                       (FieldAttributeEnum)pField->Attributes );
        }
    }

    //* Open the disconnected recordset.
    pRs->CursorLocation = adUseClient;
    hRetVal = pRs->Open(bstrEmpty, vtMissing, adOpenKeyset,
        adLockOptimistic, adCmdUnknown);

    hResult = pRs->AddNew(vtMissing, vtMissing);

    ... set all fields

    hResult = pRs->Update();

    //* connect to the database and update
    hResult = pRs->putref_ActiveConnection(m_spConnDispPtr);
    hResult = pRs->UpdateBatch(adAffectAll);
    hResult = pRs->Close();

I guess my question here is how to you set the table name that is
to be affected?

And what else am I doing wrong?

I'm fairly new to ADO, so I appologize if this question is a
no-brainer for most of you.

any help would be appreciated

Thanks,
    noirracoon.



Sent via Deja.com http://www.deja.com/
Before you buy.
Technical Details
Message-ID: <8k56ek$17$1@nnrp1.deja.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10845c,d9c5f4dcb1d819b5,start
X-Google-Attributes: gid10845c,public
X-Google-Thread: 1003e9,d9c5f4dcb1d819b5,start
X-Google-Attributes: gid1003e9,public
X-Google-Thread: f5cad,d9c5f4dcb1d819b5,start
X-Google-Attributes: gidf5cad,public
From: noirracoon@my-deja.com
Subject: Updating db by connecting a disconnected recordset in ADO
Date: 2000/07/07
Message-ID: <8k56ek$17$1@nnrp1.deja.com>
X-Deja-AN: 643567415
X-Http-Proxy: 1.0 x73.deja.com:80 (Squid/1.1.22) for client 192.158.61.12
Organization: Deja.com - Before you buy.
X-Article-Creation-Date: Fri Jul 07 18:10:30 2000 GMT
X-MyDeja-Info: XMYDJUIDnoirracoon
Newsgroups: microsoft.public.data.ado,microsoft.public.data.ado.rds,borland.public.delphi.database.ado
X-Http-User-Agent: Mozilla/4.5 [en]C-CCK-MCD (WinNT; I)