CAUSE
GetFieldValue() allocates a buffer and then calls CRecordset::GetData() to
retrieve data from the field into this buffer. For long binary and
character data, the length of the data is not known in advance, so
GetFieldValue() does not know what size of buffer to allocate.
Therefore, for SQL_LONGVARCHAR and SQL_LONGVARBINARY data, GetFieldValue()
calls GetData() with a buffer length of 1. GetData() tries to retrieve the
data and will likely fail because the buffer is too small. Whether
GetData() fails or not, it returns the actual length of the data in the
field excluding the NULL termination byte. GetFieldValue() then calls the
function CRecordset::GetLongCharDataAndCleanup() to reallocate the buffer
and retrieve the rest of the data, if necessary.
GetLongCharDataAndCleanup() must determine whether GetData() was able to
retrieve the data by checking whether the actual data length is larger than
the buffer length:
// If long data, may need to call SQLGetData again
if (nLen < nActualSize &&
(nSQLType == SQL_LONGVARCHAR || nSQLType == SQL_LONGVARBINARY))
For 1-character SQL_LONGVARCHAR data, GetData() will fail because the
buffer must be large enough to hold the data plus the NULL termination
byte. Then, GetData() returns an actual size of 1, which does not include
the NULL byte.
GetLongCharDataAndCleanup() fails to consider the space required for the
NULL termination byte. If the data length is 1, then both nLen and
nActualSize are 1, which means that GetLongCharDataAndCleanup() will not
attempt to get the additional data.