C ++中断开的记录集

时间:2018-01-08 19:23:55

标签: c++ ado recordset

我正在尝试使用ADO& Cn在C ++中打开断开连接的记录集。 SQL服务器。代码如下。如果我运行代码并且不关闭任何内容但是保持所有连接都打开,则可以正常工作。如果我尝试断开记录集(使用PutRefActiveConnection)并按照MS示例关闭连接对象,我会收到一个错误,即在打开的连接上不允许该操作。我尝试克隆记录集,但也会抛出。还尝试执行PutRefActiveConnection,然后关闭。还试过了dymanic游标。尝试只读锁,乐观和悲观锁。

有人有任何建议吗?

连接字符串:

Provider = SQLNCLI11; Data Source = GCSQL \ GFDB; DataTypeCompatibility = 80; Initial catalog = GF_Trades; Trusted_Connection = yes;

代码:

try
{
ADODB::_ConnectionPtr   _conn;

hr = _conn.CreateInstance(__uuidof(ADODB::Connection));
_conn->CommandTimeout = 0;
_conn->ConnectionString = _mConnectionString.c_str();
_conn->Open(_conn->GetConnectionString(), "", "", ADODB::adConnectUnspecified);
hr = _mRecordSet.CreateInstance(__uuidof(ADODB::Recordset));
hr = _mRecordSet->Open(_mQueryString.c_str(), _variant_t(_conn, true), ADODB::CursorTypeEnum::adOpenStatic, ADODB::LockTypeEnum::adLockReadOnly, ADODB::adCmdText || ADODB::CursorLocationEnum::adUseClient);
if (hr != S_OK)
{
    _mRecordSet = NULL;
    _conn->Close();
    return;
}
_mRecordSet->PutRefActiveConnection(NULL);
_conn->Close();
}
catch (_com_error e)
{
    std::string s = e.Description();
    s.append("\n Connection std::string = ");
    s.append(_mConnectionString);
    std::cout << s << endl;
    return;
}

1 个答案:

答案 0 :(得分:0)

原来问题在于构建连接和打开记录集。将光标设置为客户端必须在连接级别进行,而不是打开记录集的选项。

            ADODB::_ConnectionPtr _conn;

            hr = _conn.CreateInstance(__uuidof(ADODB::Connection));
            _conn->CommandTimeout = 0;
            _conn->ConnectionString = _mConnectionString.c_str();
            _conn->CursorLocation = ADODB::adUseClient;
            _conn->Open(_conn->GetConnectionString(), "", "", ADODB::adConnectUnspecified);
            hr = _mRecordSet.CreateInstance(__uuidof(ADODB::Recordset));
            hr = _mRecordSet->Open(_mQueryString.c_str(), _variant_t(_conn, true),
                ADODB::CursorTypeEnum::adOpenStatic, ADODB::LockTypeEnum::adLockReadOnly, ADODB::adCmdText);
            _mRecordSet->PutRefActiveConnection(NULL);
            _conn->Close();
            _conn = NULL;
        }
        catch (_com_error e)...