Reading columns out of order returns incorrect values (SQL Server ODBC driver)

拈花ヽ惹草 提交于 2019-11-29 14:54:25

The answer is that this behviour won't be fixed in the ODBC driver.

In the late 1980s there was a performance benefit to forcing the client to only read columns out of the row buffer in order. You would ask the driver if you were allowed to read column values in any order through the the SqlGetInfo function:

SqlGetInfo(..., SQL_GD_ANY_ORDER, ...) //returns true or false 
  • SQL_GD_ANY_COLUMN = SQLGetData can be called for any unbound column, including those before the last bound column. Note that the columns must be called in order of ascending column number unless SQL_GD_ANY_ORDER is also returned.
  • SQL_GD_ANY_ORDER = SQLGetData can be called for unbound columns in any order. Note that SQLGetData can be called only for columns after the last bound column unless SQL_GD_ANY_COLUMN is also returned.

Even though computers have more than 4MB of RAM these days, the modern SQL Server ODBC driver continues to opt-in to this limitation from the Windows 3.0 era:

The SQL Server Native Client ODBC driver does not support using SQLGetData to retrieve data in random column order.

They very well could support such a thing, as 17 year old OLEDB drivers, as well as the ADO.NET SqlClient drivers do. But they don't; so the ODBC driver is brain-dead abomination unsuitable for real-world use.

You need to continue to use:

  • SQLOLEDB (supported)
  • SQLNCLI (deprecated)
  • ADO.net SqlClient (supported)

Bonus Reading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!