【SQLServerCE2.0】打开加密的数据库(源代码)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【SQLServerCE2.0】打开加密的数据库(源代码),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5613字,纯文字阅读大概需要9分钟。
内容图文
![【SQLServerCE2.0】打开加密的数据库(源代码)](/upload/InfoBanner/zyjiaocheng/533/04fc097e9a6949f6886333428088bb7e.jpg)
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 < sizeof(sscedbprop) / sizeof(sscedbprop[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 < sizeof(sscedbprop) / sizeof(sscedbprop[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;
}
内容总结
以上是互联网集市为您收集整理的【SQLServerCE2.0】打开加密的数据库(源代码)全部内容,希望文章能够帮你解决【SQLServerCE2.0】打开加密的数据库(源代码)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。