Using Formats for Input Operations

How to Keep Default Formats

In a SAS data set, a variable (column) can have persisted (default) formatting information, which you have the option to keep with the returned data.
Note: If there is no persisted formatting information, then the provider applies a format that best fits the variable type. For more information about persisted formats, see Determining Persisted (Default) Formatting Information.

Sample Code for Using Default Formats

The sample data set has three columns: SALEDATE, QUANTITY, and PRICE. Each column has a persisted format. The following sample code shows you how to specify the default formats when you write code to bind each column.
#include "SASExtensions.h" 1


SASFORMAT formatOverride; 2


memset( &formatOverride, 0, sizeof( formatOverride ) ); 3


formatOverride.wType = SASFMT_FORMAT;4


DBBINDEXT bindingExtension; 5
bindingExtension.pExtension = (BYTE *) &formatOverride
bindingExtension.ulExtension = 1;


DBBINDING rgBindings[3]; 6 
struct ACOLUMN
{
DWORDstatus;
BSTRvalue;
};


memset( rgBindings, 0, sizeof( rgBindings ) ); 7


for( ULONG i =0; i < sizeof(rgBindings); i++) 8
{ 


rgBindings[i].iOrdinal = i+1; 9
rgBindings[i].dwPart = DBPART_VALUE | DBPART_STATUS;
rgBindings[i].obValue = (i * sizeof(struct ACOLUMN)) + offsetof(struct ACOLUMN, value);
rgBindings[i].obStatus = (i * sizeof(struct ACOLUMN)) + offsetof(struct ACOLUMN, status);


rgBindings[i].dwMemOwner = DBMEMOWNER_CLIENTOWNED; 10


rgBindings[i].wType = 11


DBTYPE_BSTR; 12
rgBindings[i].pBindExt = &bindingExtension
}
1enables the application to access SAS extensions to OLE DB. Access is specifically required in order to define the SASFORMAT structure.
2creates a SASFORMAT structure in order to request the default format. The same instance of this structure is reused for all three columns.
3All members are initialized to NULL or zero.
4sets the override type to "formatting".
5creates a DBBINDEXT structure to link the SASFORMAT structure to the DBBINDING structures. The DBBINDEXT structure explains how to organize the data that is returned by the provider. The DBBINDEXT structure is reused for all three columns.
6ties the bindingExtension to each column that will be formatted. There is a DBBINDING structure for each bound column.
7As a matter of good programming, the structure is initialized. Columns are bound in order. However, because it cannot be formatted, the self bookmark is skipped.
8a loop that binds all of the columns as DBTYPE_BSTR and applies the default format as specified by the SASFORMAT structure.
9returns the status because applying a format to a particular data item could fail. The column length is not returned because the data is being returned as BSTR, which has a fixed length.
10frees each data item as the client finishes with it.
11accepts the default character type. If you use a different character type, then specify a maximum column width. Base the value on the width of the format that is being applied.
12supplies the formatting information. You can reuse the same DBBINDEXT and SASFORMAT structure multiple times to use the default format.
With such an array of DBBINDING structures, a call can be made to IAccessor::CreateAccessor to create an HACCESSOR that will obtain columns using SAS formats.