SP with no results throws ODBC exception in CRecordset

  • Thread starter Thread starter Mike C#
  • Start date Start date
M

Mike C#

Hi all,

I'm writing a little app that uses ODBC to call a SQL 2K stored procedure
several times in a loop, with a different parameter each time. The SP can
return no results in some instances. When no results are returned, the
CRecordset throws an exception. I've tracked the problem down to
dbcore.cpp, where the CRecordset::Open method performs a MoveNext() after
executing the SP (it apparently doesn't bother checking that any results
were actually returned). It works fine as long as at least one row is
returned, otherwise a CBDException is thrown. Does anyone know if there is
a better solution than wrapping my code in a try...catch and ignoring the
CDBException? Here's a snippet of my code:

cmd = _T("{CALL [") + strDatabaseName +
_T("]..sp_helpindex ('") + strTableName + _T("')}");
cr = new CRecordset(this); // 'this' is a class that inherits from CDatabase
try // Right now I'm handling the exception by ignoring it...
{
cr->Open(CRecordset::forwardOnly, cmd.c_str(), CRecordset::none);
// Retrieve the results
while (!cr->IsEOF()) // Loop through them
{
CDBVariant name, desc, keys;
cr->GetFieldValue(_T("index_name"), name, SQL_C_TCHAR);
cr->GetFieldValue(_T("index_description"), desc, SQL_C_TCHAR);
cr->GetFieldValue(_T("index_keys"), keys, SQL_C_TCHAR);
MyIndex mi;
mi.Name = Util::SafeStr(name); // ignore nulls
mi.Keys = Util::SafeStr(keys); // by converting them to empty string
mi.Description = Util::SafeStr(desc);
cr->MoveNext();
}
catch (CDBException* ex) // Could be a real exception...
{ // Could be no results...
TCHAR s[1024];
ex->GetErrorMessage(s, 1024, 0);
wcout << s << _T("\n");
ex->Delete();
}
cr->Close();
}

Any advice appreciated. Thanks!
 
I'm writing a little app that uses ODBC to call a SQL 2K stored procedure
several times in a loop, with a different parameter each time. The SP can
return no results in some instances. When no results are returned, the
CRecordset throws an exception. I've tracked the problem down to
dbcore.cpp, where the CRecordset::Open method performs a MoveNext() after
executing the SP (it apparently doesn't bother checking that any results
were actually returned). It works fine as long as at least one row is
returned, otherwise a CBDException is thrown. Does anyone know if there
is a better solution than wrapping my code in a try...catch and ignoring
the CDBException? Here's a snippet of my code:

Looking at the implementation of Move, and MSDN, it seems that this is by
design.
But if you want to know for sure, try asking in the DAO or MFC newsgroups.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top