前面我已經寫過幾篇關於數據庫的文檔,這一篇還是它的後繼篇。前面都是用ODBC開發的,現在改用DAO,DAO有一個缺點,沒有同步支持ACCESS的最新版本,還要轉換到早期的版本,不知道微軟是咋搞的。廢話少說,首先看一下軟件的用法:
下面看一下開發過程:
因為科目是可以變化的,所以我沒有用一個具體的CDaoRecordset類,因此大部分的
功能是用SQL語句實現的。
1、首先看一下OnInitDialog():
BOOL CListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetWindowText("學生成績管理");
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
GetDlgItem(IDC_STRAT)->EnableWindow(FALSE);
this->Enable(FALSE);
m_pDB=new CDaoDatabase;
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind (''\\'');
sPath=sPath.Left (nPos);
nPos=sPath.ReverseFind(''\\'');
sPath=sPath.Left (nPos);
CString lpszFile = sPath + "\\db6.mdb";
try{
m_pDB->Open(lpszFile);
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,MB_ICONEXCLAMATION);
delete m_pDB;
e->Delete();
return FALSE;
}
CDaoTableDefInfo tabInfo;
int nTableDefCount = m_pDB->GetTableDefCount();
for (int i = 0; i < nTableDefCount; i++)
{
m_pDB->GetTableDefInfo(i,tabInfo);
if (tabInfo.m_lAttributes & dbSystemObject)
continue;
m_Com.AddString(tabInfo.m_strName);
}
m_pRecordSet=new CDaoRecordset(m_pDB);
m_bTF=TRUE;
m_bCR=FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
它的主要任務是打開數據庫和表的名字
2、下面是最為重要的OnStatr():
void CListDlg::OnStrat()
{
if(m_pRecordSet->IsOpen())
m_pRecordSet->Close();
m_List1.DeleteAllItems();
if(nField!=0)
{
for(long i=0;i<nField;i++)
{
m_List1.DeleteColumn(0);
}
}
m_Com.GetLBText(m_Com.GetCurSel(),m_GetString);
if(m_GetString=="關閉班級")
{
m_bTF=TRUE;
m_bCR=FALSE;
return ;
}
m_bTF=FALSE;
m_bCR=TRUE;
CString strSQL="SELECT * FROM " + m_GetString;
try{
m_pRecordSet->Open(dbOpenDynaset, strSQL);
m_pRecordSet->m_strFilter.Empty();
}
catch(CDaoException *e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,MB_ICONEXCLAMATION);
delete m_pRecordSet;
m_pDB->Close();
delete m_pDB;
e->Delete();
return ;
}
if(m_pRecordSet != NULL)
{
m_pImageList = new CImageList();
m_pImageList->Create(IDB_BITMAP4, 16, 1, RGB(255,0,0));
m_List1.SetImageList(m_pImageList, LVSIL_SMALL); /* set extended stlyes*/
DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | /*LVS_EX_SUBITEMIMAGES |*/
m_List1.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
LV_COLUMN lvColumn;
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT; lvColumn.cx = 67;
nField=m_pRecordSet->GetFieldCount();
for(int i = 0; i < nField; i++) // set up columns
{
CDaoFieldInfo m_fieldinfo;
m_pRecordSet->GetFieldInfo(i, m_fieldinfo);//get field name
int len = m_fieldinfo.m_strName.GetLength();
CString temp = m_fieldinfo.m_strName;
TCHAR* szBuffer = new TCHAR[len + 1];
strcpy(szBuffer, temp.GetBuffer(len));
temp.ReleaseBuffer();
lvColumn.pszText = szBuffer;
m_List1.InsertColumn(i, &lvColumn);//insert column
delete szBuffer;
}
/*set number of items in ListView*/
m_pRecordSet->MoveFirst();
m_pRecordSet->MoveLast();
long count =m_pRecordSet->GetRecordCount();//Get number of records
this->OnGetdispinfoList( count, nField);
}
GetDlgItem(IDC_STRAT)->EnableWindow(FALSE);
this->Enable(TRUE);
}
3、而OnGetdispinfoList(long row,long column) 最為有用,可以在很多方面調用:
void CListDlg::OnGetdispinfoList(long row,long column)
{
if(!m_pRecordSet)
return;
COleVariant varValue;
for(long i=0;iSetAbsolutePosition(i);//Set the file to desired index
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,
MB_ICONEXCLAMATION );
e->Delete();
return;
}
for(long j=0;jGetFieldValue(j, varValue);
else
m_pRecordSet->GetFieldValue(0, varValue);
}
catch(CDaoException* e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,
MB_ICONEXCLAMATION );
e->Delete();
return;
}
const VARIANT* variant = LPCVARIANT(varValue);
if(variant->vt & VT_BYREF)
return;
CString st;
switch(variant->vt)
{
case VT_ERROR:{
st="Error";
break;
}
case VT_I2:{
st.Format("%d", variant->iVal);
break;
}
case VT_I4:{
st.Format( "%d", variant->lVal);
break;
}
case VT_R4:{
st.Format( "%.2f", variant->fltVal);
break;}
case VT_R8:{ st.Format( "%.2f", variant->dblVal);
break;
}
case VT_CY:{ COleCurrency c(varValue);
st = c.Format();//ie. 1.00
break;
}
case VT_DATE:{ COleDateTime t(variant->date);
st = t.Format( "%B %d, %Y" );//Day of Week, Month Day, Year
break;
}
case VT_BSTR:{ st = V_BSTRT( &varValue );//convert BSTR to CString
break;
}
case VT_BOOL:{ if(variant->boolVal)
st="TRUE";
else
st= "FALSE";
break;
}
case VT_UI1:{st=(CString)((char*)variant->bVal);
break;
}
default:{
break;
}
}
if(j==0)
m_List1.InsertItem(i,st,0);
else
m_List1.SetItemText(i,j,st);
}
}
}
其它的可以看源碼。
注意: