问题
here i am trying to query a table of sql server 2008 r2 from my c++ application. It contains 21,54,514. but it throws an exception like "Query timeout expired" . below is my code base
ADODB::_ConnectionPtr m_pDBConnection;
ADODB::_RecordsetPtr m_pRS;
m_pDBConnection.CreateInstance("ADODB.Connection");
m_pRS.CreateInstance("ADODB.RecordSet");
m_pRS->Open(wstrSQL.c_str(), _variant_t((IDispatch *) m_pDBConnection, true),
ADODB::adOpenStatic,
ADODB::adLockReadOnly,
ADODB::adCmdText);
When the above line is get executed it throws the above said exception.
i know the issue , the issue is actually with default timeout set. it is actually 15 sec.
can anybody please tell how to reset or change timeout in ADODB::_RecordsetPtr. I searched in google a lot , we can do reset timeout for ADODB::_ConnectionPtr but not for ADODB::_RecordsetPtr. it would be vary usefull for me. Thanks in advance.
回答1:
According to Coding Journey you need to set the CommandTimeout on the connection and on the command.
// The recordset for the result
ADODB::_RecordsetPtr rs;
// Timeout on connection
m_pDBConnection->CommandTimeout = 60;
// Need to create command first, then configure it, then open RecordSet
ADODB::_CommandPtr cmd(__uuidof(ADODB::Command));
cmd->ActiveConnection = m_pDBConnection;
cmd->CommandText = wstrSQL.c_str();
cmd->CommandTimeout = 60;
rs->Open(cmd, vtMissing, ADODB::adOpenStatic, ADODB::adLockReadOnly, ADODB::adCmdText);
// Reset the timeout on the connection
m_pDBConnection->CommandTimeout = 15;
来源:https://stackoverflow.com/questions/24508564/adodb-recordsetptropen-throws-exception-like-query-timeout-expired-in-c