www.pudn.com > 4-5cha.rar > RentDVDDlg.cpp


// RentDVDDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "rent.h" 
#include "RentDVDDlg.h" 
#include "rentdlg.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CRentDVDDlg dialog 
 
 
CRentDVDDlg::CRentDVDDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CRentDVDDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CRentDVDDlg) 
	m_name = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
 
void CRentDVDDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CRentDVDDlg) 
	DDX_Control(pDX, IDC_DATETIMEPICKER_RENT, m_DateTimeRent); 
	DDX_Control(pDX, IDC_EDIT_NAME, m_editName); 
	DDX_Control(pDX, IDC_LIST_DVD, m_DVDInfoList); 
	DDX_Text(pDX, IDC_EDIT_NAME, m_name); 
	DDV_MaxChars(pDX, m_name, 8); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CRentDVDDlg, CDialog) 
	//{{AFX_MSG_MAP(CRentDVDDlg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CRentDVDDlg message handlers 
 
BOOL CRentDVDDlg::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	// TODO: Add extra initialization here 
	//初始化List控件的栏目 
    m_DVDInfoList.InsertColumn(0,"ID",LVCFMT_LEFT,40,-1); 
	m_DVDInfoList.InsertColumn(1,"影碟",LVCFMT_LEFT,120,-1); 
	m_DVDInfoList.InsertColumn(2,"目前数量",LVCFMT_LEFT,80,-1); 
	m_DVDInfoList.InsertColumn(3,"内容描述",LVCFMT_LEFT,170,-1); 
    //选中时是完全行 
    m_DVDInfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT| LVS_EX_GRIDLINES); 
    //初始化数据库连接 
	m_pConnection.CreateInstance(__uuidof(Connection)); 
	try 
	{   m_pConnection->ConnectionString="File Name=my_data1.udl"; 
		m_pConnection->Open("","","",adModeUnknown); 
	} 
    catch (_com_error e)  
	{ 
		AfxMessageBox("数据库连接失败!"); 
		return FALSE; 
    } 
 
    //初始化List控件中的数据,显示所有DVD的信息 
	InitListCtr(); 
 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CRentDVDDlg::InitListCtr() 
{ 
	_RecordsetPtr	pDVDRecordset; 
    pDVDRecordset.CreateInstance(__uuidof(Recordset)); 
 
	CString strSql; 
	strSql.Format("SELECT * FROM tbDVDInfo"); 
	try 
	{   //直接把表名作为第一个参数,记录集中是该表中所有记录 
		//注意最后一个参数此时应该是“adCmdTable” 
		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; 
	pDVDRecordset->MoveFirst(); 
	while(!pDVDRecordset->adoEOF) 
	{ 
	 
		var=pDVDRecordset->Fields->GetItem(long(0))->GetValue(); 
		if(var.vt != VT_NULL) 
			strValue = (LPCSTR)_bstr_t(var); 
        m_DVDInfoList.InsertItem(curItem,strValue); 
		 
		var=pDVDRecordset->Fields->GetItem(long(1))->GetValue(); 
		if(var.vt != VT_NULL) 
			strValue = (LPCSTR)_bstr_t(var); 
		m_DVDInfoList.SetItemText(curItem,1,strValue); 
		 
		var=pDVDRecordset->Fields->GetItem(long(2))->GetValue(); 
		if(var.vt != VT_NULL) 
			strValue = (LPCSTR)_bstr_t(var); 
        m_DVDInfoList.SetItemText(curItem,2,strValue); 
		 
		var=pDVDRecordset->Fields->GetItem(long(3))->GetValue(); 
		if(var.vt != VT_NULL) 
			strValue = (LPCSTR)_bstr_t(var); 
        m_DVDInfoList.SetItemText(curItem,3,strValue); 
		 
		pDVDRecordset->MoveNext(); 
		curItem++; 
		 
	} 
    pDVDRecordset->Close(); 
	pDVDRecordset=NULL; 
} 
 
/*使用记录集对象实现插入记录*/ 
/* 
void CRentDVDDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
	//得到输入 
	UpdateData(TRUE); 
    CString strDVDID,strDate; 
	 
	//获得日期 
    GetDlgItem(IDC_DATETIMEPICKER_RENT)->GetWindowText(strDate); 
	 
	int sel=m_DVDInfoList.GetSelectionMark(); 
	if(sel<0) 
	{ 
		MessageBox("请选择租借的DVD."); 
		return; 
	} 
	//获得DVDID,在list控件的第一列(序号0)  
	else strDVDID=m_DVDInfoList.GetItemText(sel,0); 
 
	//获得当前数目,判断是否为0 
    if(m_DVDInfoList.GetItemText(sel,2)=='0') 
	{ 
		MessageBox("抱歉,该影碟已经借空!"); 
		return; 
	} 
	 
	if(m_name.IsEmpty()) 
	{ 
		MessageBox("请填写租借人姓名!","提示",MB_OK|MB_ICONINFORMATION); 
		m_editName.SetFocus(); 
		return; 
	} 
 
    
	_RecordsetPtr	pRentRecordset; 
    pRentRecordset.CreateInstance(__uuidof(Recordset)); 
	 
	HRESULT  hr; 
    try 
	{ 
		hr=pRentRecordset->Open("select * from tbRentInfo", 
		                 m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针 
	                  	 adOpenDynamic, 
	                	 adLockOptimistic, 
		                 adCmdText); 
         
		if(SUCCEEDED(hr)) 
		{ 
			pRentRecordset->AddNew(); 
			pRentRecordset->PutCollect("DVDID",_variant_t(strDVDID)); 
			pRentRecordset->PutCollect("Name",_variant_t(m_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(); 
}*/ 
 
/*使用Command对象插入记录*/ 
void CRentDVDDlg::OnOK()  
{ 
	//得到输入 
	UpdateData(TRUE); 
    CString strDVDID,strDate,strSQL; 
	 
	//获得日期 
    GetDlgItem(IDC_DATETIMEPICKER_RENT)->GetWindowText(strDate); 
	 
	int sel=m_DVDInfoList.GetSelectionMark(); 
	if(sel<0) 
	{ 
		MessageBox("请选择租借的DVD."); 
		return; 
	} 
	//获得DVDID,在list控件的第一列(序号0)  
	else strDVDID=m_DVDInfoList.GetItemText(sel,0); 
	 
	//获得当前数目,判断是否为0 
    if(m_DVDInfoList.GetItemText(sel,2)=='0') 
	{ 
		MessageBox("抱歉,该影碟已经借空!"); 
		return; 
	} 
	 
	if(m_name.IsEmpty()) 
	{ 
		MessageBox("请填写租借人姓名!","提示",MB_OK|MB_ICONINFORMATION); 
		m_editName.SetFocus(); 
		return; 
	} 
	_CommandPtr pCommand; 
	pCommand.CreateInstance (__uuidof (Command)); 
    pCommand->ActiveConnection =m_pConnection; 
	 
	strSQL.Format("insert into tbRentInfo(DVDID,Name,Date) values(%s,'%s','%s')",strDVDID,m_name,strDate); 
    pCommand->CommandText =_bstr_t(strSQL); 
	 
 
	HRESULT  hr; 
    try 
	{ 
		hr=pCommand->Execute(NULL,NULL,adCmdText); 
		if(SUCCEEDED(hr)) 
			MinusDVDNum(strDVDID); 
        		 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
		return; 
	}   
		 
	CDialog::OnOK(); 
} 
void CRentDVDDlg::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(),	 // 获取库接库的IDispatch指针 
			adOpenDynamic, 
			adLockOptimistic, 
			adCmdText); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(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; 
}