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

php中文网
发布: 2016-06-07 15:32:15
原创
1350人浏览过

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号