?? cedbusedlg.cpp
字號:
// CEDBUseDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CEDBUse.h"
#include "CEDBUseDlg.h"
#include "StudentInputDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCEDBUseDlg dialog
CCEDBUseDlg::CCEDBUseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCEDBUseDlg::IDD, pParent)
{
//初始化數(shù)據(jù)庫句柄
m_hDB = 0;
//將數(shù)據(jù)庫對象標識設(shè)置0
m_ceOid = 0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCEDBUseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCEDBUseDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCEDBUseDlg, CDialog)
//{{AFX_MSG_MAP(CCEDBUseDlg)
ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
ON_BN_CLICKED(IDC_BTNREFRESH, OnBtnrefresh)
ON_BN_CLICKED(IDC_BTNADD, OnBtnadd)
ON_BN_CLICKED(IDC_BTNEDIT, OnBtnedit)
ON_BN_CLICKED(IDC_BTNDELETE, OnBtndelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCEDBUseDlg message handlers
BOOL CCEDBUseDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
//設(shè)置學生列表框標題
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LST_STUDENT);
CRect rt;
pListCtrl->GetClientRect(&rt);
pListCtrl->InsertColumn(0,_T("編號"), LVCFMT_LEFT, rt.Width() * 0.2);
pListCtrl->InsertColumn(1,_T("姓名"), LVCFMT_LEFT, rt.Width() * 0.3);
pListCtrl->InsertColumn(2, _T("出生日期"), LVCFMT_LEFT, rt.Width() * 0.3);
pListCtrl->InsertColumn(3, _T("身高"), LVCFMT_LEFT, rt.Width() * 0.2);
return TRUE; // return TRUE unless you set the focus to a control
}
//打開數(shù)據(jù)庫
void CCEDBUseDlg::OnBtnopen()
{
//1、掛載數(shù)據(jù)庫卷,如果存在則打開,不存在,就新建一個
if (!CeMountDBVol(&m_ceGuid,DBFILENAME,OPEN_ALWAYS))
{
AfxMessageBox(_T("打開或新建數(shù)據(jù)卷失敗"));
return;
}
//2、接著打開數(shù)據(jù)庫
m_hDB = CeOpenDatabaseEx(&m_ceGuid,&m_ceOid,DBTABLENAME,NULL,CEDB_AUTOINCREMENT,NULL);
if (m_hDB == INVALID_HANDLE_VALUE)
{
//3、 //如果數(shù)據(jù)庫不存在,就新建之
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
CEDBASEINFO ceDbInfo;
ceDbInfo.dwFlags = CEDB_VALIDNAME | CEDB_VALIDTYPE | CEDB_VALIDSORTSPEC ;
wcscpy(ceDbInfo.szDbaseName , DBTABLENAME);
ceDbInfo.dwDbaseType = 0;
ceDbInfo.wNumSortOrder = 2 ; //排序字段數(shù)目
ceDbInfo.rgSortSpecs[0].propid = PID_NO;
ceDbInfo.rgSortSpecs[0].dwFlags = CEDB_SORT_CASEINSENSITIVE; //升序,且大小寫無關(guān)
ceDbInfo.rgSortSpecs[1].propid = PID_NAME;
ceDbInfo.rgSortSpecs[1].dwFlags = CEDB_SORT_CASEINSENSITIVE; //升序,且大小寫無關(guān)
m_ceOid = CeCreateDatabaseEx(&m_ceGuid,&ceDbInfo);
if (m_ceOid == 0)
{
AfxMessageBox(_T("創(chuàng)建數(shù)據(jù)庫失敗"));
//此處得卸載數(shù)據(jù)庫卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸載數(shù)據(jù)庫文件卷失敗"));
}
return ;
}
//4、創(chuàng)建數(shù)據(jù)庫后,應緊接著打開數(shù)據(jù)庫
m_hDB = CeOpenDatabaseEx(&m_ceGuid,&m_ceOid,DBTABLENAME,NULL,CEDB_AUTOINCREMENT,NULL);
if (m_hDB == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("打開數(shù)據(jù)庫失敗"));
//此處得卸載數(shù)據(jù)庫卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸載數(shù)據(jù)庫文件卷失敗"));
}
return ;
}
}
else
{
AfxMessageBox(_T("打開數(shù)據(jù)庫失敗"));
//此處得卸載數(shù)據(jù)庫卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸載數(shù)據(jù)庫文件卷失敗"));
}
return ;
}
}
}
//關(guān)閉數(shù)據(jù)庫
void CCEDBUseDlg::OnBtnclose()
{
//1、關(guān)閉數(shù)據(jù)庫
if (!CloseHandle(m_hDB))
{
AfxMessageBox(_T("關(guān)閉數(shù)據(jù)庫失敗"));
return;
}
//2、將數(shù)據(jù)庫卷的數(shù)據(jù)緩沖到永久存儲介質(zhì)上
if (!CeFlushDBVol(&m_ceGuid))
{
AfxMessageBox(_T("緩沖介質(zhì)失敗"));
return ;
}
//3、卸載數(shù)據(jù)庫卷
if (!CeUnmountDBVol(&m_ceGuid))
{
AfxMessageBox(_T("卸載數(shù)據(jù)庫文件卷失敗"));
return ;
}
}
/*
函數(shù)說明:獲取數(shù)據(jù)庫的記錄數(shù)目
入口參數(shù):pCeGuid : 數(shù)據(jù)庫文件卷標識指針
ceOid : 數(shù)據(jù)庫對象標識
出口參數(shù):(無)
返 回 值:-1 :代表失敗。成功的話,返回實際的記錄數(shù)
*/
int CCEDBUseDlg::GetRecordCount(CEGUID *pCeGuid,CEOID ceOid)
{
int iCount;
CEOIDINFO oidinfo;
if (!CeOidGetInfoEx(pCeGuid,ceOid,&oidinfo))
{
AfxMessageBox(_T("獲取信息失敗"));
return -1;
}
iCount = oidinfo.infDatabase.wNumRecords;
return iCount;
}
//從數(shù)據(jù)庫中獲取所有記錄,并顯示到列表視圖中
void CCEDBUseDlg::OnBtnrefresh()
{
CEOID ceOid;
WORD wProps;
DWORD dwRecSize;
PBYTE pBuff;
PCEPROPVAL pRecord;
REC_STUDENT * pStudent;
//得到數(shù)據(jù)庫記錄數(shù)
int iRecordCount = GetRecordCount(&m_ceGuid,m_ceOid);
DWORD dwIndex;
//設(shè)置學生列表框標題
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LST_STUDENT);
pListCtrl->DeleteAllItems();
for (int k=0;k<iRecordCount;k++)
{
//移動記錄指針
ceOid = CeSeekDatabase(m_hDB,CEDB_SEEK_BEGINNING,k,&dwIndex);
ASSERT(ceOid !=0);
pBuff = 0;
//讀取所有字段值
ceOid = CeReadRecordProps(m_hDB,CEDB_ALLOWREALLOC,&wProps,NULL,&(LPBYTE)pBuff,&dwRecSize);
ASSERT(ceOid != 0);
pRecord = (PCEPROPVAL)pBuff;
pStudent = new REC_STUDENT;
for (int i=0;i<wProps;i++)
{
switch(pRecord->propid)
{
case PID_NO:
{
wcscpy(pStudent->szNo,pRecord->val.lpwstr);
break;
}
case PID_NAME:
{
wcscpy(pStudent->szName,pRecord->val.lpwstr);
break;
}
case PID_BIRTHDAY:
{
pStudent->ftBirthday = pRecord->val.filetime;
break;
}
case PID_STATURE:
{
pStudent->iStature = pRecord->val.lVal;
break;
}
}
pRecord++;
}
LocalFree(pBuff);
//向列表框中添加學生信息
pListCtrl->InsertItem(k,_T("Test"));
//添加學生編號
pListCtrl->SetItemText(k,0,pStudent->szNo);
//添加學生姓名
pListCtrl->SetItemText(k,1,pStudent->szName);
//添加學生生日,此處需要做些轉(zhuǎn)換
SYSTEMTIME systime;
FileTimeToSystemTime(&(pStudent->ftBirthday),&systime);
TCHAR szBirthday[11];
swprintf(szBirthday,_T("%d-%d-%d"),systime.wYear,systime.wMonth,systime.wDay);
pListCtrl->SetItemText(k,2,szBirthday);
//添加學生身高,此處需要將數(shù)字轉(zhuǎn)換成字符串
TCHAR szStature[5];
_itow(pStudent->iStature,szStature,10);
pListCtrl->SetItemText(k,3,szStature);
delete pStudent;
}
}
/*
函數(shù)說明:添加一條新記錄
入口參數(shù):stu : 學生數(shù)據(jù)庫表結(jié)構(gòu)
出口參數(shù):(無)
返 回 值:TRUE:添加成功;FALSE:添加失敗
*/
bool CCEDBUseDlg::AddNewStudent(REC_STUDENT stu)
{
CEOID ceOid;
CEPROPVAL *pProps;
pProps = new CEPROPVAL[4];
//學生學號
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_NO;
pProps->val.lpwstr = stu.szNo;
//學生姓名
pProps++;
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_NAME ;
pProps->val.lpwstr = stu.szName;
//學生出生日期
pProps++;
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_BIRTHDAY;
pProps->val.filetime = stu.ftBirthday;
//學生身高
pProps++;
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_STATURE;
pProps->val.iVal = stu.iStature;
pProps = pProps -3 ;
ceOid = CeWriteRecordProps(m_hDB,0,4,pProps);
if (ceOid == 0)
{
return false;
}
return true;
}
/*
函數(shù)說明:編輯記錄
入口參數(shù):stu : 學生數(shù)據(jù)庫表結(jié)構(gòu)
ceOid : 記錄對象標識
出口參數(shù):(無)
返 回 值:TRUE:編輯成功;FALSE:編輯失敗
*/
bool CCEDBUseDlg::EditStudent(REC_STUDENT stu,CEOID ceOid)
{
CEOID tmpCeOid;
CEPROPVAL *pProps;
pProps = new CEPROPVAL[4];
//學生學號
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_NO;
pProps->val.lpwstr = stu.szNo;
//學生姓名
pProps++;
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_NAME ;
pProps->val.lpwstr = stu.szName;
//學生出生日期
pProps++;
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_BIRTHDAY;
pProps->val.filetime = stu.ftBirthday;
//學生身高
pProps++;
memset(pProps,0,LocalSize(pProps));
pProps->propid = PID_STATURE;
pProps->val.iVal = stu.iStature;
pProps = pProps -3 ;
tmpCeOid = CeWriteRecordProps(m_hDB,ceOid,4,pProps);
if (tmpCeOid == 0)
{
return false;
}
return true;
}
//添加記錄按鈕單擊實現(xiàn)方法
void CCEDBUseDlg::OnBtnadd()
{
REC_STUDENT rec_stu;
CStudentInputDlg inputDlg;
if (inputDlg.DoModal() == IDOK)
{
//得到編號
wcscpy(rec_stu.szNo,LPCTSTR(inputDlg.m_no));
//得到姓名
wcscpy(rec_stu.szName,LPCTSTR(inputDlg.m_name));
//得到出生日期
SYSTEMTIME timeDest;
inputDlg.m_birthday.GetAsSystemTime(timeDest);
::SystemTimeToFileTime(&timeDest, &(rec_stu.ftBirthday));
//得到身高值
rec_stu.iStature = inputDlg.m_stature;
AddNewStudent(rec_stu);
}
//添加完成之后,調(diào)用刷新按鈕單擊方法
OnBtnrefresh();
}
//編輯記錄按鈕單擊實現(xiàn)方法
void CCEDBUseDlg::OnBtnedit()
{
REC_STUDENT rec_stu;
CEOID ceOid;
PBYTE pBuff;
WORD wProps;
DWORD dwRecSize;
PCEPROPVAL pRecord;
REC_STUDENT *pStudent;
//設(shè)置學生列表框標題
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LST_STUDENT);
int iItemIndex = pListCtrl->GetNextItem(-1,LVNI_SELECTED);
ASSERT(iItemIndex !=-1);
DWORD dwIndex;
ceOid = CeSeekDatabase(m_hDB,CEDB_SEEK_BEGINNING,iItemIndex,&dwIndex);
ASSERT(ceOid !=0);
pBuff = 0;
//讀取所有記錄值
ceOid = CeReadRecordProps(m_hDB,CEDB_ALLOWREALLOC,&wProps,NULL,&(LPBYTE)pBuff,&dwRecSize);
ASSERT(ceOid != 0);
pRecord = (PCEPROPVAL)pBuff;
pStudent = new REC_STUDENT;
for (int i=0;i<wProps;i++)
{
switch(pRecord->propid)
{
case PID_NO:
{
wcscpy(pStudent->szNo,pRecord->val.lpwstr);
break;
}
case PID_NAME:
{
wcscpy(pStudent->szName,pRecord->val.lpwstr);
break;
}
case PID_BIRTHDAY:
{
pStudent->ftBirthday = pRecord->val.filetime;
break;
}
case PID_STATURE:
{
pStudent->iStature = pRecord->val.lVal;
break;
}
}
pRecord++;
}
LocalFree(pBuff);
CStudentInputDlg inputDlg;
//同步編輯對話框輸入框值
inputDlg.m_no = pStudent->szNo;
inputDlg.m_name = pStudent->szName;
SYSTEMTIME tmpTime;
FileTimeToSystemTime(&(pStudent->ftBirthday),&tmpTime);
inputDlg.m_birthday = tmpTime;
inputDlg.m_stature = pStudent->iStature;
delete pStudent;
if (inputDlg.DoModal() == IDOK)
{
//得到編號
wcscpy(rec_stu.szNo,LPCTSTR(inputDlg.m_no));
//得到姓名
wcscpy(rec_stu.szName,LPCTSTR(inputDlg.m_name));
//得到出生日期
SYSTEMTIME timeDest;
inputDlg.m_birthday.GetAsSystemTime(timeDest);
::SystemTimeToFileTime(&timeDest, &(rec_stu.ftBirthday));
//得到身高值
rec_stu.iStature = inputDlg.m_stature;
EditStudent(rec_stu,ceOid);
}
//編輯完成之后,調(diào)用刷新按鈕單擊方法
OnBtnrefresh();
}
//刪除當前記錄
void CCEDBUseDlg::OnBtndelete()
{
//設(shè)置學生列表框標題
CListCtrl * pListCtrl = (CListCtrl*)GetDlgItem(IDC_LST_STUDENT);
int iItemIndex = pListCtrl->GetNextItem(-1,LVNI_SELECTED);
ASSERT(iItemIndex !=-1);
DWORD dwIndex;
CEOID ceOid;
ceOid = CeSeekDatabase(m_hDB,CEDB_SEEK_BEGINNING,iItemIndex,&dwIndex);
ASSERT(ceOid !=0 );
if (!CeDeleteRecord(m_hDB,ceOid))
{
AfxMessageBox(_T("刪除失敗"));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -