//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;
*/