当前位置: 动力学知识库 > 问答 > 编程问答 >

Open a disconnected ADO Recordset in C++

问题描述:

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;

}

网友答案:

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.

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);.

分享给朋友:
您可能感兴趣的文章:
随机阅读: