Home >Database >Mysql Tutorial >【SQL Server CE2.0】创建加密的数据库(源代码)

【SQL Server CE2.0】创建加密的数据库(源代码)

WBOY
WBOYOriginal
2016-06-07 15:32:071360browse

HRESULThr = NOERROR; DBPROPSETdbpropset[2]; DBPROPdbprop[1]; // Property array to initialize the provider. DBPROPsscedbprop[2]; // Property array for SSCE security properties INTi = 0; IDBDataSourceAdmin*pIDBDataSourceAdmin = NULL; IUnknow

HRESULT  hr = NOERROR;
DBPROPSET dbpropset[2];
DBPROP  dbprop[1]; // Property array to initialize the provider.
DBPROP  sscedbprop[2]; // Property array for SSCE security properties
INT   i = 0;
IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL;
IUnknown   *pIUnknownSession = NULL;
IDBInitialize  *pIDBInitialize = NULL;

// 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("1==CoCreateInstance: %d /r/n"),GetLastError()));
 goto CleanExit;
}

// Initialize property structures.
VariantInit(&dbprop[0].vValue);
for (i = 0; i {
 VariantInit(&sscedbprop[i].vValue);
}

// Leo:To create a new database, you must specify the DBPROP_INIT_DATASOURCE property to
// specify a name for the database.
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");
if(NULL == dbprop[0].vValue.bstrVal)
{
 hr = E_OUTOFMEMORY;
 goto CleanExit;
}

// 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 sets.
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]);

hr = pIDBInitialize->QueryInterface(IID_IDBDataSourceAdmin,(void **)&pIDBDataSourceAdmin);
if(FAILED(hr))
{
 RETAILMSG(1,(TEXT("1==pIDBInitialize->QueryInterface: %d /r/n"),hr));
 goto CleanExit;
}

// Create and initialize the database.
hr = pIDBDataSourceAdmin->CreateDataSource(sizeof(dbpropset)/sizeof(dbpropset[0]),
 dbpropset, NULL, IID_IUnknown, &pIUnknownSession);
if(FAILED(hr))
{
 RETAILMSG(1,(TEXT("1==pIDBDataSourceAdmin->CreateDataSource: %d /r/n"),hr));
 goto CleanExit;
}
// At this point, the new encrypted database is created.

leanExit:
VariantClear(&dbprop[0].vValue);
SysFreeString(dbprop[0].vValue.bstrVal);

for(i = 0;i {
 VariantClear(&sscedbprop[i].vValue);
}

// Do cleanup tasks here.
if(NULL != pIDBDataSourceAdmin)
{
 pIDBDataSourceAdmin->Release();
 pIDBDataSourceAdmin = NULL;
}
if(NULL != pIDBInitialize)
{
 pIDBInitialize->Release();
 pIDBInitialize = NULL;
}
if(NULL != pIUnknownSession)
{
 pIUnknownSession->Release();
 pIUnknownSession = NULL;
}

return;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn