⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dvdrentdlg.cpp

📁 该系统是一个集数据库操作与数据集显示的单机系统
💻 CPP
字号:
// DVDRentDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RentDVD.h"
#include "DVDRentDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDVDRentDlg dialog


CDVDRentDlg::CDVDRentDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDVDRentDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDVDRentDlg)
	m_CString_Name = _T("");
	//}}AFX_DATA_INIT
}


void CDVDRentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDVDRentDlg)
	DDX_Control(pDX, IDC_EDIT_NAME, m_Edit_Name);
	DDX_Control(pDX, IDC_DATETIMEPICKER_RENT, m_DateTimerPicker_Rent);
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl_DVDInfo);
	DDX_Text(pDX, IDC_EDIT_NAME, m_CString_Name);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDVDRentDlg, CDialog)
	//{{AFX_MSG_MAP(CDVDRentDlg)
	ON_BN_CLICKED(IDC_BUTTON_OK, OnButtonOk)
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDVDRentDlg message handlers

BOOL CDVDRentDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_ListCtrl_DVDInfo.InsertColumn(0,"ID",LVCFMT_LEFT,50,-1);
	m_ListCtrl_DVDInfo.InsertColumn(1,"DVD名称",LVCFMT_LEFT,90,-1);
	m_ListCtrl_DVDInfo.InsertColumn(2,"可借数量",LVCFMT_LEFT,80,-1);
	m_ListCtrl_DVDInfo.InsertColumn(3,"内容简介",LVCFMT_LEFT,300,-1);
	m_ListCtrl_DVDInfo.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

		HRESULT hr;
	try
	{
		hr=m_pConnection.CreateInstance(__uuidof(Connection));
		if(SUCCEEDED(hr))
		{
			m_pConnection->ConnectionString="File Name=my_data1.udl";
			m_pConnection->ConnectionTimeout=20;
			hr=m_pConnection->Open("","","",adConnectUnspecified);
			if(FAILED(hr))
			{
				AfxMessageBox("Open connection failed!");
				return TRUE;
			}
		}
		else
		{
			AfxMessageBox("Create Connection failed!");
			return TRUE;
		}
	}
	catch(_com_error e)
	{
		_bstr_t bstrSourse(e.Source());
		_bstr_t bstrDescription(e.Description());
		AfxMessageBox(bstrSourse+bstrDescription);
		return TRUE;
	}

	InitListCtr();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDVDRentDlg::InitListCtr()
{
	_RecordsetPtr pDVDRecordset;
	pDVDRecordset.CreateInstance(__uuidof(Recordset));

	try
	{
		pDVDRecordset->Open(_variant_t("tbDVDInfo"),
			m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdTable);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		return;
	}
	_variant_t var;
	CString strValue;
	int curItem = 0;
	try
	{
		while(!pDVDRecordset->ADOEOF)
		{
			var=pDVDRecordset->Fields->GetItem(long(0))->GetValue();
			if(var.vt != VT_NULL)
				strValue=(LPCSTR)_bstr_t(var);
			m_ListCtrl_DVDInfo.InsertItem(curItem,strValue);
			var=pDVDRecordset->Fields->GetItem(long(1))->GetValue();
			if(var.vt != VT_NULL)
				strValue=(LPCSTR)_bstr_t(var);
			m_ListCtrl_DVDInfo.SetItemText(curItem,1,strValue);
			var=pDVDRecordset->Fields->GetItem(long(2))->GetValue();
			if(var.vt != VT_NULL)
				strValue=(LPCSTR)_bstr_t(var);
			m_ListCtrl_DVDInfo.SetItemText(curItem,2,strValue);
			var=pDVDRecordset->Fields->GetItem(long(3))->GetValue();
			if(var.vt != VT_NULL)
				strValue=(LPCSTR)_bstr_t(var);
			m_ListCtrl_DVDInfo.SetItemText(curItem,3,strValue);

			pDVDRecordset->MoveNext();
			curItem++;
		}
	}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		pDVDRecordset->Close();
		pDVDRecordset=NULL;
}

void CDVDRentDlg::OnButtonOk() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString strDVDID,strDate,strSQL,strID;
	CTime currentTime;

	GetDlgItem(IDC_DATETIMEPICKER_RENT)->GetWindowText(strDate);
	int sel = m_ListCtrl_DVDInfo.GetSelectionMark();
	if(sel<0)
	{
		MessageBox("请选择想租赁的DVD!");
		return;
	}

	else
		strDVDID=m_ListCtrl_DVDInfo.GetItemText(sel,0);
	
//	CString temp;
//	GUID arrayGuid;
//	CoCreateGuid(&arrayGuid);
//	temp.Format("%s",arrayGuid.Data1);
//	strID+=temp;
//	currentTime.SetFormat(";
//	strID.Format("%s",currentTime);
//	MessageBox(strID);
	strID=m_ListCtrl_DVDInfo.GetItemText(sel,2);
	if(strID=='0')
	{
		MessageBox("该DVD已被借空!");
		return;
	}

	if(m_CString_Name.IsEmpty())
	{
		MessageBox("租借人姓名不能为空!","提示",MB_OK | MB_ICONINFORMATION);
		m_Edit_Name.SetFocus();
		return;
	}

	GetDlgItem(IDC_DATETIMEPICKER_RENT)->GetWindowText(strDate);
	MessageBox(strDate+strDVDID+m_CString_Name);
	_RecordsetPtr pRentRecordset;
	pRentRecordset.CreateInstance(__uuidof(Recordset)); 
	HRESULT hr;
	try
	{
		hr=pRentRecordset->Open("select * from tbRentInfo",
			m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);

		if(SUCCEEDED(hr))
		{
			pRentRecordset->AddNew();
			pRentRecordset->PutCollect("ID",_variant_t(strID));
			pRentRecordset->PutCollect("DVDID",_variant_t(strDVDID));
			pRentRecordset->PutCollect("Name",_variant_t(m_CString_Name));
			pRentRecordset->PutCollect("Date",_variant_t(strDate));
			pRentRecordset->Update();
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		return;
	}
	pRentRecordset->Close();
	pRentRecordset=NULL;
	MinusDVDNum(strDVDID);
	CDialog::OnOK();
}


void CDVDRentDlg::MinusDVDNum(CString strDVDID)
{
	_RecordsetPtr pDVDRecordset;
	pDVDRecordset.CreateInstance(__uuidof(Recordset));
	_bstr_t vSQL;
	vSQL="select * from tbDVDInfo  where DVDID="+strDVDID;
	try
	{
		pDVDRecordset->Open(vSQL,
			m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		MessageBox(e->ErrorMessage());
		return;
	}

	if((pDVDRecordset->BOF)&&(pDVDRecordset->ADOEOF))
	{
		MessageBox("error!");
		pDVDRecordset->Close();
		pDVDRecordset=NULL;
		return;
	}

	_variant_t var;
	CString strValue;
	int dvdNum;
	var=pDVDRecordset->GetCollect("Num");
	if(var.vt != VT_NULL)
	{
		strValue = (LPCSTR)_bstr_t(var);
		dvdNum=atoi(strValue);
		dvdNum-=1;
		strValue.Format("%d",dvdNum);
		pDVDRecordset->PutCollect("Num",_variant_t(strValue));
		pDVDRecordset->Update();
	}
	pDVDRecordset->Close();
	pDVDRecordset=NULL;
}


void CDVDRentDlg::OnButtonCancel() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -