【SQL Server CE2.0】创建加密的数据库(源代码)
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;
推荐阅读
-
SQL Server 2008 数据库中创建只读用户的方法
-
SQL Server创建数据库和数据表的相关约束实现方法
-
创建SQL Server数据库集群的经历
-
SQL server数据库创建代码 filegroup文件组修改的示例代码
-
FineReport9.0定义数据连接(创建与SQL Server 2016数据库的连接)
-
在Navicat premium上创建的SQL Server数据库,实现用PHP连接(即php连接微软MSSQL),navicatpremium_PHP教程
-
SQL Server数据库之数据库的创建、修改、查看、删除及分离与附加的方法讲解
-
【SQL Server CE2.0】创建加密的数据库(源代码)
-
在Navicat premium上创建的SQL Server数据库,实现用PHP连接即php连接微软MSSQL
-
FineReport9.0定义数据连接(创建与SQL Server 2016数据库的连接)