//SP-A1.创建或打开数据库卷
CEGUID m_ceguid; //4个DWORD的数据组成的结构体,用来标识数据库文件位置
// PCEGUID pguid;
if(!CeMountDBVol(&m_ceguid, //数据库文件位置
L"DB", //数据卷的名称
OPEN_ALWAYS)) //操作标志
{
MessageBox(_T("Open Or Create DBVol Fail!"));
}
else
{
MessageBox(_T("Open Or Create DBVol Success!"));
}
/*
//SP-A2.枚举已装在的数据库卷
CEGUID guid;
TCHAR szVolume[10];
TCHAR szVolumef[10];
int nCnt = 0;
CREATE_INVALIDGUID(&guid);
while(CeEnumDBVolumes(&guid,szVolume,sizeof(szVolume)))
{
nCnt++;
wsprintf(szVolumef,TEXT("Mounted Vol is %s"),szVolume); //格式化输出
MessageBox(szVolumef); //弹出所有已装载数据库的名字
}
//2.弹出已装载数据库的数量nCnt
TCHAR temp[20];
TCHAR tempf[20];
_itow(nCnt,temp,10); //将i转换为字符串放入temp中,最后一个数字表示十进制
wsprintf(tempf,TEXT("The Number of Mounted Vol is %s"),temp);
MessageBox(tempf);
//SP-A3.卸载数据库卷,并重新弹出所有已装载数据库的名字及数量
CEGUID guid1;
TCHAR szVolume1[10];
int nCnt1 = 0;
CREATE_INVALIDGUID(&guid1);
CeUnmountDBVol(&m_ceguid);
while(CeEnumDBVolumes(&guid1,szVolume1,sizeof(szVolume1)))
{
nCnt1++;
MessageBox(szVolume1); //弹出所有已装载数据库的名字
}
TCHAR temp1[10];
_itow(nCnt1,temp1,10); //将i转换为字符串放入temp中,最后一个数字表示十进制
MessageBox(temp1);
*/
/**/
//SP-A4.创建数据库
// #define PID_PASSWORD MAKELONG (CEVT_LPWSTR, 1)
HANDLE m_hdb=0; //返回的已打开数据库的句柄
CEOID m_ceoid=0; //typedef DWORD CEOID;
TCHAR DBTABLENAME[20] = _T("myDB");
//1.打开数据库,并检查返回值
m_hdb = CeOpenDatabaseEx(&m_ceguid, //数据库卷的位置
&m_ceoid, //通过名称引用数据库,所以将CEOID值设为0
DBTABLENAME, //数据库名称
NULL, //
CEDB_AUTOINCREMENT, //读取一个记录后,数据库指针自增
NULL); //控制发送数据库变化消息的方式
if (m_hdb == INVALID_HANDLE_VALUE)
{
int rc;
rc = GetLastError();
TCHAR temp1[10];
_itow(rc,temp1,10);
MessageBox(temp1);
//2.如果没有该数据库,则创建个新的
if (rc == ERROR_FILE_NOT_FOUND)
{
//2.1填写数据库信息
CEDBASEINFO cedbaseinfo; //数据库信息的结构体
cedbaseinfo.dwFlags = CEDB_VALIDNAME
| CEDB_VALIDTYPE
| CEDB_VALIDSORTSPEC; //修建数据库中要用到的类型
wcscpy(cedbaseinfo.szDbaseName,DBTABLENAME);
cedbaseinfo.dwDbaseType = 0;
cedbaseinfo.wNumSortOrder = 1 ;
cedbaseinfo.rgSortSpecs[0].propid = PID_PASSWORD;
cedbaseinfo.rgSortSpecs[0].dwFlags = CEDB_SORT_CASEINSENSITIVE;
//2.2创建数据库
m_ceoid = CeCreateDatabaseEx(&m_ceguid,&cedbaseinfo);
if(m_ceoid==0)
{
MessageBox(TEXT("fail"));
}
else
{
//3创建后打开数据库
m_hdb = CeOpenDatabaseEx(&m_ceguid,
&m_ceoid,
DBTABLENAME,
NULL,
CEDB_AUTOINCREMENT,
NULL);
MessageBox(TEXT("build Database success"));
//3.1.打开数据库错误判断
if(INVALID_HANDLE_VALUE == m_hdb)
{
rc = GetLastError();
switch(rc)
{
case ERROR_INVALID_PARAMETER:
MessageBox(L"ERROR_INVALID_PARAMETER");
break;
case ERROR_FILE_NOT_FOUND:
MessageBox(L"ERROR_FILE_NOT_FOUND");
break;
case ERROR_NOT_ENOUGH_MEMORY:
MessageBox(L"ERROR_NOT_ENOUGH_MEMORY");
break;
default :
MessageBox(L"Database Opened");
break;
}
}
}
}
else //1.1打开数据库错误判断
{
switch(rc)
{
case ERROR_INVALID_PARAMETER:
MessageBox(L"ERROR_INVALID_PARAMETER");
break;
case ERROR_FILE_NOT_FOUND:
MessageBox(L"ERROR_FILE_NOT_FOUND");
break;
case ERROR_NOT_ENOUGH_MEMORY:
MessageBox(L"ERROR_NOT_ENOUGH_MEMORY");
break;
default :
MessageBox(L"Database Opened");
break;
}
}
}
//SP-A5.查找(搜索)记录
DWORD dwIndex; //索引
CEOID oid; //CEOID是DWORD
/*
@type CEOID | Unique identifier for all WINCE objects
@comm Every WINCE object can be efficiently referred to by its OID. OID's are unique
in the system and are not reused
*/
oid = CeSeekDatabase(m_hdb, //已打开数据库的句柄
CEDB_SEEK_BEGINNING, //查找数据库的第n个记录
0, //n值
&dwIndex); // receives the index from the start of the database to the beginning of the record that was found. This parameter can be NULL.
if(0 == oid)
{
MessageBox(L"there is no first item in the database!");
///insert record
CEPROPVAL * pRorps;
pRorps = new CEPROPVAL;
//
memset(pRorps,0,LocalSize(pRorps));
pRorps->propid = PID_PASSWORD;
pRorps->val.lpwstr = TEXT("SONGPENG");
oid =0;
oid = CeWriteRecordProps(m_hdb, //已打开数据库句柄
0, //为0,则创建新记录
1, //属性ID结构数组中项目的数量
pRorps); //要记录的东东
if (oid == 0)
{
MessageBox(TEXT("insert failues"));
}
else
{
MessageBox(TEXT("insert success"));
oid = CeSeekDatabase(m_hdb, //已打开数据库的句柄
CEDB_SEEK_BEGINNING, //查找数据库的第n个记录
0, //n值
&dwIndex);
if(0 != oid)
{
MessageBox(L"first item found!");
}
}
}
else
{
MessageBox(L"first item found!");
}
//SP-A6.读取记录
WORD wProps; //属性个数
PBYTE pBuff; //缓冲区
pBuff=0;
DWORD dwRecSize; //缓冲区大小
oid=CeReadRecordProps(m_hdb, //已打开数据库的句柄
CEDB_ALLOWREALLOC, //函数可以扩大缓冲区,以便容纳返回数据
&wProps, //包含了rProgID指向的CEPROPID结构的 数量
NULL, //一次读取记录所有必须属性
&(LPBYTE)pBuff, //指向缓冲区指针的指针
&dwRecSize);
if (oid == 0)
{
TCHAR szTxt[64];
INT rc = GetLastError();
wsprintf (szTxt, TEXT ("Db item not read. rc = %d (%x)"),
rc, rc);
MessageBox (szTxt);
}
else
{
PCEPROPVAL pRecord;
pRecord = (PCEPROPVAL)pBuff; //指向CEPROVAL数组
TCHAR tmp[20];
for(int i=0;i<wProps;i++)
{
switch(pRecord->propid)
{
case PID_PASSWORD:
lstrcpy(tmp,pRecord->val.lpwstr);
break;
default:
break;
}
pRecord++;
}
MessageBox(tmp);
LocalFree(pBuff);
}
//SP-A7.删除记录
oid = CeSeekDatabase(m_hdb,CEDB_SEEK_BEGINNING,0,NULL);
if( CeDeleteRecord(m_hdb,oid))
MessageBox(_T("DELE success"));
if(CloseHandle(m_hdb))
MessageBox(_T("close success"));
if(CeUnmountDBVol(&m_ceguid))
MessageBox(_T("unmount success"));
}
/*
//用到的结构体和联合体
typedef struct _CEPROPVAL {
CEPROPID propid; //属性ID
WORD wLenData; //不用
WORD wFlags; //标识
CEVALUNION val; //属性值(联合体)
} CEPROPVAL;
typedef CEPROPVAL *PCEPROPVAL;
typedef union _CEVALUNION {
short iVal;
USHORT uiVal;
long lVal;
ULONG ulVal;
FILETIME filetime;
LPWSTR lpwstr; //字符串形式
CEBLOB blob;
BOOL boolVal
double dblVal
} CEVALUNION;
typedef struct _CEDBASEINFO {
DWORD dwFlags; //@field Indicates which fields are valid. Possible values are:
// @flag CEDB_VALIDNAME | The name field is valid and should be used
// @flag CEDB_VALIDTYPE | The type field is valid and should be used
// @flag CEDB_VALIDSORTSPEC | The sortspecs are valid and should be used
WCHAR szDbaseName[CEDB_MAXDBASENAMELEN]; //@field Name of Database. Max CEDB_MAXDBASENAMELEN characters.
DWORD dwDbaseType; //@field A type ID for this database
WORD wNumRecords; //@field Number of records in the database
WORD wNumSortOrder; //@field Number of sort orders active in the database
// Maximum is CEDB_MAXSORTORDER.
DWORD dwSize; //@field Size in bytes that this database is using
FILETIME ftLastModified; //@field Last time this database was modified
SORTORDERSPEC rgSortSpecs[CEDB_MAXSORTORDER]; //@field Actual sort order descriptions.
// Only first wNumSortOrder of this array are valid.
} CEDBASEINFO, *PCEDBASEINFO;
*/