?? wcedbdoc.cpp
字號:
CCeDBDatabase* pdb = new CCeDBDatabase; // use ptr to new
if(!pdb->Create((LPCTSTR)str))
FAIL_AND_EXIT;
OK;
PRINT(IDS_CHECK_EXIST);
if(CCeDBDatabase::Exists((LPCTSTR)str))
OK;
else
FAIL;
PRINT(IDS_CLOSEIT);
pdb->Close();
delete pdb;
CCeDBDatabase db; // use auto
PRINT(IDS_OPEN_AGAIN);
if(!db.Open((LPCTSTR)str))
FAIL_AND_EXIT;
OK;
PRINT(IDS_DELETEIT);
db.Delete();
PRINT_BLANK();
PRINT(IDS_DONE_BASIC);
}
void CWCEDBDoc::OnFileEnum()
{
START;
PRINT(IDS_ENUM_TEST);
PRINT_BLANK();
// Create 3 databases, inside a new scope so the db's are closed at the end of it
{
CStringArray names;
CCeDBDatabase databases[3];
names.Add(CString((LPCTSTR)IDS_HERE));
names.Add(CString((LPCTSTR)IDS_THERE));
names.Add(CString((LPCTSTR)IDS_NOWHERE));
PRINT(IDS_CREATING_DB99);
for(int n=0; n < names.GetSize(); n++)
{
LPWSTR name = (LPWSTR)(LPCTSTR)names[n]; // blah!
if(CCeDBDatabase::Exists(name))
{
CCeDBDatabase db;
db.Open(name);
db.Delete();
}
CString printLine;
printLine.LoadString(IDS_CREATING);
printLine += name;
PRINT_WSTR(printLine);
if(!databases[n].Create(name,99))
FAIL_AND_EXIT;
OK;
PRINT(IDS_SLEEPING);
Sleep(500);
}
// note that databases that go out of scope (i.e. destructed) get closed
}
// First pass: p=0: enumerates the three databases
// Subsequent passes delete the three databases (in Windows CE 2.1, the object store
// stores the databases in a different order, so multiple passes is necessary).
int nDeleted = 0;
for(int p=0; nDeleted < 3; p++)
{
PRINT_BLANK();
if(!p)
PRINT(IDS_DOING_ENUM);
else
PRINT(IDS_DOING_ENUM99);
CCeDBEnum dbEnumAll;
CCeDBEnum dbEnum(99);
CEOID CEOID;
while(CEOID = (p?dbEnum.Next():dbEnumAll.Next()))
{
CCeDBDatabase db;
WCHAR szBuf[200];
CString str;
str.LoadString(IDS_FOUND_DB);
wsprintf(szBuf,(LPCTSTR)str,CEOID);
PRINT_WSTR(szBuf);
if(!db.Open(CEOID))
FAIL_AND_EXIT;
WCHAR szName[33];
db.GetName(szName);
FILETIME FileTime = db.GetLastModified();
SYSTEMTIME SystemTime;
FileTimeToSystemTime(&FileTime, &SystemTime);
str.LoadString(IDS_NAME_SIZE);
wsprintf(szBuf,(LPCTSTR)str,
szName,db.GetSize(),db.GetIdent(),
SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond,
SystemTime.wMilliseconds);
PRINT_WSTR(szBuf);
if(p)
{
PRINT(IDS_ENUM_DELETE);
db.Delete();
nDeleted++;
}
}
}
PRINT(IDS_ENUM_DONE);
}
void CWCEDBDoc::OnFileSortTest()
{
START;
PRINT(IDS_SORT_TEST);
PRINT_BLANK();
if(!GeneratePeopleDB())
FAIL_AND_EXIT;
OK;
PRINT(IDS_PRINT_DEFAULT);
CCeDBDatabase db;
if(db.Open(DB_NAME_PEOPLE))
PrintPeopleDB(&db);
db.Close();
PRINT(IDS_NOTE);
// Note: the property has to match on both the type and the identifier in
// order for the Open() to work!
#if !(_WIN32_WCE <= 211)
PRINT_BLANK();
PRINT(IDS_PRINT_SALARY);
CCeDBProp Salary(CCeDBProp::Type_Double,PROP_SALARY);
if(db.Open(DB_NAME_PEOPLE,&Salary))
PrintPeopleDB(&db);
db.Close();
#endif // _WIN32_WCE
PRINT_BLANK();
PRINT(IDS_PRINT_AGE);
CCeDBProp Age(CCeDBProp::Type_UShort,PROP_AGE);
if(db.Open(DB_NAME_PEOPLE,&Age))
PrintPeopleDB(&db);
db.Close();
PRINT_BLANK();
PRINT(IDS_PRINT_GENDER);
CCeDBProp Gender(CCeDBProp::Type_UShort,PROP_GENDER);
if(db.Open(DB_NAME_PEOPLE,&Gender))
PrintPeopleDB(&db);
db.Close();
PRINT_BLANK();
PRINT(IDS_CHANGE_FIRST);
PRINT(IDS_PRIMARY_NOTE);
if(db.Open(DB_NAME_PEOPLE))
{
CCeDBProp sortProps[4];
int nNumSortProps;
db.GetSortProps(&nNumSortProps,sortProps);
sortProps[0].SetSortFlags(CCeDBProp::Sort_Descending);
#if (_WIN32_WCE >= 300)
db.Close();
#endif
db.SetSortProps(nNumSortProps,sortProps);
}
#if (_WIN32_WCE < 300)
db.Close();
#endif
CCeDBProp LastName(CCeDBProp::Type_String,PROP_LASTNAME);
if(db.Open(DB_NAME_PEOPLE,&LastName))
PrintPeopleDB(&db);
db.Close();
}
void CWCEDBDoc::OnFileSeekTest()
{
START;
PRINT(IDS_SEEK_TEST);
PRINT_BLANK();
if(!GeneratePeopleDB())
FAIL_AND_EXIT;
OK;
CCeDBDatabase db;
if(!db.Open(DB_NAME_PEOPLE))
FAIL_AND_EXIT;
CCeDBRecord rec;
PRINT_BLANK();
PRINT(IDS_SEEK_FIRST);
if(!db.SeekFirst() || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_SEEK_LAST);
if(!db.SeekLast() || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_SEEK_SECOND);
if(!db.SeekToIndex(1) || !db.ReadCurrRecord(&rec)) // 0-based indexing!
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_REMEMBERING);
CEOID poid = db.GetCurrRecord();
PRINT_BLANK();
PRINT(IDS_SECOND);
if(!db.SeekToIndex(1,TRUE) || !db.ReadCurrRecord(&rec)) // 0-based indexing!
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_SEEK_PREV);
if(!db.SeekPrev() || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_REMEMBERED);
if(!db.SeekToRecord(poid) || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
CString str;
str.LoadString(IDS_SMITH);
CCeDBProp lastName((LPWSTR)(LPCTSTR)str,PROP_LASTNAME);
PRINT_BLANK();
PRINT(IDS_FIRST_SMITH);
if(!db.SeekFirstEqual(lastName) || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_NEXT_SMITH);
if(!db.SeekNextEqual(lastName) || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_NEXT_SMITH2);
if(!db.SeekNextEqual(lastName) || !db.ReadCurrRecord(&rec))
PRINT(IDS_NOT_FOUND);
PRINT_BLANK();
PRINT(IDS_PREV_EVANS);
str.LoadString(IDS_EVANS);
CCeDBProp propLarger((LPWSTR)(LPCTSTR)str,PROP_LASTNAME);
if(!db.SeekValueSmaller(propLarger) || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
// Reset propLarger to be the current property.
propLarger.SetString(rec.GetPropFromIdent(PROP_LASTNAME)->GetString());
if(!db.SeekValueSmaller(propLarger) || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_NEXT_EVANS);
str.LoadString(IDS_EVANS);
CCeDBProp propSmaller((LPWSTR)(LPCTSTR)str,PROP_LASTNAME);
if(!db.SeekValueGreater(propSmaller) || !db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
PrintIndex(&db);
PrintPerson(&rec);
PRINT_BLANK();
PRINT(IDS_DONE);
}
void CWCEDBDoc::OnFileModifyTest()
{
START;
PRINT(IDS_MODIFY_TEST);
if(!GeneratePeopleDB())
FAIL_AND_EXIT;
OK;
PRINT_BLANK();
PRINT(IDS_MODIFY_INITIAL);
CCeDBDatabase db;
if(db.Open(DB_NAME_PEOPLE))
PrintPeopleDB(&db);
db.SeekFirst();
PRINT_BLANK();
PRINT(IDS_MODIFY_AGE);
CCeDBRecord rec;
CString str;
str.LoadString(IDS_CABANA);
CCeDBProp prop((LPWSTR)(LPCTSTR)str,PROP_LASTNAME);
if(!db.SeekFirstEqual(prop))
FAIL_AND_EXIT;
if(!db.ReadCurrRecord(&rec))
FAIL_AND_EXIT;
CCeDBProp* pAge = rec.GetPropFromIdent(PROP_AGE);
if(pAge == NULL)
FAIL_AND_EXIT;
pAge->SetUShort(20);
CCeDBProp ageFilter(CCeDBProp::Type_UShort,PROP_AGE);
db.WriteCurrRecord(&rec,1,&ageFilter);
PrintPeopleDB(&db);
}
void CWCEDBDoc::OnFileDeletionTest()
{
START;
PRINT(IDS_DELETION_TEST);
if(!GeneratePeopleDB())
FAIL_AND_EXIT;
OK;
PRINT_BLANK();
PRINT(IDS_INITIAL);
CCeDBDatabase db;
if(db.Open(DB_NAME_PEOPLE))
PrintPeopleDB(&db);
PRINT_BLANK();
PRINT(IDS_RECORD);
db.SeekToIndex(1);
if(!db.DeleteCurrRecord())
FAIL_AND_EXIT;
PrintPeopleDB(&db);
PRINT_BLANK();
PRINT(IDS_DELETE_FIRST);
CCeDBProp props[] = {
CCeDBProp(CCeDBProp::Type_String,PROP_FIRSTNAME),
CCeDBProp(CCeDBProp::Type_UShort,PROP_GENDER)
};
db.SeekToIndex(2);
if(!db.DeleteCurrRecordProps(2,props))
FAIL_AND_EXIT;
PrintPeopleDB(&db);
PRINT(IDS_DELETE_LAST);
CCeDBProp props2[] = {
CCeDBProp(CCeDBProp::Type_String,PROP_LASTNAME)
};
db.SeekToIndex(1);
if(!db.DeleteCurrRecordProps(1,props2))
FAIL_AND_EXIT;
PrintPeopleDB(&db);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -