首页 > 解决方案 > CRecordset::GetFieldValue() 抛出异常(但并非总是如此)

问题描述

有人可以解释这种奇怪的行为吗?

CString A, B;
CRecordset  rs.Open( forwardOnly, "select A,B from table", readOnly);

// does not work:
rs.GetFieldValue( "A", A); // ok
rs.GetFieldValue( "B", B); // throws; m_nRetCode is set to -1; 
//trace says "dbcore.cpp(174) : AppMsg - invalid descriptor index."

// works:
rs.GetFieldValue( (short) 0, A); // ok
rs.GetFieldValue( (short) 1, B); // ok

// does´nt work either !?
rs.GetFieldValue( rs.GetFieldIndexByName( "A"), A); // ok
rs.GetFieldValue( rs.GetFieldIndexByName( "B"), B); // asserts !?

// throws a different exception:
rs.GetFieldValue( "A", A); // ok
rs.GetFieldValue( "A", A); // throws, but m_nRetCode is 100 now and the trace is saying "data allready fetched" !?

// strange...
rs.GetFieldValue( (short) 0, A); // ok
rs.GetFieldValue( "B", B); // this works
rs.GetFieldValue( "A", A); // this asserts ???

我搜索了一个提示,说forwardOnly不是正确的光标,但使用快照不会改变任何事情。谁能解释一下?

有没有办法来解决这个问题?

标签: databasemfc

解决方案


想我找到了答案:它似乎超出了SQLGetData()中的 CRecordset :

如果驱动程序不支持对 SQLGetData 的扩展,则该函数只能返回数字大于最后一个绑定列的未绑定列的数据。此外,在一行数据中,每次调用 SQLGetData 的 Col_or_Param_Num 参数的值必须大于或等于上一次调用的 Col_or_Param_Num 的值;也就是说,必须按列号递增的顺序检索数据。

https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetdata-function?view=sql-server-2017


推荐阅读