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
BooleanDouble
8-byte signed value
//好啊,有CEBLOB,不错不错 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设为invalid while (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.
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 好累,今天先写到这里吧,有时间在丰富一下
本文地址:http://com.8s8s.com/it/it34375.htm