📄 department.cpp
字号:
// Department.cpp : implementation file
//
#include "stdafx.h"
#include "StuChgManager.h"
#include "Department.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CStuChgManagerApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CDepartment dialog
CDepartment::CDepartment(CWnd* pParent /*=NULL*/)
: CDialog(CDepartment::IDD, pParent)
{
//{{AFX_DATA_INIT(CDepartment)
m_depcode = _T("");
m_depdescribe = _T("");
m_depmanager = _T("");
m_depname = _T("");
//}}AFX_DATA_INIT
}
void CDepartment::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDepartment)
DDX_Control(pDX, IDC_DEPname, m_ctrldepname);
DDX_Control(pDX, IDC_DEPmanager, m_ctrldepmanager);
DDX_Control(pDX, IDC_DEPdescribe, m_ctrldepdescribe);
DDX_Control(pDX, IDC_DEPcode, m_ctrldepcode);
DDX_Text(pDX, IDC_DEPcode, m_depcode);
DDX_Text(pDX, IDC_DEPdescribe, m_depdescribe);
DDX_Text(pDX, IDC_DEPmanager, m_depmanager);
DDX_Text(pDX, IDC_DEPname, m_depname);
DDX_Control(pDX, IDC_DATAGRID1, m_depgrid);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDepartment, CDialog)
//{{AFX_MSG_MAP(CDepartment)
ON_BN_CLICKED(IDC_DEPENQU, OnDepenqu)
ON_BN_CLICKED(IDC_DEPMODI, OnDepmodi)
ON_BN_CLICKED(IDC_DEPINSERT, OnDepinsert)
ON_BN_CLICKED(IDC_DEPDELETE, OnDepdelete)
ON_BN_CLICKED(IDC_DEPSUBMIT, OnDepsubmit)
ON_BN_CLICKED(IDC_DEPNEW, OnDepnew)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDepartment message handlers
BOOL CDepartment::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
deleteItem = false; //初始全部操作有效
insertItem = false;
modifyItem = false;
queryItem = false;
m_ctrldepcode.EnableWindow(false);//编辑框失效
m_ctrldepname.EnableWindow(false);
m_ctrldepdescribe.EnableWindow(false);
m_ctrldepmanager.EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDepartment::OnDepenqu()
{
// TODO: Add your control notification handler code here
deleteItem = false;//初始查询操作有效
insertItem = false;
modifyItem = false;
queryItem = true;
m_ctrldepcode.SetWindowText("");//将Edit框清空
m_ctrldepname.SetWindowText("");
m_ctrldepdescribe.SetWindowText("");
m_ctrldepmanager.SetWindowText("");
m_ctrldepcode.EnableWindow(true);//初始相应对话框
m_ctrldepname.EnableWindow(false);
m_ctrldepdescribe.EnableWindow(false);
m_ctrldepmanager.EnableWindow(false);
m_ctrldepcode.SetFocus();
}
void CDepartment::OnDepmodi()
{
// TODO: Add your control notification handler code here
deleteItem = false;
insertItem = false;
modifyItem = true;
queryItem = false;
m_ctrldepcode.SetWindowText("");
m_ctrldepname.SetWindowText("");
m_ctrldepdescribe.SetWindowText("");
m_ctrldepmanager.SetWindowText("");
m_ctrldepcode.EnableWindow(true);
m_ctrldepname.EnableWindow(true);
m_ctrldepdescribe.EnableWindow(true);
m_ctrldepmanager.EnableWindow(true);
m_ctrldepcode.SetFocus();
}
void CDepartment::OnDepinsert()
{
// TODO: Add your control notification handler code here
deleteItem = false;//初始插入操作有效
insertItem = true;
modifyItem = false;
queryItem = false;
m_ctrldepcode.SetWindowText("");
m_ctrldepname.SetWindowText("");
m_ctrldepdescribe.SetWindowText("");
m_ctrldepmanager.SetWindowText("");
m_ctrldepcode.EnableWindow(true);
m_ctrldepname.EnableWindow(true);
m_ctrldepdescribe.EnableWindow(true);
m_ctrldepmanager.EnableWindow(true);
m_ctrldepcode.SetFocus();
}
void CDepartment::OnDepdelete()
{
// TODO: Add your control notification handler code here
deleteItem = true;//初始删除操作有效
insertItem = false;
modifyItem = false;
queryItem = false;
m_ctrldepcode.SetWindowText("");//清空Edit框
m_ctrldepname.SetWindowText("");
m_ctrldepdescribe.SetWindowText("");
m_ctrldepmanager.SetWindowText("");
m_ctrldepcode.EnableWindow(true);////初始相应对话框
m_ctrldepname.EnableWindow(false);
m_ctrldepdescribe.EnableWindow(false);
m_ctrldepmanager.EnableWindow(false);
m_ctrldepcode.SetFocus();
}
void CDepartment::RefreshInfo()
{
m_ctrldepcode.SetWindowText("");//清空Edit框
m_ctrldepname.SetWindowText("");
m_ctrldepdescribe.SetWindowText("");
m_ctrldepmanager.SetWindowText("");
m_depgrid.SetRefDataSource(NULL);
CString sql;//查询相应信息
sql = "select DepID as 学院代号,Depname as 学院名称,describs as 学院描述,Depmanager as 学院主任 from depinfo";
theApp.record->raw_Close(); //关闭当前的数据集
theApp.record->CursorLocation = adUseClient;
try //捕捉异常
{
theApp.record->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
}
catch(_com_error & e)
{
AfxMessageBox(e.Description());
return; //捕捉到异常后就不在继续执行
}
if(theApp.record->RecordCount > 0) //非空集合
{
m_depgrid.SetRefDataSource((LPUNKNOWN)theApp.record->DataSource); //将查询结果显示到DataGrid控件
m_depgrid.Refresh();
}
}
bool CDepartment::isRepeated(CString str)
{
CString sql;//查询相应信息
sql.Format("select * from DepInfo where DepID = \'%s\'",str);
theApp.record1->raw_Close(); //关闭当前数集
theApp.record1->CursorLocation = adUseClient;
theApp.record1->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
if (theApp.record1->RecordCount > 0)
return true;
return false;
}
bool CDepartment::notExist(CString str)
{
CString sql;//查询相应信息
sql.Format("select * from DepInfo where DepID = \'%s\'",str);
theApp.record1->raw_Close(); //关闭当前数据集they are the one
theApp.record1->CursorLocation = adUseClient;
theApp.record1->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
if(theApp.record1->RecordCount == 0)
return true;
return false;
}
void CDepartment::OnDepsubmit()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(insertItem) //如果是插入
{
if(m_depcode.GetLength() == 0)
{
MessageBox("院系代号不能为空!");
m_ctrldepcode.SetFocus();
return;
}
if(m_depname.GetLength() == 0)
{
MessageBox("原系名称不能为空!");
m_ctrldepname.SetFocus();
return;
}
if(m_depmanager.GetLength() == 0)
{
MessageBox("主任姓名不能为空!");
m_ctrldepmanager.SetFocus();
return;
}
if(isRepeated(m_depcode))
{
MessageBox("已经存在!");
m_ctrldepcode.SetFocus();
return;
}
CString sql;//插入数据
sql.Format("Insert into depinfo values(\'%s\',\'%s\',\'%s\',\'%s\')",m_depcode,m_depname,m_depdescribe,m_depmanager);
theApp.record->raw_Close(); //关闭当前的数据集
theApp.record->CursorLocation = adUseClient;
try //捕捉异常
{
theApp.record->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
}
catch(_com_error & e)
{
AfxMessageBox(e.Description());
return; //捕捉到异常后就不在继续执行
}
RefreshInfo();//刷新
MessageBox("插入成功!");
m_ctrldepcode.SetFocus();
}
if(deleteItem) //如果是删除
{
if(m_depcode.GetLength() == 0)
{
MessageBox("院系代号不能为空!");
m_ctrldepcode.SetFocus();
return;
}
if(notExist(m_depcode))
{
MessageBox("不存在!");
m_ctrldepcode.SetFocus();
return;
}
CString sql;//删除相应信息
sql.Format("delete from depinfo where depID = \'%s\'",m_depcode);
theApp.record->raw_Close(); //关闭当前的数据集
theApp.record->CursorLocation = adUseClient;
try //捕捉异常
{
theApp.record->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
}
catch(_com_error & e)
{
AfxMessageBox(e.Description());
return; //捕捉到异常后就不在继续执行
}
RefreshInfo();//刷新
MessageBox("删除成功!");
m_ctrldepcode.SetFocus();
}
if(queryItem) //如果是查询
{
if(m_depcode.GetLength() == 0)
{
MessageBox("院系代号不能为空!");
m_ctrldepcode.SetFocus();
return;
}
if(notExist(m_depcode))
{
MessageBox("不存在!");
m_ctrldepcode.SetFocus();
return;
}
CString sql; //查询相应信息
sql.Format("select * from depinfo where depID = \'%s\'",m_depcode);
theApp.record->raw_Close(); //关闭当前的数据集
theApp.record->CursorLocation = adUseClient;
try //捕捉异常
{
theApp.record->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
}
catch(_com_error & e)
{
AfxMessageBox(e.Description());
return; //捕捉到异常后就不在继续执行
}
if(theApp.record->RecordCount > 0) //非空集合
{
m_depgrid.SetRefDataSource((LPUNKNOWN)theApp.record->DataSource); //将查询结果显示到DataGrid控件
m_depgrid.Refresh();
}
m_ctrldepcode.SetFocus();
}
if(modifyItem) //如果是修改
{
if(m_depcode.GetLength() == 0)
{
MessageBox("院系代号不能为空!");
m_ctrldepcode.SetFocus();
return;
}
if(m_depname.GetLength() == 0)
{
MessageBox("原系名称不能为空!");
m_ctrldepname.SetFocus();
return;
}
if(m_depmanager.GetLength() == 0)
{
MessageBox("主任姓名不能为空!");
m_ctrldepmanager.SetFocus();
return;
}
if(notExist(m_depcode))
{
MessageBox("不存在!");
m_ctrldepcode.SetFocus();
return;
}
CString sql; //修改相应信息
sql.Format("update depinfo set depname =\'%s\',describs=\'%s\',depmanager=\'%s\' where depID = \'%s\'",m_depname,m_depdescribe,m_depmanager,m_depcode);
theApp.record->raw_Close(); //关闭当前的数据集
theApp.record->CursorLocation = adUseClient;
try //捕捉异常
{
theApp.record->Open((_bstr_t)sql,theApp.connection.GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
}
catch(_com_error & e)
{
AfxMessageBox(e.Description());
return; //捕捉到异常后就不在继续执行
}
RefreshInfo(); //刷新
MessageBox("修改成功!");
m_ctrldepcode.SetFocus();
}
}
void CDepartment::OnDepnew()
{
// TODO: Add your control notification handler code here
this->RefreshInfo(); //刷新
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -