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

📄 rentdvddlg.cpp

📁 影碟出租系统~vc++进行数据库sql管理的经典程序..已调好
💻 CPP
字号:
// RentDVDDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "rent.h"
#include "RentDVDDlg.h"


// CRentDVDDlg 对话框

IMPLEMENT_DYNAMIC(CRentDVDDlg, CDialog)

CRentDVDDlg::CRentDVDDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRentDVDDlg::IDD, pParent)
	, m_name(_T(""))
{

}

CRentDVDDlg::~CRentDVDDlg()
{
}

void CRentDVDDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_DVDinfoList);
	DDX_Control(pDX, IDC_EDIT1, m_editName);
	DDX_Control(pDX, IDC_DATETIMEPICKER1, m_DateTimeRent);
	DDX_Text(pDX, IDC_EDIT1, m_name);
}


BEGIN_MESSAGE_MAP(CRentDVDDlg, CDialog)
	//ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CRentDVDDlg::OnLvnItemchangedList1)
	ON_BN_CLICKED(IDOK, &CRentDVDDlg::OnBnClickedOk)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CRentDVDDlg::OnLvnItemchangedList1)
	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIMEPICKER1, &CRentDVDDlg::OnDtnDatetimechangeDatetimepicker1)
END_MESSAGE_MAP()


// CRentDVDDlg 消息处理程序

BOOL CRentDVDDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// TODO:  在此添加额外的初始化  
  m_DVDinfoList.InsertColumn(0,"车主ID",LVCFMT_LEFT,40,-1);
  m_DVDinfoList.InsertColumn(1,"车牌号",LVCFMT_LEFT,40,-1);
  m_DVDinfoList.InsertColumn(2,"车主姓名",LVCFMT_LEFT,120,-1);
  m_DVDinfoList.InsertColumn(3,"身份证号",LVCFMT_LEFT,80,-1);
  m_DVDinfoList.InsertColumn(4,"车型",LVCFMT_LEFT,170,-1);
  m_DVDinfoList.InsertColumn(5,"预存金额",LVCFMT_LEFT,170,-1);

  m_DVDinfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  HRESULT hr;
	
	 try
	 {
		 //实例化连接对象
	   hr=m_pConnection.CreateInstance(_uuidof(Connection));
       if(SUCCEEDED(hr))
       //设置连接串属性为UDL文件
	   {//m_pConnection->ConnectionString="File Name=my_data1.udl";
       //m_pConnection->ConnectionString="Provider=SQLOLEDB;DataSource=local;InitialCatalog=影碟出租系统;User ID=sa;Password=123456;";
     //_bstr_t strConnect = "Provider=SQLOLEDB; Server=(local); Database=影碟出租系统; uid=sa; pwd=123456;";

	   m_pConnection->ConnectionTimeout=20;
    m_pConnection->Open("Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa1;Password=123456;Initial Catalog=车牌识别系统;Data Source=(local);","","",adModeUnknown);

	   
       //设置等待连接打开时间为20s
	  
	    
	  // hr=m_pConnection->Open("","","",adModeReadWrite);//adConnectUnspecified
	  
	   if(FAILED(hr))
	     {
		  AfxMessageBox("open fail!");
		  return TRUE;
	     }
	   }
	   else
	   {AfxMessageBox("createinstance of Connection fail!");
	   return TRUE;
	   }
	 }
	  catch(_com_error e)
	 {//给出异常消息
	 _bstr_t bstrSource(e.Source());
     _bstr_t bstrDescription(e.Description());
     AfxMessageBox(bstrSource+bstrDescription);
    CString errormessage;  
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());  
     AfxMessageBox(errormessage);///显示错误信息 
    // Connection.Close(); 
	 return TRUE;
	 
	 }
 
  InitListCtr1();
  
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
void CRentDVDDlg::InitListCtr1()
{
	_RecordsetPtr	pDVDRecordset;
    pDVDRecordset.CreateInstance(__uuidof(Recordset));
  
	try
	{   //直接把表名作为第一个参数,记录集中是该表中所有记录
		//注意最后一个参数此时应该是“adCmdTable
          pDVDRecordset->Open(_variant_t("车主信息"), 
	       m_pConnection.GetInterfacePtr(),	
			adOpenDynamic,
			adLockOptimistic,
			/*adCmdText);*/
			adCmdTable);
	}
	catch(_com_error *e)
	{ 
		AfxMessageBox(e->ErrorMessage());
		
   /*CString errormessage;  
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());  
     AfxMessageBox(errormessage);///显示错误信息 */
   
	// return TRUE;
		return;
	}  
    _variant_t var;
	CString strValue;
	int curItem=0;
	//pDVDRecordset->MoveFirst();
	try{
	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);

		var=pDVDRecordset->Fields->GetItem(long(4))->GetValue();
		if(var.vt != VT_NULL)
			strValue = (LPCSTR)_bstr_t(var);
        m_DVDinfoList.SetItemText(curItem,4,strValue);

		var=pDVDRecordset->Fields->GetItem(long(5))->GetValue();
		if(var.vt != VT_NULL)
			strValue = (LPCSTR)_bstr_t(var);

        m_DVDinfoList.SetItemText(curItem,5,strValue);
		pDVDRecordset->MoveNext();
		curItem++;
		
	}
	}
	catch(_com_error *e)
		{
		AfxMessageBox(e->ErrorMessage());
		
	}  
    pDVDRecordset->Close();
	pDVDRecordset=NULL;
}
//void CRentDVDDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
//{
//	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
//	// TODO: 在此添加控件通知处理程序代码
//	*pResult = 0;
//}

void CRentDVDDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	OnOK();
}
////void CRentDVDDlg::OnOK() 
////{
////	//得到输入
////	UpdateData(TRUE);
////    CString strDVDID,strDate,strSQL;
////	
////	//获得日期
////    GetDlgItem(IDC_DATETIMEPICKER1)->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->adoBOF)&&(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 CRentDVDDlg::OnOK() 
{
	// TODO: Add extra validation here
	//得到输入
	UpdateData(TRUE);
    CString strDVDID,strDate;
	
	//获得日期
    GetDlgItem(IDC_DATETIMEPICKER1)->GetWindowText(strDate);
	
	int sel=m_DVDinfoList.GetSelectionMark();
	if(sel<0)
	{
		MessageBox("请选择车主.");
		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));
//"+m_name+" where 车主ID="+strDVDID
	_bstr_t vSQL;
	_variant_t RecordsAffected;
	vSQL="update 车主信息 set 预存金额 = 预存金额 + "+m_name+" where 车主ID = "+strDVDID;
//MessageBox(vSQL,"提示",MB_OK|MB_ICONINFORMATION);
	_variant_t var;
	CString strValue;
	_RecordsetPtr	pDVDRecordset;
    pDVDRecordset.CreateInstance(__uuidof(Recordset));
	//获得影碟编号DVDID
	try
	{	m_pConnection->BeginTrans();

		m_pConnection->Execute(vSQL,&RecordsAffected,adCmdText);

		m_pConnection->CommitTrans();
	}
	catch(_com_error *e)
	{
		m_pConnection->RollbackTrans();
		AfxMessageBox(e->ErrorMessage());
	}

		/*hr=pRentRecordset->Open("select * from 车主信息",
		                 m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针
	                  	 adOpenDynamic,
	                	 adLockOptimistic,
		                 adCmdText);

        
		if(SUCCEEDED(hr))
		{  //m_pConnection->BeginTrans();
			pRentRecordset->AddNew();
			pRentRecordset->PutCollect("DVDID",_variant_t(strDVDID));
			pRentRecordset->PutCollect("Name",_variant_t(m_name));
			pRentRecordset->PutCollect("date",_variant_t(strDate));
			pRentRecordset->Update();
		} //m_pConnection->CommitTrans();*/

	/*catch(_com_error *e)
	{ //m_pConnection->RollbackTrans();
		AfxMessageBox(e->ErrorMessage());
		return;
	}  */
	
	m_pConnection->Close();
	m_pConnection=NULL;

	//MinusDVDNum(strDVDID);
  
	CDialog::OnOK();
}

void CRentDVDDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}

void CRentDVDDlg::OnDtnDatetimechangeDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}

⌨️ 快捷键说明

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