OLE DB Session Objects (SQL Server Compact)

The primary function of the session object is to define transactions, commands, and rowsets, and for creating and modifying tables and indexes.

Using the Session Objects

In Microsoft SQL Server Compact 4.0, session properties can be set using the ISessionProperties interface.

From a session, you can do the following:

  • Create a command object by calling IDBCreateCommand::CreateCommand. A single session can support multiple commands. For more information, see OLE DB Commands (SQL Server Compact).

  • Create a rowset by calling IOpenRowset::OpenRowset. For more information, see OLE DB Rowsets (SQL Server Compact).

  • Create a schema rowset by calling IDBSchemaRowset::GetRowset. For more information about the schema rowsets supported by SQL Server Compact 4.0, see OLE DB Schema Rowsets (SQL Server Compact).

  • Create or modify tables and indexes by using ITableDefinition and IIndexDefinition.

  • Start and end transactions.

Provider-specific Session Properties

SQL Server Compact 4.0 supports the provider specific property DBPROP_SSCE_ TRANSACTION_COMMIT_MODE. This property is used to specify whether the engine should flush the buffer pool after commit. To change this value, pass DBPROP_SSCE_ TRANSACTION_COMMIT_MODE in the DBPROPSET_SSCE_SESSION property set to the ISessionProperties interface. For more information, see Provider-Specific Properties (OLE DB).

The following example shows how to create a session object and modify the session properties to make the database engine flush the buffer pool after commit.

Example

   // Object declarations
HRESULT                  hr=S_OK;
ULONG                    i                    = 0;
DBPROPSET                dbpropset[2];
DBPROP                   dbprop[1];
DBPROP                   sscedbprop[1];

// Declare the provider interfaces.

IDBInitialize *          pIDBInitialize       = NULL;
IDBProperties *          pIDBProperties       = NULL;
IDBCreateSession *       pIDBCreateSession    = NULL;
ISessionProperties *     pISessionProperties  = NULL;
// Initialize the the data source object.
hr = CoCreateInstance(CLSID_SQLSERVERCE, NULL, CLSCTX_INPROC_SERVER, 
        IID_IDBInitialize, (LPVOID *) &pIDBInitialize);
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}
// Initialize the the property variants.
for (i = 0; i < sizeof(dbprop)/sizeof(dbprop[0]); i++)
{
    VariantInit(&dbprop[i].vValue);
}
for (i = 0; i < sizeof(sscedbprop)/sizeof(sscedbprop[0]); i++)
{
    VariantInit(&sscedbprop[i].vValue);
}
dbprop[0].dwPropertyID   = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(L"\\windows\\Northwind.sdf");
if(NULL == dbprop[0].vValue.bstrVal)
{
    hr = E_OUTOFMEMORY;
    goto Exit; 
}
dbpropset[0].rgProperties    = dbprop;
dbpropset[0].cProperties     = sizeof(dbprop)/sizeof(dbprop[0]);
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
// Initialize the property to change the maximum buffer pool size.
sscedbprop[0].dwPropertyID = DBPROP_SSCE_MAXBUFFERSIZE;
sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[0].vValue.vt = VT_I4;
sscedbprop[0].vValue.lVal = 20*1024; //limit cache usage to 20M, default is 640K
dbpropset[1].rgProperties    = sscedbprop;
dbpropset[1].cProperties     = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
// Set the properties into the provider's data source object.
hr = pIDBInitialize->QueryInterface(IID_IDBProperties,(void**)&pIDBProperties);
if(FAILED(hr))
{
    goto Exit;
}
hr = pIDBProperties->SetProperties(sizeof(dbpropset)/sizeof(dbpropset[0]), 
    dbpropset);
if(FAILED(hr))
{
    goto Exit;
}
// Initialize the data source.
hr = pIDBInitialize->Initialize();
if(FAILED(hr))
{
   goto Exit;
}
// Initialize a session object.
hr = pIDBProperties->QueryInterface(IID_IDBCreateSession, (void **) &pIDBCreateSession);
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}
hr = pIDBCreateSession->CreateSession(NULL, IID_ISessionProperties, 
    (IUnknown**) &pISessionProperties);
Exit:
    // Clean up resources
    return hr;