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

摘要:
=pIDBInitialize)85{86pIDBI初始化-˃释放();87pIDBImitialize=NULL;88}89if(NULL!=pInknownSession)90{91pInknownsession-˃释放(();92pInknowsSession=NULL;93}94返回;
 1 HRESULT  hr = NOERROR; 
 2 DBPROPSET dbpropset[2]; 
 3 DBPROP  dbprop[1]; // Property array to initialize the provider.
 4 DBPROP  sscedbprop[2]; // Property array for SSCE security properties
 5 INT   i = 0;
 6 IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL; 
 7 IUnknown   *pIUnknownSession = NULL;
 8 IDBInitialize  *pIDBInitialize = NULL;
 9 // Create an instance of the OLE DB provider.
10 hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER, 
11  IID_IDBInitialize, (void**)&pIDBInitialize);
12 if(FAILED(hr))
13 {
14  RETAILMSG(1,(TEXT("1==CoCreateInstance: %d /r/n"),GetLastError()));
15  goto CleanExit;
16 }
17 // Initialize property structures.
18 VariantInit(&dbprop[0].vValue);
19 for (i = 0; i < sizeof(sscedbprop) / sizeof(sscedbprop[0]); i++)
20 {
21  VariantInit(&sscedbprop[i].vValue);
22 }
23 // Leo:To create a new database, you must specify the DBPROP_INIT_DATASOURCE property to 
24 // specify a name for the database.
25 dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
26 dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
27 dbprop[0].vValue.vt = VT_BSTR;
28 dbprop[0].vValue.bstrVal = SysAllocString(L"Encrypted.sdf");
29 if(NULL == dbprop[0].vValue.bstrVal)
30 {
31  hr = E_OUTOFMEMORY;
32  goto CleanExit;
33 }
34 // Specify the property for encryption. 
35 sscedbprop[0].dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE;
36 sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
37 sscedbprop[0].vValue.vt = VT_BOOL;
38 sscedbprop[0].vValue.boolVal = VARIANT_TRUE;
39 // Specify the password.
40 sscedbprop[1].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
41 sscedbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED;
42 sscedbprop[1].vValue.vt = VT_BSTR;
43 sscedbprop[1].vValue.bstrVal = SysAllocString(L"123456"); //密码
44 if(NULL == sscedbprop[1].vValue.bstrVal)
45 {
46  hr = E_OUTOFMEMORY;
47  goto CleanExit;
48 }
49 // Initialize the property sets.
50 dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
51 dbpropset[0].rgProperties  = dbprop;
52 dbpropset[0].cProperties  = sizeof(dbprop)/sizeof(dbprop[0]);
53 dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT ;
54 dbpropset[1].rgProperties = sscedbprop;
55 dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
56 hr = pIDBInitialize->QueryInterface(IID_IDBDataSourceAdmin,(void **)&pIDBDataSourceAdmin);
57 if(FAILED(hr))
58 {
59  RETAILMSG(1,(TEXT("1==pIDBInitialize->QueryInterface: %d /r/n"),hr));
60  goto CleanExit;
61 }
62 // Create and initialize the database.
63 hr = pIDBDataSourceAdmin->CreateDataSource(sizeof(dbpropset)/sizeof(dbpropset[0]),
64  dbpropset, NULL, IID_IUnknown, &pIUnknownSession);
65 if(FAILED(hr)) 
66 {
67  RETAILMSG(1,(TEXT("1==pIDBDataSourceAdmin->CreateDataSource: %d /r/n"),hr));
68  goto CleanExit;
69 }
70 // At this point, the new encrypted database is created.
71 leanExit:
72 VariantClear(&dbprop[0].vValue);
73 SysFreeString(dbprop[0].vValue.bstrVal);
74 for(i = 0;i < sizeof(sscedbprop) / sizeof(sscedbprop[0]);i++)
75 {
76  VariantClear(&sscedbprop[i].vValue);
77 }
78 // Do cleanup tasks here.
79 if(NULL != pIDBDataSourceAdmin)
80 {
81  pIDBDataSourceAdmin->Release();
82  pIDBDataSourceAdmin = NULL;
83 }
84 if(NULL != pIDBInitialize)
85 {
86  pIDBInitialize->Release();
87  pIDBInitialize = NULL;
88 }
89 if(NULL != pIUnknownSession)
90 {
91  pIUnknownSession->Release();
92  pIUnknownSession = NULL;
93 }
94 return;

免责声明:文章转载自《【SQL Server CE2.0】创建加密的数据库(源代码)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQL Server CE开发环境建立过程【SQL Server CE2.0】打开加密的数据库(源代码)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SIGPIPE信号

SIGPIPE往一个已经接收到FIN的套接中写是允许的,接收到的FIN仅仅代表对方不再发送数据。并不能代表我不能发送数据给对方。往一个FIN结束的进程中写(write),TCP 协议栈会发送一个RST TCP段过来,TCP重置。如果再调用write就会产生SIGPIPE信号 通常,我们只需要忽略这个信号即可:signal(SIGPIPE,ISG_IGN);...

Linux Socket

Server:{ #include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <arpa/inet.h>#include <sys/socket.h>#include <n...

(转)六类qsort排序方法

转自http://hi.baidu.com/zhangwp999/blog/item/185f9afba454ba63024f5675.html *六类qsort排序方法 P.S.:qsort函数是ANSI C标准中提供的,其声明在stdlib.h文件中,是根据二分发写的,其时间复杂度为n*log(n),其结构为: void qsort(void *base...

Android Binder------ServiceManager启动分析

ServiceManager启动分析 简述: ServiceManager是一个全局的manager、调用了Jni函数,实现addServicew getService checkService listService等函数, Server进程先注册一些service到SercviceManager中。 Client想获得一些service,就要到Serv...

C/C++ 知识点---sizeof使用规则及陷阱分析(网摘)

C/C++ 知识点---sizeof使用规则及陷阱分析 原文出处:【胖奇的专栏】 1、什么是sizeof首先看一下sizeof在msdn上的定义:The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including...

c++内置变量类型

1,各种变量占据的内存空间 char:1个字节,也可亦作为0-255的数值参与运算 一般来说,静态存储区的自动赋初值,动态则不自动(貌似也不对,因为非内置变脸的类型,也都调用了默认构造函数进行初始化) 各种类型的sizeof(为size_t类型,其实就是unsigned int) sizeof是运算符,不是函数,这点和new和delete类似 一个由c/C+...