database - CRecordset::GetFieldValue() 抛出异常(但并非总是如此)

标签 database mfc

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

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不是正确的光标,但使用快照不会改变任何事情。谁能解释一下吗?

有办法解决这个问题吗?

最佳答案

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

If the driver does not support extensions to SQLGetData, the function can return data only for unbound columns with a number greater than that of the last bound column. Furthermore, within a row of data, the value of the Col_or_Param_Num argument in each call to SQLGetData must be greater than or equal to the value of Col_or_Param_Num in the previous call; that is, data must be retrieved in increasing column number order.

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

关于database - CRecordset::GetFieldValue() 抛出异常(但并非总是如此),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53059617/

相关文章:

mysql - 数据库设计: storing hourly analytics for thousands of seperate apps

.net - 在程序中使用 SQL 数据库的要求

c++ - 在工作线程完成 MFC 时禁用控制变量

c++ - 我如何(安全地)中断单独线程上的计算?

mysql - 存储对 MYSQL 数据库所做的更改并将更改更新到不同服务器上的另一个 MYSQL 数据库

mysql - 在 JOIN 上使用键

php - 我需要将我的表单连接到数据库(我认为)

c++ - MFC如何解释SetWindowTextW(LPCTSTR)?

multithreading - MFC中的工作线程终止

c++ - 如何从 CFont 对象获取自动计算的字体宽度?