MDAC 2.5 SDK - OLE DB Programmer's Reference
Chapter 13: Data Types in OLE DB


 

String Data

The term string is used collectively to refer to ANSI strings, Unicode strings, and  BSTRs. When used as metadata—for example, in a schema rowset, a metadata interface such as IColumnsInfo or a data definition interface such as ITableDefinition—string lengths are always measured in characters. When passing data, such as with IRowset::GetData, string lengths are always measured in bytes.

When a consumer gets string data from a provider as an in-memory string rather than through a storage object, the provider always returns a null-terminated string to the consumer. If the consumer's buffer is smaller than the sum of the length of the character data plus the length of the null-termination character, the provider truncates the data and adds a null-termination character to the truncated data. String data retrieved through a storage object is not null-terminated.

Note   Consumers cannot assume that multibyte character set (MBCS) string data was truncated at a character boundary.

To ensure that strings returned by the provider are not truncated, consumers should allocate space for a null-termination character. The following examples illustrate three cases where the buffer size determines whether all significant data is copied into the consumer's buffer:

Note   It is an error to set cbMaxLen to less than the size of the null-termination character.

Whether character data is stored in the provider with a null-termination character is specific to each provider. If the consumer binds the character as another type, such as binary, the character data is returned without a null-termination character.

For ANSI strings and Unicode strings, the length returned in the length part of the binding is the actual length of the data in bytes. The null-termination character is not counted. For BSTRs, the length returned in the length part of the binding is sizeof(BSTR), not the length of the actual string. The length of the actual string can be obtained by calling SysStringLength. For more information, see "Type Indicators" in Appendix A, "Data Types."