Compartilhar via


Objetos de sessão do OLE DB (SQL Server Compact)

A principal função do objeto de sessão é definir transações, comandos e conjuntos de linhas, além de criar e modificar tabelas e índices.

Usando os objetos de sessão

No Microsoft SQL Server Compact 4.0, as propriedades de sessão podem ser definidas com o uso da interface ISessionProperties.

Em uma sessão, é possível fazer o seguinte:

Propriedades de sessão específicas do provedor

O SQL Server Compact 4.0 dá suporte à propriedade específica ao provedor DBPROP_SSCE_TRANSACTION_COMMIT_MODE. Essa propriedade é usada para especificar se o mecanismo deve liberar o pool de buffers após a confirmação. Para alterar esse valor, passe DBPROP_SSCE_TRANSACTION_COMMIT_MODE na propriedade DBPROPSET_SSCE_SESSION definida para a interface ISessionProperties. Para obter mais informações, consulte Propriedades específicas do provedor (OLE DB).

O exemplo a seguir mostra como criar um objeto de sessão e modificar as propriedades de sessão para fazer o mecanismo de banco de dados liberar o pool de buffers após a confirmação.

Exemplo

   // 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;