简体   繁体   English

在C ++中打开断开连接的ADO记录集

[英]Open a disconnected ADO Recordset in C++

I am trying to clone an ADO Recordset from a database query into a disconnected Recordset. 我试图将ADO Recordset从数据库查询克隆到断开连接的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. C ++代码基于我在Classic ASP中使用的工作功能,在我尝试打开新的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.") (“ Die Verbindung kann nicht verwendet werden,um diesen Vorgangauszuführen。Sieist enwrder geschlossen oder in sisem Zusammenhangungültig。”)

Since it works in VBScript, I am trying to reproduce the simple VBScript code rs.Open without any arguments in C++. 由于它可以在VBScript中运行,因此我试图在C ++中重现简单的VBScript代码rs.Open不带任何参数。

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;
}

The described error message appears, when neither connection and source nor fields are correctly defined. 当未正确定义连接和源或字段时,将出现所描述的错误消息。 In the above code the initialization of the variable long i is missing. 在上面的代码中,缺少变量long i的初始化。

Changing it to for(long i=0; i < rs->Fields->Count; ++i) makes it work and also voids the necessity for the line newRs->putref_Source(NULL); 将其更改为for(long i=0; i < rs->Fields->Count; ++i)使其起作用,并且也newRs->putref_Source(NULL);需要newRs->putref_Source(NULL); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM