以 OLE DB 存取密碼保護的資料庫
MicrosoftSQL Server Compact 3.5 支援檔案層級存取控制機制,這種機制需要提供密碼才能存取受密碼保護的 SQL Server Compact 3.5 資料庫。每次開啟資料庫時,都必須提供此密碼。
存取受保護的資料庫
使用特定提供者屬性集 DBPROP_SSCE_DBPASSWORD 中的 DBPROPSET_SSCE_DBINIT 屬性來指定此屬性。建立資料庫時,可利用此屬性來指定資料庫的密碼。加密的資料庫永遠受到密碼保護。
壓縮資料庫時,可以使用此屬性將資料庫密碼變更為新的值。
以下程式碼範例說明如何開啟密碼保護的 SQL Server Compact 3.5 資料庫。系統會顯示對話方塊,要求使用者輸入密碼,但本範例未包含這段程式碼。
範例
// Object declarations
HRESULT hr = NOERROR;
DBPROPSET dbpropset[2];
DBPROP dbprop[1];
DBPROP sscedbprop[1];
BSTR pwdPassword; // user input password
// Declare the provider interfaces.
IDBInitialize * pIDBInitialize = NULL;
IDBProperties * pIDBProperties = NULL;
// Initialize the data source.
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_SQLSERVERCE, 0, CLSCTX_INPROC_SERVER,
IID_IDBInitialize, (void**) &pIDBInitialize);
if (FAILED(hr))
{
// Send an error-specific message and do error handling.
goto Exit;
}
// Initialize property structures.
VariantInit(&dbprop[0].vValue);
VariantInit(&sscedbprop[0].vValue);
// Initialize Property with name of database.
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(L"ProtectedData.sdf");
if(NULL == dbprop[0].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
// Second property set has one property containing the
// provider-specific database password in the pwdPassword variable
// that was obtained from a dialog box (not shown).
sscedbprop[0].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[0].vValue.vt = VT_BSTR;
sscedbprop[0].vValue.bstrVal = SysAllocString(<pwdPassword>);
if(NULL == sscedbprop[0].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
// Initialize property set.
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
// Initialize the provider-specific property set.
dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
dbpropset[1].rgProperties = sscedbprop;
dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
// Set the properties into the provider's data source object.
pIDBInitialize->QueryInterface(IID_IDBProperties,(void**)&pIDBProperties);
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;
}
Exit:
// Clean up resources here.
return;