Accessing Data with a Different Encoding

Goal

You want to access data sets that use an encoding that is different from the machine locale, and want to display characters instead of question marks (?).

OLE DB Implementation

When the local provider is used to access data that contains international characters, and the character variables are not bound correctly, the character values appear as question marks (?). When reading character data from a data set that has an encoding that is different from the machine locale, bind the type as DBTYPE_WSTR.
Binding Character Data as DBTYPE_WSTR
DBBINDING        rgBind[MAXBINDINGS];
DBCOLUMNINFO     *rgInfo = NULL;
DBORDINAL        cNumColumns, i = 0;

for (i = 0; i < cNumColumns; i++)
{
    rgBind[i].cbMaxLen = rgInfo[i].ulColumnSize + sizeof(wchar_t); 1
    // more code
    rgBind[i].wType = DBTYPE_WSTR; 2
    rgBind[i].bPrecision = rgInfo[i].bPrecision;
    rgBind[i].bScale = rgInfo[i].bScale;
    rgBind[i].obLength = dwOffset + offsetof(COLUMNDATA, dwLength);
    rgBind[i].obStatus = dbOffset + offsetof(COLUMNDATA, dwStatus);
    rgBind[i].obValue = dbOffset + offsetof(COLUMNDATA, bData);
}
1This line of code shows how to calculate the length of the character data. In UTF-16 encoding, each character is stored in two bytes.
2When binding to DBTYPE_WSTR, then the local provider returns the character data in UTF-16 encoding.
There are three ways to bind character data:
DBTYPE_STR The local provider interprets the data according to the Windows code page that is associated with the locale of the machine. This is also known as the thread encoding. This value is different from the Windows system code page.
DBTYPE_WSTR The local provider interprets the data as UTF-16.
DBTYPE_BSTR The local provider interprets the data as a BSTR, as defined by Microsoft. This is similar to UTF-16 and is the type that is used by all ADO client applications.