博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【SQL Server CE2.0】打开加密的数据库(源代码)
阅读量:4583 次
发布时间:2019-06-09

本文共 5892 字,大约阅读时间需要 19 分钟。

1 HRESULT  hr;  2 DBID  TableName;  // name of table for new constraint  3 DBID  ColumnList[1]; // name of column for new constraint  4 DBID  ConstraintName; // name of new constraint  5 DBPROP  dbprop[1];  6 DBPROP  sscedbprop[2]; // Property array for SSCE security properties  7 DBPROPSET dbpropset[2];  8 DBCONSTRAINTDESC rgConstraintDescs[1]; // Structure for constraint properties  9 IDBInitialize  *pIDBInitialize       = NULL;         10 IDBProperties  *pIDBProperties       = NULL;         11 IDBCreateSession *pIDBCreateSession    = NULL; 12 ITableDefinitionWithConstraints *pITbleDefWithConstrt = NULL; // supports adding constraints 13 int i = 0; 14 // Create an instance of the OLE DB Provider 15 hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER, 16  IID_IDBInitialize, (void**)&pIDBInitialize); 17 if(FAILED(hr)) 18 { 19  RETAILMSG(1,(TEXT("2==CoCreateInstance failed: 0x%x/r/n"),hr)); 20  goto CleanExit; 21 } 22 // Initialize a property with name of database 23 // Open an exsiting database myDatabase 24 VariantInit(&dbprop[0].vValue); 25 for(i = 0;i < sizeof(sscedbprop) / sizeof(sscedbprop[0]);i++) 26 { 27  VariantInit(&sscedbprop[i].vValue); 28 } 29 dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE; 30 dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED; 31 dbprop[0].vValue.vt = VT_BSTR; 32 dbprop[0].vValue.bstrVal = SysAllocString(L"Encrypted.sdf"); 33 // Specify the property for encryption.  34 sscedbprop[0].dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE; 35 sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED; 36 sscedbprop[0].vValue.vt = VT_BOOL; 37 sscedbprop[0].vValue.boolVal = VARIANT_TRUE; 38 // Specify the password. 39 sscedbprop[1].dwPropertyID = DBPROP_SSCE_DBPASSWORD; 40 sscedbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED; 41 sscedbprop[1].vValue.vt = VT_BSTR; 42 sscedbprop[1].vValue.bstrVal = SysAllocString(L"123456"); //密码 43 if(NULL == sscedbprop[1].vValue.bstrVal) 44 { 45  hr = E_OUTOFMEMORY; 46  goto CleanExit; 47 } 48 // Initialize the property set 49 dbpropset[0].guidPropertySet = DBPROPSET_DBINIT; 50 dbpropset[0].rgProperties = dbprop; 51 dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]); 52 dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT; 53 dbpropset[1].rgProperties = sscedbprop; 54 dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]); 55 //Set initialization properties. 56 hr = pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)&pIDBProperties); 57 if(FAILED(hr)) 58 { 59  RETAILMSG(1,(TEXT("2==pIDBInitialize->QueryInterface failed: 0x%x/r/n"),hr)); 60  goto CleanExit; 61 } 62 // Sets properties in the Data Source and initialization property groups 63 hr = pIDBProperties->SetProperties(sizeof(sscedbprop) / sizeof(sscedbprop[0]), dbpropset);  64 if(FAILED(hr)) 65 { 66  RETAILMSG(1,(TEXT("2==pIDBProperties->SetProperties failed: 0x%x/r/n"),hr)); 67  goto CleanExit; 68 } 69 // Initializes a data source object  70 hr = pIDBInitialize->Initialize(); 71 if(FAILED(hr)) 72 { 73  RETAILMSG(1,(TEXT("2==pIDBInitialize->Initialize failed: 0x%x/r/n"),hr)); 74  goto CleanExit; 75 } 76 //只有已经创建表,以下操作才可能成功 77 hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession,(void**)&pIDBCreateSession); 78 if(FAILED(hr)) 79 { 80  RETAILMSG(1,(TEXT("2==pIDBInitialize->QueryInterface failed: 0x%x/r/n"),hr)); 81  goto CleanExit; 82 } 83 // Create a session object.  84 hr = pIDBCreateSession->CreateSession(NULL, IID_ITableDefinitionWithConstraints,  85  (IUnknown**) &pITbleDefWithConstrt); 86 if(FAILED(hr)) 87 { 88  RETAILMSG(1,(TEXT("2==pIDBCreateSession->CreateSession failed: 0x%x/r/n"),hr)); 89  goto CleanExit; 90 } 91 // (This sample assumes that we have information about the TestTable table database schema.) 92 // Prepare the table name DBID as Employees. 93 TableName.eKind = DBKIND_NAME; 94 TableName.uName.pwszName = L"TestTable"; 95 // Prepare the list of columns that will get the UNIQUE constraint.  96 // In this case, just the iID column.  97 ColumnList[0].eKind = DBKIND_NAME; 98 ColumnList[0].uName.pwszName = L"iID"; 99 // Build the DBCONSTRAINTDESC structure needed to make the 100 // ITableDefinitionWithConstraints::AddConstraint 101 // call to add the constraint. 102 rgConstraintDescs[0].pConstraintID = &ConstraintName;103 rgConstraintDescs[0].ConstraintType = DBCONSTRAINTTYPE_UNIQUE;104 rgConstraintDescs[0].cColumns = 1;105 rgConstraintDescs[0].rgColumnList = ColumnList;106 rgConstraintDescs[0].Deferrability = 0;  // SQL Server CE constraints are not deferrable.107 // The following properties are not used in UNIQUE constraints108 rgConstraintDescs[0].pReferencedTableID = NULL;109 rgConstraintDescs[0].cForeignKeyColumns = 0;110 rgConstraintDescs[0].rgForeignKeyColumnList = NULL;111 rgConstraintDescs[0].pwszConstraintText = NULL;112 rgConstraintDescs[0].UpdateRule = DBUPDELRULE_NOACTION;113 rgConstraintDescs[0].DeleteRule = DBUPDELRULE_NOACTION;114 rgConstraintDescs[0].MatchType = DBMATCHTYPE_NONE;115 // Add the new constraint116 hr = pITbleDefWithConstrt->AddConstraint(&TableName, rgConstraintDescs);117 if(FAILED(hr))118 { //0x80040e37: Table does not exist.119  RETAILMSG(1,(TEXT("2==pITbleDefWithConstrt->AddConstraint: 0x%x/r/n"),hr));120  goto CleanExit;121 }122 CleanExit:123 VariantClear(&dbprop[0].vValue);124 SysFreeString(dbprop[0].vValue.bstrVal);125 for (i = 0; i < sizeof(sscedbprop) / sizeof(sscedbprop[0]); i++)126 {127  VariantClear(&sscedbprop[i].vValue);128 }129 if(NULL != pITbleDefWithConstrt)130 {131  pITbleDefWithConstrt->Release();132  pITbleDefWithConstrt = NULL;133 }134 if(NULL != pIDBCreateSession)135 {136  pIDBCreateSession->Release();137  pIDBCreateSession = NULL;138 }139  140 if(NULL != pIDBProperties)141 {142  pIDBProperties->Release();143  pIDBProperties = NULL;144 }145 if(NULL != pIDBInitialize)146 {147  pIDBInitialize->Release();148  pIDBInitialize = NULL;149 }

 

转载于:https://www.cnblogs.com/91program/p/5246526.html

你可能感兴趣的文章
Servlet的生命周期
查看>>
统计文本单词个数,并个数大小按序排列 C#
查看>>
maven setting仓库镜像
查看>>
如何让HTML的编写更具结构性
查看>>
在实际的运用中,我经常遇到需要对基础表的数据进行筛选后再进行行转列
查看>>
UVALive 6560 The Urge to Merge
查看>>
菜鸟简述Jquery中Ajax发送post请求及XML响应
查看>>
Codeforces Round #269 (Div. 2) D. MUH and Cube Walls KMP
查看>>
HDU 4251 The Famous ICPC Team Again 主席树
查看>>
POJ 2774 Long Long Message 后缀数组
查看>>
datagrid中设置编辑,删除列是否可以访问
查看>>
Linux下I/O复用 Select与Poll
查看>>
python全栈学习--day10(函数进阶)
查看>>
Android初学第19天
查看>>
Flask框架web开发
查看>>
LRU算法
查看>>
MongoDB 系列(一) C# 类似EF语法简单封装
查看>>
ios github网址
查看>>
Django 数据库操作之数据库连接
查看>>
写log
查看>>