Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

How to open a disconnected ADO Recordset in C++?

$
0
0

Hello, this is a crosspost from stackoverflow, because I think this might be the better place to get an answer.

I am trying to clone an ADO Recordset from a database query into a disconnected Recordset. The goal is to have control over the field attributes in order to be able to modify even autoincrement values and computed columns.

The C++ code is based on a working function that I use in Classic ASP and the cloning works fine until I try to open the new Recordset. Then I get an error message saying, that the connection cannot be used. ("Die Verbindung kann nicht verwendet werden, um diesen Vorgang auszuführen. Sie ist entweder geschlossen oder in diesem Zusammenhang ungültig.")

Since it works in VBScript, I am trying to reproduce the simple VBScript code rs.Open without any arguments in C++.

What would be the correct Syntax to open this disconnected Recordset, that got created in code, without a connection to a database?

_RecordsetPtr DataService::CloneRecordset(_RecordsetPtr rs, bool withValues)
{
    _RecordsetPtr newRs;
    HRESULT hr = newRs.CreateInstance(__uuidof(Recordset));
    if(FAILED(hr))
        return nullptr;
    newRs->CursorLocation = adUseClient;
    try
    {
        for(long i; i < rs->Fields->Count; ++i)
        {
            FieldPtr f = rs->Fields->GetItem(i);
            long attributes = adFldIsNullable | adFldMayBeNull | adFldUpdatable;
            if(f->Attributes & adFldKeyColumn)
                attributes |= adFldKeyColumn;
            newRs->Fields->Append(f->Name, f->Type, f->DefinedSize, (FieldAttributeEnum)attributes);
        }
        if(withValues)
        {
            newRs->putref_Source(NULL);
            //Throws error
            newRs->Open(vtMissing, vtMissing, adOpenUnspecified, adLockUnspecified, adCmdUnspecified);
            CopyRecordset(rs, newRs);
            newRs->UpdateBatch(adAffectAll);
        }
        return newRs;
    }
    catch (_com_error &ce)
    {
        ShowComErrorMessageBox(ce, newRs);
    }
    catch(...)
    {
        AfxMessageBox("An unknown error has occured.");
    }
    return nullptr;
}


Viewing all articles
Browse latest Browse all 15302

Trending Articles