【SQL Server CE2.0】打开加密的数据库(源代码)
来源:互联网 发布:淘宝网店首页图片 编辑:程序博客网 时间:2024/05/16 05:46
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;
}
- 【SQL Server CE2.0】打开加密的数据库(源代码)
- 【SQL Server CE2.0】创建加密的数据库(源代码)
- sql server ce2.0开发前的准备
- sql server 2000与sql server ce2.0通过SqlCeRemoteDataA
- sql server 2000与sql server ce2.0通过SqlCeRemoteDataAccess实现数据同步
- VS2008下开发Sql CE2.0应用程序
- SQL Server 2005 数据库加密
- SQL Server 数据库对称加密
- 无法打开sql server 数据库
- 关于使用EFS加密SQL-Server数据库的心得
- SQL Server无法打开用户默认数据库的解决方法
- SQL Server打开发现之前创建的数据库都不见了
- sql server的HASH加密
- SQL Server无法打开用户默认数据库
- VS2012 未能打开解决方案的源代码数据库
- 加密PL/SQL源代码
- 用OleDB打开加密的Access数据库
- SQL--sql server无法打开用户默认数据库 登录失败错误4064的解决方法
- 【SQL Server CE2.0】创建加密的数据库(源代码)
- 个人站点要抓紧ICP备案,否则会被停止服务
- 3月16日天天上周表现
- 解决掉S60系统出错问题
- 农民工的出路
- 【SQL Server CE2.0】打开加密的数据库(源代码)
- 读取DBF库数据
- 金发尤物的困惑
- 在线学习平台、微软应用程序商店
- 每天六个动作小肚子不见了
- SQL SERVER 2000中各表外键名,主键名的获取
- 并发基础 -- Linux 内核同步机制
- OICQ 99c版本加密方法研究报告
- 面对严峻的就业形式,我们该做些什么?