deptment.cpp
来自「这是一个不错的商店管理系统」· C++ 代码 · 共 405 行
CPP
405 行
// deptment.cpp : implementation file
//
#include "stdafx.h"
#include "shop.h"
#include "deptment.h"
#include "deptmentquery.h"
#include <time.h>
#include <fstream.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CShopApp theApp;
struct tm *ptrdeptment;
/////////////////////////////////////////////////////////////////////////////
// Cdeptment dialog
Cdeptment::Cdeptment(CWnd* pParent /*=NULL*/)
: CDialog(Cdeptment::IDD, pParent)
{
//{{AFX_DATA_INIT(Cdeptment)
m_vdeptname = _T("");
m_vdeptno = _T("");
m_vmanagername = _T("");
//}}AFX_DATA_INIT
}
void Cdeptment::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Cdeptment)
DDX_Control(pDX, IDC_USERLIST, m_userlist);
DDX_Text(pDX, IDC_deptname, m_vdeptname);
DDX_Text(pDX, IDC_deptno, m_vdeptno);
DDX_Text(pDX, IDC_managername, m_vmanagername);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Cdeptment, CDialog)
//{{AFX_MSG_MAP(Cdeptment)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_adddeptment, Onadddeptment)
ON_BN_CLICKED(IDC_deletedeptment, Ondeletedeptment)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_USERLIST, OnItemchangedUserlist)
ON_BN_CLICKED(IDC_modifydeptment, Onmodifydeptment)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Cdeptment message handlers
void Cdeptment::OnButton4()
{
CDialog::OnOK();
// TODO: Add your control notification handler code here
}
BOOL Cdeptment::OnInitDialog()
{
CDialog::OnInitDialog();
time_t lt;
lt = time(NULL);
ptrdeptment = localtime(<);
::SendMessage(m_userlist.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);//当被选中时,所有子项都处于选中状态
//////////为列表控件添加列//////////
m_userlist.InsertColumn(0,"部门编号",LVCFMT_LEFT,100);
m_userlist.InsertColumn(1,"部门名称",LVCFMT_LEFT,100);
m_userlist.InsertColumn (2,"部门经理",LVCFMT_LEFT,100);
//////////读取数据库中的信息添加到列表控件///////////
displayintheuserlist();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void Cdeptment::Onadddeptment()
{
//bool judge;
//judge = false;
UpdateData(true);//把控件的值传给相应的变量
if (m_vdeptno == "" || m_vdeptname == "")
{
AfxMessageBox("部门编号或部门名称不能为空!!");
goto tiaozhuan;
}
//UpdateData(true);//把控件的值传给相应的变量
//m_pRecordset1->Close();
//m_pRecordset1 = NULL;
m_pRecordset.CreateInstance(_uuidof(Recordset));//
m_pRecordset->Open("select * from deptment",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
try
{
m_pRecordset->AddNew();
vdeptno = m_vdeptno;
vdeptname = m_vdeptname;
vmanagername = m_vmanagername;
//UpdateData(true);
m_pRecordset->PutCollect("deptno",vdeptno);
m_pRecordset->PutCollect("deptname",vdeptname);
m_pRecordset->PutCollect("managername",vmanagername);
//UpdateData(true);
//m_pRecordset.CreateInstance("ADODB.Recordset");
//m_pRecordset->Open("insert into employee values(empno,empname,sex,salary,duty,deptno,birthday,address,telephone,remark) ",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
//AfxMessageBox("添加记录成功!");
m_pRecordset->Update();
m_pRecordset->Close();
m_pRecordset = NULL;
//重建指针,以备下次添加记录时使用
//m_pRecordset1.CreateInstance(_uuidof(Recordset));//
//m_pRecordset1->Open("select * from employee",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
displayintheuserlist();
AfxMessageBox("添加记录成功!");
ofstream out("databf\\diary.txt",ios::app);
out<<asctime(ptrdeptment)<<"||..."<<"成功执行了向部门表中添加数据:"<<"部门编号:"<<m_vdeptno<<" 部门名称:"<<m_vdeptname<<" 部门经理: "+m_vmanagername<<endl;
out.close();
}
catch(_com_error e)///捕捉异常
{
AfxMessageBox("此部门已存在!");///显示错误信息
}
tiaozhuan:
//m_vdeptno = "";
// m_vdeptname = "";
UpdateData(false);
// TODO: Add your control notification handler code here
}
void Cdeptment::displayintheuserlist()
{
//////////读取数据库中的信息添加到列表控件///////////
try
{
m_userlist.DeleteAllItems();
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("select * from deptment order by deptno",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
//m_bSuccess = TRUE;
while(!m_pRecordset->adoEOF)
{
vdeptno= m_pRecordset->GetCollect("deptno");
vdeptname= m_pRecordset->GetCollect("deptname");
vmanagername = m_pRecordset->GetCollect("managername");
//this->AbleFalse();
//this->LoadData();
//this->GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE);
//this->GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE);
nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vdeptno);//先插第一个项
m_userlist.SetItem(nItem,1,1,(_bstr_t)vdeptname,NULL,0,0,0);
m_userlist.SetItem(nItem,2,1,(_bstr_t)vmanagername ,NULL,0,0,0);
m_pRecordset->MoveNext();
}
}
catch(_com_error e)///捕捉异常
{
//AfxMessageBox("读取数据库失败!");///显示错误信息
}
}
void Cdeptment::Ondeletedeptment()
{
UpdateData(true);
//_variant_t vclassnumber;
//int panduan = 1;
//CString sql,sql2,classnumber;
//m_insertstudentnum = "'"+m_insertstudentnum + "'";
//sql = "select * from student where studentnum = "+m_insertstudentnum ;
if(MessageBox("确定要删除吗?",0,
MB_OKCANCEL|MB_DEFBUTTON2|MB_ICONWARNING)==IDOK)
{
m_vdeptno = "'"+m_vdeptno + "'";
sql = "select * from deptment where deptno = "+m_vdeptno ;
//////////////////////////////////////////////////////////////////////////////
try
{
m_pRecordset->Close();
m_pRecordset = NULL;
m_pRecordset.CreateInstance(_uuidof(Recordset));//
m_pRecordset->Open( (_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->Delete(adAffectCurrent);
//AfxMessageBox("删除记录成功!!");
displayintheuserlist();
AfxMessageBox("删除记录成功!!");
ofstream out("databf\\diary.txt",ios::app);
out<<asctime(ptrdeptment)<<"||..."<<"成功执行了向部门表中删除数据:"<<"部门编号:"<<m_vdeptno<<" 部门名称:"<<m_vdeptname<<" 部门经理: "+m_vmanagername<<endl;
out.close();
//m_pRecordset->Close();
//m_pRecordset = NULL;
// m_pRecordset.CreateInstance(_uuidof(Recordset));//
// m_pRecordset->Open("select * from student",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
AfxMessageBox("删除记录失败!");///显示错误信息
// panduan = 0;
}
}
// TODO: Add your control notification handler code here
}
void Cdeptment::LoadData0()
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("select * from deptment order by deptno",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));//从第一条记录移动
vdeptno = m_pRecordset->GetCollect("deptno");
vdeptname = m_pRecordset->GetCollect("deptname");
vmanagername = m_pRecordset->GetCollect("managername");
UpdateData(true);//使得控件显示值
//m_num = vNum.lVal;
// m_name = (LPCTSTR)(_bstr_t)vName;
//m_sex = vSex.lVal;
/// m_brithday = vBrithday.lVal;
//m_tel=vTel.lVal;
//m_addr=vAddr.lVal;
m_vdeptno= (LPCTSTR)(_bstr_t)vdeptno;
m_vdeptname = (LPCTSTR)(_bstr_t)vdeptname;
m_vmanagername = (LPCTSTR)(_bstr_t)vmanagername;
//m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));//从第一条记录移动
UpdateData(FALSE);//使得控件显示值
}
void Cdeptment::OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->uNewState&LVIS_SELECTED)
{
//UpdateData(true);
//SaveData();///保存旧数据
m_nCurrentSel = pNMListView->iItem;
LoadData0();///加载新数据
//m_cdelitem.EnableWindow();
}
// TODO: Add your control notification handler code here
*pResult = 0;
}
void Cdeptment::Onmodifydeptment()
{
// TODO: Add your control notification handler code here
//////////////////////////////////////////////////////////
UpdateData(true);
CString sql1,sql2;
m_vdeptno = "'"+m_vdeptno + "'";
m_vdeptname = "'"+m_vdeptname+"'";
m_vmanagername = "'"+m_vmanagername+"'";
sql1 = " update deptment set deptname = "+m_vdeptname+" where deptno = "+m_vdeptno ;
sql2 = " update deptment set managername = "+m_vmanagername+" where deptno = "+m_vdeptno ;
try
{
m_pRecordset->Close();
m_pRecordset = NULL;
m_pRecordset.CreateInstance(_uuidof(Recordset));//
m_pRecordset->Open( (_variant_t)sql1,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->Close();
m_pRecordset = NULL;
// m_pRecordset.CreateInstance(_uuidof(Recordset));//
// m_pRecordset->Open("select * from student",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉异常
{
//AfxMessageBox("删除记录失败!");///显示错误信息
}
try
{
m_pRecordset.CreateInstance(_uuidof(Recordset));//
m_pRecordset->Open( (_variant_t)sql2,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
//AfxMessageBox("修改记录成功!");
// m_pRecordset->Close();
// m_pRecordset = NULL;
displayintheuserlist();
AfxMessageBox("修改记录成功!");
ofstream out("databf\\diary.txt",ios::app);
out<<asctime(ptrdeptment)<<"||..."<<"成功执行了向部门表中修改数据:"<<"部门编号:"<<m_vdeptno<<" 部门名称:"<<m_vdeptname<<" 部门经理: "+m_vmanagername<<endl;
out.close();
}
catch(_com_error e)///捕捉异常
{
//AfxMessageBox("删除记录失败!");///显示错误信息
}
//ofstream out("databf\\diary.txt",ios::app);
// out<<asctime(ptrdeptment)<<"||..."<<"执行向学生表中更新数据:"<<"学号:"<<m_insertstudentnum<<" 姓名:"<<m_insertstudentname<<" 出生日期: "+m_insertstudentbirthday<<" 所在班级: "<<m_insertstudentclassnum<<endl;
// out.close();
}
void Cdeptment::OnButton1()
{
CDialog::OnOK();
Cdeptmentquery dlg;
dlg.DoModal();
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?