Share via


OLE DB 工作階段物件 (SQL Server Compact)

工作階段物件的主要功能在於定義交易、命令與資料列集,以及建立和修改資料表和索引。

使用工作階段物件

在 Microsoft SQL Server Compact 4.0 中,您可以使用 ISessionProperties 介面來設定工作階段的屬性。

在工作階段中,您可以:

  • 呼叫 IDBCreateCommand::CreateCommand 建立命令物件。單一工作階段可以支援多個命令。如需詳細資訊,請參閱<OLE DB 命令 (SQL Server Compact)>。

  • 呼叫 IOpenRowset::OpenRowset 建立資料列集。如需詳細資訊,請參閱<OLE DB 資料列集 (SQL Server Compact)>。

  • 呼叫 IDBSchemaRowset::GetRowset 建立結構描述資料列集。如需 SQL Server Compact 4.0 所支援結構描述資料集的詳細資訊,請參閱<OLE DB 結構描述資料列集 (SQL Server Compact)>。

  • 使用 ITableDefinition 和 IIndexDefinition 建立或修改資料表和索引。

  • 起始與結束交易。

特定提供者的工作階段屬性

SQL Server Compact 4.0 支援提供者的特定 DBPROP_SSCE_TRANSACTION_COMMIT_MODE 屬性。此屬性是用來指定引擎在確認之後是否清除緩衝集區。若要修改這個值,請將 DBPROPSET_SSCE_SESSION 屬性集中的 DBPROP_SSCE_TRANSACTION_COMMIT_MODE 傳遞至 ISessionProperties 介面。如需詳細資訊,請參閱<提供者特定的屬性 (OLE DB)>。

以下範例將示範如何建立工作階段並且修改工作階段屬性,使資料庫引擎在確認之後清除緩衝區。

範例

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