OLE DB를 통한 암호로 보호된 데이터베이스 액세스

Microsoft SQL Server Compact 4.0에서는 파일 수준 액세스 제어 메커니즘을 지원합니다. 이 메커니즘을 사용하면 암호로 보호된 SQL Server Compact 4.0 데이터베이스에 액세스할 때 암호를 입력해야 합니다. 데이터베이스를 열 때마다 암호를 제공해야 합니다.

보호된 데이터베이스 액세스

DBPROPSET_SSCE_DBINIT 공급자별 속성 집합의 DBPROP_SSCE_DBPASSWORD 속성을 사용하여 이 속성을 지정할 수 있습니다. 데이터베이스를 만들 때 이 속성을 사용하면 데이터베이스에 데이터베이스 암호를 지정할 수 있습니다. 암호화된 데이터베이스는 항상 암호로 보호됩니다.

데이터베이스를 압축할 때 이 속성을 사용하여 데이터베이스 암호를 새 값으로 변경할 수 있습니다.

다음 코드 예에서는 암호로 보호된 SQL Server Compact 4.0 데이터베이스를 여는 방법을 보여 줍니다. 사용자에게 암호 입력을 요구하는 대화 상자가 표시되는데, 이 샘플 프로그램에는 그 코드가 포함되어 있지 않습니다.

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