MajorDlg.cpp
资源名称:school.rar [点击查看]
上传用户:rs600066
上传日期:2017-10-16
资源大小:4788k
文件大小:7k
源码类别:
数据库系统
开发平台:
Visual C++
- // MajorDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "school.h"
- #include "MajorDlg.h"
- #include "DepartmentSet.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMajorDlg dialog
- CMajorDlg::CMajorDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMajorDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMajorDlg)
- m_strDepartment = _T("");
- m_strCode = _T("");
- m_strInfo = _T("");
- m_strName = _T("");
- //}}AFX_DATA_INIT
- }
- void CMajorDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMajorDlg)
- DDX_Control(pDX, IDC_COMBO_DEPARTMENT, m_ctrDepartment);
- DDX_Control(pDX, IDC_LIST1, m_ctrList);
- DDX_Control(pDX, IDC_BUTTON_SAVE, m_bntSave);
- DDX_Control(pDX, IDC_BUTTON_NEW, m_bntNew);
- DDX_Control(pDX, IDC_BUTTON_MODIFY, m_bntModify);
- DDX_Control(pDX, IDC_BUTTON_DELETE, m_bntDelete);
- DDX_CBString(pDX, IDC_COMBO_DEPARTMENT, m_strDepartment);
- DDX_Text(pDX, IDC_EDIT_CODE, m_strCode);
- DDX_Text(pDX, IDC_EDIT_INFO, m_strInfo);
- DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMajorDlg, CDialog)
- //{{AFX_MSG_MAP(CMajorDlg)
- ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
- ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
- ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
- ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
- ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMajorDlg message handlers
- BOOL CMajorDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- CDepartmentSet DeptSet ;
- CString strSQL;
- strSQL="select * from department";
- if(!DeptSet.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
- {
- MessageBox("打开数据库失败!","数据库错误",MB_OK);
- }
- while(!DeptSet.IsEOF())
- {
- m_ctrDepartment.AddString(DeptSet.m_name);
- DeptSet.MoveNext();
- }
- DeptSet.Close();
- m_ctrList.InsertColumn(0,"专业代码");
- m_ctrList.InsertColumn(1,"专业名称");
- m_ctrList.InsertColumn(2,"所属系别");
- m_ctrList.InsertColumn(3,"说明");
- m_ctrList.SetColumnWidth(0,60);
- m_ctrList.SetColumnWidth(1,120);
- m_ctrList.SetColumnWidth(2,120);
- m_ctrList.SetColumnWidth(3,160);
- m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- //设置按钮状态
- m_bntSave.EnableWindow(FALSE);
- m_bntNew.EnableWindow(FALSE);
- m_bntDelete.EnableWindow(FALSE);
- m_bntModify.EnableWindow(FALSE);
- RefreshData();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CMajorDlg::RefreshData()
- {
- m_ctrList.DeleteAllItems();
- m_ctrList.SetRedraw(FALSE);
- UpdateData(TRUE);
- CString strSQL;
- strSQL.Format( "select * from major ");
- if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
- {
- MessageBox("打开数据库失败!","数据库错误",MB_OK);
- return ;
- }
- int i=0;
- while(!m_recordset.IsEOF())
- {
- m_ctrList.InsertItem(i,m_recordset.m_code);
- m_ctrList.SetItemText(i,1,m_recordset.m_name);
- m_ctrList.SetItemText(i,2,m_recordset.m_department);
- m_ctrList.SetItemText(i,3,m_recordset.m_brief);
- i++;
- m_recordset.MoveNext();
- }
- m_recordset.Close();
- m_ctrList.SetRedraw(TRUE);
- //设置按钮状态
- m_bntSave.EnableWindow(FALSE);
- m_bntNew.EnableWindow();
- m_bntDelete.EnableWindow(FALSE);
- m_bntModify.EnableWindow(FALSE);
- }
- void CMajorDlg::OnButtonNew()
- {
- // TODO: Add your control notification handler code here
- m_strName = "";
- m_strCode = "";
- m_strDepartment = "";
- m_strInfo = "";
- m_bntSave.EnableWindow();
- m_bntNew.EnableWindow(FALSE);
- m_bntDelete.EnableWindow(FALSE);
- m_bntModify.EnableWindow(FALSE);
- UpdateData(FALSE);
- }
- void CMajorDlg::OnButtonSave()
- {
- // TODO: Add your control notification handler code here
- UpdateData();
- if(m_strName=="")
- {
- AfxMessageBox("请输入专业名称!");
- return;
- }
- if(m_strCode=="")
- {
- AfxMessageBox("请输入专业代码!");
- return;
- }
- if(m_strDepartment=="")
- {
- AfxMessageBox("请输入所属系别!");
- return;
- }
- CString strSQL;
- strSQL.Format("select * from major where code='%s'",m_strCode);
- if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
- {
- MessageBox("打开数据库失败!","数据库错误",MB_OK);
- return ;
- }
- if( m_recordset.GetRecordCount()!=0)
- {
- AfxMessageBox("当前编码已经存在!请重新输入!");
- m_strCode = "";
- UpdateData(FALSE);
- m_recordset.Close();
- return;
- }
- m_recordset.AddNew();
- m_recordset.m_name = m_strName ;
- m_recordset.m_code = m_strCode ;
- m_recordset.m_department= m_strDepartment ;
- m_recordset.m_brief = m_strInfo ;
- m_recordset.Update();
- m_recordset.Close();
- RefreshData();
- }
- void CMajorDlg::OnButtonDelete()
- {
- // TODO: Add your control notification handler code here
- int i = m_ctrList.GetSelectionMark();
- if(0>i)
- {
- MessageBox("请选择一条记录进行删除!");
- return;
- }
- CString strSQL;
- strSQL.Format("select * from major where code= '%s' ",m_ctrList.GetItemText(i,0));
- if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
- {
- MessageBox("打开数据库失败!","数据库错误",MB_OK);
- return ;
- }
- //删除该记录
- m_recordset.Delete();
- m_recordset.Close();
- //更新用户界面
- RefreshData();
- m_strName = "";
- m_strCode = "";
- m_strDepartment = "";
- m_strInfo = "";
- UpdateData(FALSE);
- }
- void CMajorDlg::OnButtonModify()
- {
- // TODO: Add your control notification handler code here
- UpdateData();
- int i = m_ctrList.GetSelectionMark();
- if(0>i)
- {
- MessageBox("请选择一条记录进行修改!");
- return;
- }
- if(m_strName=="")
- {
- AfxMessageBox("请输入专业名称!");
- return;
- }
- if(m_strCode=="")
- {
- AfxMessageBox("请输入专业代码!");
- return;
- }
- if(m_strDepartment=="")
- {
- AfxMessageBox("请输入所属系别!");
- return;
- }
- CString strSQL;
- strSQL.Format("select * from major where code= '%s' ",m_ctrList.GetItemText(i,0));
- if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
- {
- MessageBox("打开数据库失败!","数据库错误",MB_OK);
- return ;
- }
- m_recordset.Edit();
- m_recordset.m_name = m_strName ;
- m_recordset.m_code = m_strCode ;
- m_recordset.m_department= m_strDepartment ;
- m_recordset.m_brief = m_strInfo ;
- m_recordset.Update();
- m_recordset.Close();
- RefreshData();
- }
- void CMajorDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- // TODO: Add your control notification handler code here
- CString strSQL;
- UpdateData(TRUE);
- int i = m_ctrList.GetSelectionMark();
- strSQL.Format("select * from major where code='%s'",m_ctrList.GetItemText(i,0));
- if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
- {
- MessageBox("打开数据库失败!","数据库错误",MB_OK);
- return ;
- }
- m_strName = m_recordset.m_name;
- m_strCode = m_recordset.m_code;
- m_strDepartment = m_recordset.m_department;
- m_strInfo = m_recordset.m_brief;
- m_recordset.Close();
- UpdateData(FALSE);
- m_bntSave.EnableWindow(FALSE);
- m_bntNew.EnableWindow();
- m_bntDelete.EnableWindow();
- m_bntModify.EnableWindow();
- *pResult = 0;
- }