会话对象的主要功能是用于定义事务、命令和行集,以及用于创建和修改表和索引。
使用会话对象
在 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;