使用EVC4创建基于对话框的工程,增加一按键并在其响应中Copy sqlce.chm中Creating Databases部分代码进行测试。记录过程如下:
1) error C2065: 'DBPROPSET' : undeclared identifier 等79个编译错误
解决方法:在StdAfx.h中增加以下几个头文件 :
#include <oledb.h>
#include <oledberr.h>
#include <coguid.h>
#include "ca_mergex20.h"
#include "ssceoledb.h" //DBPROPSET
注:在CSDN上有一帖子,说明除了以上内容外,还需要修改并增加以下内容:
修改StdAfx.h
//#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows
//上面这条一定要注释掉的,并加入以下引用
////////////////////////////////////////////////////////////////////////////////
//以下这几句很重要,否则编译会提示你错误
#define DBINITCONSTANTS
#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时的调试代码):
//Object declarations
HRESULT hr = NOERROR;
DBPROPSET dbpropset[2];
DBPROP dbprop[1]; // Property array to initialize the provider.
DBPROP sscedbprop[2]; // Property array for SSCE security properties
INT i = 0;
IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL;
IUnknown *pIUnknownSession = NULL;
//Create an instance of the OLE DB provider.
hr = CoCreateInstance( CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER,
IID_IDBDataSourceAdmin, (void**)& pIDBDataSourceAdmin);
if(FAILED(hr))
{
goto Exit;
}
//Initialize property structures.
VariantInit(&dbprop[0].vValue);
for(i = 0;i < sizeof(sscedbprop) / sizeof(sscedbprop[0]);i++)
{
VariantInit(&sscedbprop[i].vValue);
}
//Specify the property with name of the database.
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(L"数据库名.sdf");
if(NULL == dbprop[0].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
//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"数据库密码");
if(NULL == sscedbprop[1].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
//Initialize the property sets.
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]);
//Create and initialize the database.
hr = pIDBDataSourceAdmin->CreateDataSource(sizeof(dbpropset) / sizeof(dbpropset[0]),dbpropset,NULL,IID_IUnknown,&pIUnknownSession);
if(FAILED(hr))
{
goto Exit;
}
//At this point, the new encrypted database is created.
Exit:
// Do cleanup tasks here.
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来实现