简体   繁体   English

C ++中断开连接的记录集

[英]Disconnected recordset in C++

I am trying to open a disconnected recordset in C++ using ADO & SQL server. 我正在尝试使用ADO&SQL Server在C ++中打开断开连接的记录集。 Code is below. 代码如下。 If I run the code and do not close anything but leave all the connections open, works correctly. 如果我运行代码并且不关闭任何东西,而是保持所有连接打开,则说明工作正常。 If I try to disconnect the recordset (using PutRefActiveConnection) and close the connect object per the MS example, I get an error that the operation is not allowed on an open connection. 如果我尝试断开记录集(使用PutRefActiveConnection)并按照MS示例关闭连接对象,则会收到错误消息,表示在打开的连接上不允许进行该操作。 I tried cloning the record set but that throws up too. 我尝试克隆记录集,但这也引发了问题。 Also tried executing PutRefActiveConnection, then close. 还尝试执行PutRefActiveConnection,然后关闭。 Also tried dymanic cursors. 还尝试了动态光标。 Tried read only locks, optimistic and pessimistic locks. 尝试了只读锁,乐观锁和悲观锁。

Anyone have any suggestions? 有人有什么建议吗?

Connection string : 连接字符串:

Provider=SQLNCLI11;Data Source=GCSQL\\GFDB;DataTypeCompatibility=80;Initial catalog=GF_Trades;Trusted_Connection=yes; Provider = SQLNCLI11;数据源= GCSQL \\ GFDB; DataTypeCompatibility = 80;初始目录= GF_Trades; Trusted_Connection =是;

Code: 码:

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

Turns out that the problem was in the construction of the connection and the opening of the recordset. 事实证明,问题出在连接的建立和记录集的打开。 Setting the cursor to the client side has to occur at the connection level, not as an option to opening the recordset. 将光标设置到客户端必须在连接级别进行,而不是作为打开记录集的选项。

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

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

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