Helo guys
I use this code to read a single column from a sql server table. the column's data type is text, containing 174 chars. I use smaller buffer size of 100 wchars to get the actual data size in StrLen_or_IndPtr. When BufferLength is <= 348 bytes, SQLGetData returns SQL_SUCCESS_WITH_INFO, SALSTATE is 01004(Indicating right truncation), 100 wchars is copied to my buffer, with a terminating null, but StrLen_or_IndPtr returns -4. when BufferLength >= 350 bytes, Every thing is okay, and StrLen_or_IndPtr returns 348.
every thing is okay and StrLen_or_IndPtr returns 350.
wchar_t * wszQuery = L"SELECT [MATN] FROM [ATSETUP] WHERE [IDKEY] = \'AT:CODING\'";
SQLRETURN iRetCode;
iRetCode = ::SQLExecDirectW(hstmt, (SQLWCHAR*)wszQuery, SQL_NTS);
if (!SQL_SUCCEEDED(iRetCode)) return FALSE;
iRetCode = ::SQLFetch(hstmt);
if (iRetCode == SQL_NO_DATA) return TRUE;
if (!SQL_SUCCEEDED(iRetCode)) return FALSE;
SQLSMALLINT iSqlCType = SQL_C_WCHAR;
SQLSMALLINT iColumn = 1;
SQLLEN iOutputLen = 0;
const int BUFLEN = 100;
wchar_t wszBuffer[BUFLEN + 1];
iRetCode = ::SQLGetData(hstmt, iColumn, iSqlCType, wszBuffer, sizeof(wszBuffer), &OutputLen);
if I use SQL_C_CHAR, this problem doesn't occur. I use sql-server 2012 express 64 bit.
Can any one help me?
Edit: This problem doesn't occur when data type of column is ntext or nvarchar(max). It occurs for text and varchar(max).
Edit: The problem is: SQLGetData refuses to return size of data in case converting from char to wchar, vice versa. leaving calculating size of data to programmer. This is so bad. returning the size of data would be very simpler and faster if returned by SQLGetData.
What I have tried:
I have tried the C code above.