SQL Server CE开发环境建立过程

使用EVC4创建基于对话框的工程,增加一按键并在其响应中Copy sqlce.chm中Creating Databases部分代码进行测试。记录过程如下:

1) error C2065: 'DBPROPSET' : undeclared identifier 等79个编译错误
解决方法:在StdAfx.h中增加以下几个头文件 :

1 #include   <oledb.h>
2 #include   <oledberr.h>
3 #include   <coguid.h>
4 #include   "ca_mergex20.h"
5 #include   "ssceoledb.h"   //DBPROPSET

注:在CSDN上有一帖子,说明除了以上内容外,还需要修改并增加以下内容:

1 修改StdAfx.h
2 //#define   VC_EXTRALEAN   //   Exclude   rarely-used   stuff   from   Windows
3 //上面这条一定要注释掉的,并加入以下引用
4 ////////////////////////////////////////////////////////////////////////////////
5 //以下这几句很重要,否则编译会提示你错误
6 #define   DBINITCONSTANTS
7 #define   INITGUID

验证结果:不需要修改并增加以上内容,否则会产生错误:

StdAfx.obj : error LNK2005: IID_IUnknown already defined in dd2Dlg.obj
ARMV4IDbg/dd2.exe : fatal error LNK1169: one or more multiply defined symbols found

2) 编译产生以下错误:

ssceoledb.h(74)   :   error   C2061:   syntax   error   :   identifier   'DBPROPSET'
ssceoledb.h(100)   :   error   C2061:   syntax   error   :   identifier   'HCHAPTER'
ssceoledb.h(112)   :   error   C2061:   syntax   error   :   identifier   'HCHAPTER'
ssceoledb.h(116)   :   error   C2061:   syntax   error   :   identifier   'HCHAPTER'

解决方法:更新一下SqlCe里的ssceoldb.h文件。下载 http://download.microsoft.com/download/d/0/3/d0337fad-0a9d-4c87-9fe2-c5a2916c7b80/ssceoledb.exe,其中是ssceoldb.h的压缩文件,解压后覆盖SqlCe安装目录的文件。

也可以参考下面的网站: http://support.microsoft.com/default.aspx?scid=kb;en-us;825393

至此,SQLCE.chm中Creating Databases部分代码编译成功!

调试时,发现CoCreateInstance()失败,分析原因是COM未初始化。所以在应用开始与结束时分别调用:CoInitializeEx(NULL, COINIT_MULTITHREADED);和CoUninitialize();

调试代码执行完成,但功能是否实现待分析。

Creating Databases To create a new database, you must specify the DBPROP_INIT_DATASOURCE property to specify a name for the database.

Creating Secure Databases To create an encrypted database using the OLE DB Provider for SQL Server CE, you must pass the provider-specific property DBPROP_SSCE_ENCRYPTDATABASE as VARIANT_TRUE and specify a password by using the provider-specific property DBPROP_SSCE_DBPASSWORD.

示例代码如下(以下代码即为建立SQL CE时的调试代码):

 1 //Object declarations
 2 HRESULT    hr = NOERROR;
 3 DBPROPSET  dbpropset[2];
 4 DBPROP     dbprop[1]; // Property array to initialize the provider.
 5 DBPROP     sscedbprop[2]; // Property array for SSCE security properties
 6 INT        i = 0;
 7 IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL;
 8 IUnknown           *pIUnknownSession = NULL;
 9 
10 //Create an instance of the OLE DB provider.
11 hr = CoCreateInstance( CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER,
12     IID_IDBDataSourceAdmin, (void**)& pIDBDataSourceAdmin);
13 if(FAILED(hr))
14 {
15     goto Exit;
16 }
17 
18 //Initialize property structures.
19 VariantInit(&dbprop[0].vValue);
20 for(i = 0;i < sizeof(sscedbprop) / sizeof(sscedbprop[0]);i++)
21 {
22     VariantInit(&sscedbprop[i].vValue);
23 }
24 
25 //Specify the property with name of the database.
26 dbprop[0].dwPropertyID  = DBPROP_INIT_DATASOURCE;
27 dbprop[0].dwOptions   = DBPROPOPTIONS_REQUIRED;
28 dbprop[0].vValue.vt   = VT_BSTR;
29 dbprop[0].vValue.bstrVal = SysAllocString(L"数据库名.sdf");
30 if(NULL == dbprop[0].vValue.bstrVal)
31 {
32     hr = E_OUTOFMEMORY;
33     goto Exit;
34 }
35 
36 //Specify the property for encryption.
37 sscedbprop[0].dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE;
38 sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
39 sscedbprop[0].vValue.vt = VT_BOOL;
40 sscedbprop[0].vValue.boolVal = VARIANT_TRUE;
41 
42 //Specify the password.
43 sscedbprop[1].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
44 sscedbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED;
45 sscedbprop[1].vValue.vt = VT_BSTR;
46 sscedbprop[1].vValue.bstrVal = SysAllocString(L"数据库密码");
47 if(NULL == sscedbprop[1].vValue.bstrVal)
48 {
49     hr = E_OUTOFMEMORY;
50     goto Exit;
51 }
52 
53 //Initialize the property sets.
54 dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
55 dbpropset[0].rgProperties  = dbprop;
56 dbpropset[0].cProperties  = sizeof(dbprop)/sizeof(dbprop[0]);
57 
58 dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT ;
59 dbpropset[1].rgProperties  = sscedbprop;
60 dbpropset[1].cProperties  = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
61 
62 //Create and initialize the database.
63 hr = pIDBDataSourceAdmin->CreateDataSource(sizeof(dbpropset) / sizeof(dbpropset[0]),dbpropset,NULL,IID_IUnknown,&pIUnknownSession);
64 if(FAILED(hr))
65 {
66     goto Exit;
67 }
68 //At this point, the new encrypted database is created.
69 
70 Exit:
71 // Do cleanup tasks here.
72 
73 return;

Accessing Password-protected Databases Use the DBPROP_SSCE_DBPASSWORD property in the DBPROPSET_SSCE_DBINIT provider-specific property set to specify this property.

注:

一:注意每次操作SQL CE前都Close一次,因为SQLCE2.0只支持一个连接

二:检测数据库里是否存在某个表用:select table_name from Information_Schema.Tables

三:SQL CE 2.0 不支持存储过程、触发器等,都要用SQL来实现

四:从XML转换过来的时间要转换一下: DateTime dtConvert = DateTime.Parse(xmlTable.ChildNodes[0].InnerXml); dtConvert.ToString("G");

五:SQL CE的字段类型和SQL Server的不一样,具体见SQLCE帮助文档

六:SQL CE不支持Distict 、top 等函数,Distinct可以用GroupBy来实现

posted @ 2016-03-06 09:10  91program  阅读(646)  评论(0编辑  收藏  举报