?? rentdvddlg.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 + -