本文主要是介绍DVD出租系统【6】完成DVD系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DVD出租系统【6】完成DVD系统
本节最后目的:完成出租DVD的功能,删除出租记录功能。
1、继续打开上节的工程,双击出租DVD按钮,进行代码修改。
2、打开CRentDVD类进行初始化代码添加,当打开出租DVD时候,自动读取数据库数据,将函数添加到初始化函数中。
void CRentDVD::InitListCtrl()
{HRESULT hr;_RecordsetPtr pDVDRecordset;hr = pDVDRecordset.CreateInstance(__uuidof(Recordset));if (FAILED(hr)) {MessageBox(_T("创建记录集对象失败."));return;}try{//直接把表明作为第一个参数,记录集中是该表中的所有记录,最后一个参数传递 adCmdTablepDVDRecordset->Open(_variant_t("tbDVDInfo"), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdTable);}catch (_com_error *e){MessageBox(e->ErrorMessage());return;}_variant_t var;CString strValue;int curItem = 0;try {while (!pDVDRecordset->GetadoEof()) {var = pDVDRecordset->Fields->GetItem(long(0))->GetValue();if (var.vt != NULL)strValue = (LPCTSTR)_bstr_t(var);m_DVDInfoList.InsertItem(curItem, strValue);var = pDVDRecordset->Fields->GetItem(long(1))->GetValue();if (var.vt != NULL)strValue = (LPCTSTR)_bstr_t(var);m_DVDInfoList.SetItemText(curItem, 1, strValue);var = pDVDRecordset->Fields->GetItem(long(2))->GetValue();if (var.vt != NULL)strValue = (LPCTSTR)_bstr_t(var);m_DVDInfoList.SetItemText(curItem, 2, strValue);var = pDVDRecordset->Fields->GetItem(long(3))->GetValue();if (var.vt != NULL)strValue = (LPCTSTR)_bstr_t(var);m_DVDInfoList.SetItemText(curItem, 3, strValue);pDVDRecordset->MoveNext();curItem++;}}catch (_com_error *e){MessageBox(e->ErrorMessage());return;}pDVDRecordset->Close();pDVDRecordset = NULL;
}
3、 双击租给他按钮,编辑代码:
void CRentDVD::OnBnClickedButtonRentit()
{// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE);CString strDVDID, strDate;GetDlgItem(IDC_DATE_RENT)->GetWindowText(strDate);int sel = m_DVDInfoList.GetSelectionMark();if (sel < 0){MessageBox(_T("请选择要租借的DVD."));return;}else {strDVDID = m_DVDInfoList.GetItemText(sel, 0);}CString strLeftNum = m_DVDInfoList.GetItemText(sel, 2);int iLeftNum = _ttoi(strLeftNum);if (iLeftNum <= 0){MessageBox(_T("不好意思,该DVD已经没有存货了."));return;}if (m_rentName.IsEmpty()){MessageBox(_T("请填写租借人的姓名."), _T("提示"), MB_OK | MB_ICONINFORMATION);m_editRentName.SetFocus();return;}BOOL bRet = RentDVDByRecordset(strDVDID, m_rentName, strDate); //增加数据方法一//BOOL bRet = RentDVDByCommand(strDVDID, m_rentName, strDate); //增加数据方法二if (TRUE == bRet) {// m_bRent = TRUE;CDVDRentDlg *pParentWnd = (CDVDRentDlg *)GetParent();pParentWnd->ModDVDNum(strDVDID, -1);MessageBox(_T("租借成功!"), _T("提示"), MB_OK);}
}
4、增加数据添加函数:
//增加数据方法一
BOOL CRentDVD::RentDVDByRecordset(CString& strDVDID, CString& strRentName, CString& strRentDate)
{BOOL bRet = FALSE;_RecordsetPtr pRentRecordset;pRentRecordset.CreateInstance(__uuidof(Recordset));try {HRESULT hr = pRentRecordset->Open(_T("select * from tbRentInfo"),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);if (SUCCEEDED(hr)){pRentRecordset->AddNew();pRentRecordset->PutCollect("sDVDID", _variant_t(strDVDID));pRentRecordset->PutCollect("sName", _variant_t(strRentName));pRentRecordset->PutCollect("sDate", _variant_t(strRentDate));pRentRecordset->Update();bRet = TRUE;}}catch (_com_error *e) {MessageBox(e->ErrorMessage());}pRentRecordset->Close();pRentRecordset = NULL;return bRet;
}//增加数据方法二
BOOL CRentDVD::RentDVDByCommand(CString& strDVDID, CString& strRentName, CString& strRentDate)
{BOOL bRet = FALSE;_CommandPtr pCommand;pCommand.CreateInstance(__uuidof(Command));pCommand->ActiveConnection = m_pConnection;CString strSQL;strSQL.Format(_T("insert into tbRentInfo(sDVDID,sName,sDate) values(\'%s\',\'%s\',\'%s\')"), strDVDID, strRentName, strRentDate);pCommand->CommandText = _bstr_t(strSQL);try {HRESULT hr = pCommand->Execute(NULL, NULL, adCmdText);if (SUCCEEDED(hr)) {bRet = TRUE;}}catch (_com_error *e) {MessageBox(e->ErrorMessage());}return bRet;
}
5、注意,新的类初始化一定要初始化新传递的参数:
6、回到主界面,双击删除出租记录,编辑代码:
void CDVDRentDlg::OnBnClickedButtonDel()
{// TODO: 在此添加控件通知处理程序代码int sel = m_rentInfoList.GetSelectionMark();if (sel >= 0 && MessageBox(_T("确定删除这条记录?"), _T("提示"), MB_YESNO) == IDYES){CString rentID = m_rentInfoList.GetItemText(sel, 0);CString dvdName = m_rentInfoList.GetItemText(sel, 2);DeleteRecord(rentID, dvdName);m_rentInfoList.DeleteItem(sel);}else if (sel < 0) {MessageBox(_T("请先选中一条记录再进行删除操作。"));}
}
7、增加删除出租记录函数所用的函数:
void CDVDRentDlg::DeleteRecord(CString& rentID, CString& dvdName)
{_bstr_t vSQL;vSQL = _T("delete from tbRentInfo where sID=") + rentID;_variant_t RecordsAffected;try{HRESULT hr = m_pConnection->Execute(_bstr_t(vSQL), &RecordsAffected, adCmdText);if (SUCCEEDED(hr)){CString strDVDID = QueryDVDId(dvdName);ModDVDNum(strDVDID,1);}}catch (_com_error *e){MessageBox(e->ErrorMessage());return;}
}BOOL CDVDRentDlg::ModDVDNum(CString & strDVDID,int SDVDNum)
{HRESULT hr;_RecordsetPtr pRentRecordset;hr = pRentRecordset.CreateInstance(__uuidof(Recordset));if (FAILED(hr)) {MessageBox(_T("创建记录集对象失败."));return FALSE;}try{CString strSQL;strSQL = _T("SELECT * FROM tbDVDInfo where sDVDID=" + strDVDID);hr = pRentRecordset->Open(_variant_t(strSQL), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);if (SUCCEEDED(hr)) {_variant_t var;CString strValue;var = pRentRecordset->GetCollect(_T("sNum"));if (var.vt != NULL) {strValue = (LPCTSTR)_bstr_t(var);int num;num = _ttoi(strValue);num = num + (SDVDNum);CString DVDNumStr;DVDNumStr.Format(_T("%d"),num);BOOL bRet = FALSE;_CommandPtr pCommand;pCommand.CreateInstance(__uuidof(Command));pCommand->ActiveConnection = m_pConnection;CString strSQL;strSQL.Format(_T("update tbDVDInfo set sNum=%s where sDVDID=(\'%s\')"), DVDNumStr,strDVDID);MessageBox(strSQL);pCommand->CommandText = _bstr_t(strSQL);try {HRESULT hr = pCommand->Execute(NULL, NULL, adCmdText);if (SUCCEEDED(hr)) {bRet = TRUE;}return bRet;}catch (_com_error *e) {MessageBox(e->ErrorMessage());}}}else {MessageBox(_T("打开结果记录集失败."));return FALSE;}}catch (_com_error &e) {MessageBox(e.ErrorMessage());return FALSE;}pRentRecordset->Close();pRentRecordset = NULL;
}
8、至此功能基本完成,运行图如下:
9、文中有一些小细节没有说到,有问题的可以留言。工程地址:
链接:https://pan.baidu.com/s/1d8R9tBBnRvZwvEULuUGt7A
提取码:qh2y (本工程为完整性工程,索要密码请关注作者并联系作者QQ1967449842)
这篇关于DVD出租系统【6】完成DVD系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!