[CE编程]第7章 Files, Databases, and the Registry (二)CE中的数据库API

来源:互联网 发布:php项目例子 编辑:程序博客网 时间:2024/04/19 07:38

                                                                CE中的数据库API         



  CE提供了一套唯一的(只能在CE下)的数据库API集,能为一些有组织的数据提供很有效的支持。

        一,基本的定义:
         
     1. Records允许的9种数据类型:
                
                      数据类型                            描述

IVal

2-byte signed integer

UiVal

2-byte unsigned integer

LVal

4-byte signed integer

UlVal

4-byte unsigned integer

FILETIME

A time and date structure

LPWSTR

0-terminated Unicode string

CEBLOB

A collection of bytes

BOOL

Boolean

Double

8-byte signed value

//好啊,有CEBLOB,不错不错

索引在数据库创建时同时被建立,但以后也是可以重新定义的。索引是有代价的,所以请根据你的需要来定制索引。
简而言之,CE提供了基本的数据库功能,能有效的帮助你的应用程序组织一些简单的数据结构。Pocket PC 使用database API 来管理the address book, the task list和e-mail messages.

        2.设计数据库:
        请在你使用CeCreateDatabaseEx之前请先仔细的想想数据库将要怎样使用。不要把这里的数据库想象成大型数据库,不能超过数据库结构的限制。虽然限制是很宽松的:一个单独属性最多不能超过CEDB_MAXPROPDATASIZE=65,471,一个单一的记录不能达到CEDB_MAXRECORDSIZE=131,072。

        3.
Database Volumes :
        Database Volumes以Volumes的形式存储在外部介质中,也直接在object store中。它是一种CE数据库能定位的特定的格式,能存储在PC卡上或者类似的存储设备上。不方便的就是你必须在使用其之前先mount,关闭前unmount。Volume是一个文件,在文件系统中,Volume是被隐藏的,但是可以通过标准的文件操作函数删除。因此你不能阻止一些勇猛的用户当空间不够时不顾一切的去寻找并有可能删除一个Volume。//汗ing.....

            
        
二,数据库API:
        
计划了数据库,那就开始编程吧:

        
Mounting a Database Volume 
        如果你的数据库在外部设备上,如CompactFlash card,你就必须挂载一个database volume 包含该数据库

         BOOL CeMountDBVol (PCEGUID pguid, LPWSTR lpszVol, DWORD dwFlags);    
        这个函数有2个作用:创建一个新的volume或者打开一个已经存在的volume。第一个参数是指向一个guid的指针。CeMountDBVol
返回一个大部分数据库函数用来确定数据库文件的地址的gudi。不要和OLE中的GUID类型相混淆,-CEGUID只是一个句柄来追踪已经
打开的数据库文件。
        第2个参数是要挂载的volume名。注意:不是数据库名。因为一个volume可以包含多个数据库,这里也是个文件名。标准扩展名应
该是.CDB。
        第3个参数用来说明函数的行动:
CREATE_NEW ,CREATE_ALWAYS,OPEN_EXISTING ,OPEN_ALWAYS ,
TRUNCATE_EXISTING。
    如果函数成功的话,返回TRUE,guid被设值,以后会在其他数据库函数里用到。失败的话,调用GetLastError,返回
错误。


    数据库volume一次可被多个进程打开。系统会为此volume维护一个引用计数器,当最后一个进程unmount这个volume,系统
就unmount这个volume。
    






    Enumerating Mounted Database Volumes
  
 列举已经挂载的数据库volumes:

    
    BOOL CeEnumDBVolumes (PCEGUID pguid, LPWSTR lpBuf, DWORD dwSize);
    
    CeEnumDBVolumes 返回TRUE 如果一个已挂载的volume被发现,然后这个volume的guid和name用pguid
lpBuff
这2个参数来指向。第3个参数是lpBuff所指向的缓冲区的大小。
    
    Ex:
CEGUID guid;TCHAR szVolume[MAX_PATH];INT nCnt = 0;   CREATE_INVALIDGUID (&guid);        //将guid设为invalidwhile (CeEnumDBVolumes (&guid, szVolume, sizeof (szVolume))) {    // guid contains the guid of the mounted volume;    // szVolume contains the name of the volume.    nCnt++;   // Count the number of mounted volumes.}
     Unmounting a Database Volume
     BOOL CeUnmountDBVol (PCEGUID pguid);
     
只要一个已挂载的数据库volume的guid就可以了:)










 
    

     Using the Object Store as a Database Volume

   尽管你可以在外部设备上以volume的形式存储一个数据库,但更常用的,你要在object store里存储数据库。因为
大部分数据库函数都需要一个CEGUID 来确定一个数据库volume,你可以用下面这个宏:
   
CREATE_SYSTEMGUID (PCEGUID pguid);






     Creating a Database: 

   CEOID CeCreateDatabaseEx2 (PCEGUID pguid, CEDBASEINFOEX *pInfo);

    
    第1个参数是用来指示数据库位置的guid。第2个参数就厉害了,看看他的结构:
typedef struct _CEDBASEINFOEX {    WORD     wVersion;        //结构自己的version,要被设为CEDBASEINFOEX_VERSION    WORD     wNumSortOrder;   // 设为rgSortSpecsArray里的排序索引数,最大为4    DWORD    dwFlags;         //看下面的介绍    WCHAR    szDbaseName[CEDB_MAXDBASENAMELEN]; //数据库名,最大32字符(包括最后一个0)    DWORD    dwDbaseType;          DWORD    dwNumRecords;    //ignored     DWORD    dwSize;          //ignored     FILETIME ftLastModified;  //ignored     SORTORDERSPECEX rgSortSpecs[CEDB_MAXSORTORDER];} CEDBASEINFOEX, *PCEDBASEINFOEX;
    The dwFlags field has two uses. First, it contains flags indicating which fields in the
structure are valid. The possible values for the dwFlags field
are CEDB_VALIDNAME,
CEDB_VALIDTYPE, CEDB_VALIDSORTSPEC, and CEDB_ VALIDDBFLAGS. When you're creating a database,
it's easier to set the dwFlags field to CEDB_VALIDCREATE, which is a combination of the flags
I just listed. An additional
flag, CEDB_VALIDMODTIME, is used when CeOidGetInfo uses this
structure.
The other use for the dwFlags parameter is to specify the properties of the database. Two flags
are currently defined. The first
is CEDB_NOCOMPRESS, which can be specified if you don't want
the database you're creating to be compressed. By default, all databases are compressed, which
saves storage space at the expense of speed. By specifying the CEDB_NOCOMPRESS flag, the
database will be larger but you will be able to read and write to the database faster. The
second flag that can be
defined is CEDB_SYSTEMDB. This flag indicates that the database cannot
be deleted by an untrusted application. Trusted and untrusted applications are part of the
Windows CE security architecture and will be discussed in
Chapter 10.

typedef struct _SORTORDERSPECEX {    WORD wVersion;    WORD wNumProps;    WORD wKeyFlags;    CEPROPID rgPropID[CEDB_MAXSORTPROP];    DWORD rgdwFlags[CEDB_MAXSORTPROP];} SORTORDERSPECEX;
Note:

The function CeCreateDatabaseEx2 was added to Windows CE .NET 4.0. If an application needs to
run on a Windows CE 3.0–based system, such as a Pocket PC 2000 or Pocket PC 2002, the
application must use the
function CeCreateDatabaseEx to create a database. The chief difference
between the two functions is that CeCreateDatabaseEx2 allows multilevel sorting, whereas

CeCreateDatabaseEx
does not.


    
Opening a Database:







    HANDLE CeOpenDatabaseEx2 (PCEGUID pguid, PCEOID poid, LPWSTR lpszName,                                                         SORTORDERSPECEX *pSort,                                                         DWORD dwFlags,                                                         CENOTIFYREQUEST *pRequest);








    
Seeking (or Searching for) a Record:

CEOID CeSeekDatabaseEx (HANDLE hDatabase, DWORD dwSeekType, DWORD dwValue, WORD wNumVals, LPDWORD lpdwIndex);

    


Changing the Sort Order:
    BOOL CeSetDatabaseInfoEx2 (PCEGUID pguid,                                CEOID oidDbase,                                CEDBASEINFOEX *pNewInfo);











     Reading a Record:

 

   CEOID CeReadRecordPropsEx (HANDLE hDbase, DWORD dwFlags,                                                       LPWORD lpcPropID,                                                       CEPROPID *rgPropID, LPBYTE *lplpBuffer,                                                       LPDWORD lpcbBuffer,                                                        HANDLE hHeap);

     Writing a Record:

CEOID CeWriteRecordProps (HANDLE hDbase, CEOID oidRecord, WORD cPropID,                         CEPROPVAL * rgPropVal);






Deleting Properties, Records, and Entire Databases:

    To delete an entire record in a database, call

        BOOL CeDeleteRecord (HANDLE hDatabase, CEOID oidRecord);



    You can delete an entire database using this function:

        BOOL CeDeleteDatabaseEx (PCEGUID pguid, CEOID oid);


 


Enumerating Databases:

   HANDLE CeFindFirstDatabaseEx (PCEGUID pguid, DWORD dwDbaseType);
   CEOID CeFindNextDatabaseEx (HANDLE hEnum, PCEGUID pguid);


    Querying Object Information

    BOOL CeOidGetInfoEx2 (PCEGUID pguid, CEOID oid, CEOIDINFOEX *oidInfo);





2004年11月3日20:00:14 好累,今天先写到这里吧,有时间在丰富一下

























原创粉丝点击