【SQL Server CE2.0】打开加密的数据库(源代码)
HRESULThr; DBIDTableName; // name of table for new constraint DBIDColumnList[1]; // name of column for new constraint DBIDConstraintName; // name of new constraint DBPROPdbprop[1]; DBPROPsscedbprop[2]; // Property array for SSCE security p
HRESULT hr;
DBID TableName; // name of table for new constraint
DBID ColumnList[1]; // name of column for new constraint
DBID ConstraintName; // name of new constraint
DBPROP dbprop[1];
DBPROP sscedbprop[2]; // Property array for SSCE security properties
DBPROPSET dbpropset[2];
DBCONSTRAINTDESC rgConstraintDescs[1]; // Structure for constraint properties
IDBInitialize *pIDBInitialize = NULL;
IDBProperties *pIDBProperties = NULL;
IDBCreateSession *pIDBCreateSession = NULL;
ITableDefinitionWithConstraints *pITbleDefWithConstrt = NULL; // supports adding constraints
int i = 0;
// Create an instance of the OLE DB Provider
hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER,
IID_IDBInitialize, (void**)&pIDBInitialize);
if(FAILED(hr))
{
RETAILMSG(1,(TEXT("2==CoCreateInstance failed: 0x%x/r/n"),hr));
goto CleanExit;
}
// Initialize a property with name of database
// Open an exsiting database myDatabase
VariantInit(&dbprop[0].vValue);
for(i = 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"Encrypted.sdf");
// Specify the property for encryption.
sscedbprop[0].dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE;
sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[0].vValue.vt = VT_BOOL;
sscedbprop[0].vValue.boolVal = VARIANT_TRUE;
// Specify the password.
sscedbprop[1].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
sscedbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[1].vValue.vt = VT_BSTR;
sscedbprop[1].vValue.bstrVal = SysAllocString(L"123456"); //密码
if(NULL == sscedbprop[1].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto CleanExit;
}
// Initialize the property set
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
dbpropset[1].rgProperties = sscedbprop;
dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
//Set initialization properties.
hr = pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)&pIDBProperties);
if(FAILED(hr))
{
RETAILMSG(1,(TEXT("2==pIDBInitialize->QueryInterface failed: 0x%x/r/n"),hr));
goto CleanExit;
}
// Sets properties in the Data Source and initialization property groups
hr = pIDBProperties->SetProperties(sizeof(sscedbprop) / sizeof(sscedbprop[0]), dbpropset);
if(FAILED(hr))
{
RETAILMSG(1,(TEXT("2==pIDBProperties->SetProperties failed: 0x%x/r/n"),hr));
goto CleanExit;
}
// Initializes a data source object
hr = pIDBInitialize->Initialize();
if(FAILED(hr))
{
RETAILMSG(1,(TEXT("2==pIDBInitialize->Initialize failed: 0x%x/r/n"),hr));
goto CleanExit;
}
//只有已经创建表,以下操作才可能成功
hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession,(void**)&pIDBCreateSession);
if(FAILED(hr))
{
RETAILMSG(1,(TEXT("2==pIDBInitialize->QueryInterface failed: 0x%x/r/n"),hr));
goto CleanExit;
}
// Create a session object.
hr = pIDBCreateSession->CreateSession(NULL, IID_ITableDefinitionWithConstraints,
(IUnknown**) &pITbleDefWithConstrt);
if(FAILED(hr))
{
RETAILMSG(1,(TEXT("2==pIDBCreateSession->CreateSession failed: 0x%x/r/n"),hr));
goto CleanExit;
}
// (This sample assumes that we have information about the TestTable table database schema.)
// Prepare the table name DBID as Employees.
TableName.eKind = DBKIND_NAME;
TableName.uName.pwszName = L"TestTable";
// Prepare the list of columns that will get the UNIQUE constraint.
// In this case, just the iID column.
ColumnList[0].eKind = DBKIND_NAME;
ColumnList[0].uName.pwszName = L"iID";
// Build the DBCONSTRAINTDESC structure needed to make the
// ITableDefinitionWithConstraints::AddConstraint
// call to add the constraint.
rgConstraintDescs[0].pConstraintID = &ConstraintName;
rgConstraintDescs[0].ConstraintType = DBCONSTRAINTTYPE_UNIQUE;
rgConstraintDescs[0].cColumns = 1;
rgConstraintDescs[0].rgColumnList = ColumnList;
rgConstraintDescs[0].Deferrability = 0; // SQL Server CE constraints are not deferrable.
// The following properties are not used in UNIQUE constraints
rgConstraintDescs[0].pReferencedTableID = NULL;
rgConstraintDescs[0].cForeignKeyColumns = 0;
rgConstraintDescs[0].rgForeignKeyColumnList = NULL;
rgConstraintDescs[0].pwszConstraintText = NULL;
rgConstraintDescs[0].UpdateRule = DBUPDELRULE_NOACTION;
rgConstraintDescs[0].DeleteRule = DBUPDELRULE_NOACTION;
rgConstraintDescs[0].MatchType = DBMATCHTYPE_NONE;
// Add the new constraint
hr = pITbleDefWithConstrt->AddConstraint(&TableName, rgConstraintDescs);
if(FAILED(hr))
{ //0x80040e37: Table does not exist.
RETAILMSG(1,(TEXT("2==pITbleDefWithConstrt->AddConstraint: 0x%x/r/n"),hr));
goto CleanExit;
}
CleanExit:
VariantClear(&dbprop[0].vValue);
SysFreeString(dbprop[0].vValue.bstrVal);
for (i = 0; i {
VariantClear(&sscedbprop[i].vValue);
}
if(NULL != pITbleDefWithConstrt)
{
pITbleDefWithConstrt->Release();
pITbleDefWithConstrt = NULL;
}
if(NULL != pIDBCreateSession)
{
pIDBCreateSession->Release();
pIDBCreateSession = NULL;
}
if(NULL != pIDBProperties)
{
pIDBProperties->Release();
pIDBProperties = NULL;
}
if(NULL != pIDBInitialize)
{
pIDBInitialize->Release();
pIDBInitialize = NULL;
}