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

📄 speciality.cpp

📁 自己弄得一个学生管理系统
💻 CPP
字号:
// Speciality.cpp : implementation file
//

#include "stdafx.h"
#include "StuChgManager.h"
#include "Speciality.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CStuChgManagerApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CSpeciality dialog


CSpeciality::CSpeciality(CWnd* pParent /*=NULL*/)
	: CDialog(CSpeciality::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSpeciality)
	m_specode = _T("");
	m_spedepa = _T("");
	m_spedescribes = _T("");
	m_spename = _T("");
	//}}AFX_DATA_INIT
}


void CSpeciality::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSpeciality)
	DDX_Control(pDX, IDC_SPEname, m_ctrlspename);
	DDX_Control(pDX, IDC_SPEdescribes, m_ctrlspecribes);
	DDX_Control(pDX, IDC_SPEdepa, m_ctrlspedepa);
	DDX_Control(pDX, IDC_SPEcode, m_ctrlspecode);
	DDX_Text(pDX, IDC_SPEcode, m_specode);
	DDX_Text(pDX, IDC_SPEdepa, m_spedepa);
	DDX_Text(pDX, IDC_SPEdescribes, m_spedescribes);
	DDX_Text(pDX, IDC_SPEname, m_spename);
	DDX_Control(pDX, IDC_DATAGRID3, m_spegrid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSpeciality, CDialog)
	//{{AFX_MSG_MAP(CSpeciality)
	ON_BN_CLICKED(IDC_SPEQUIRE, OnSpequire)
	ON_BN_CLICKED(IDC_SPEMODIFY, OnSpemodify)
	ON_BN_CLICKED(IDC_SPEINSERT, OnSpeinsert)
	ON_BN_CLICKED(IDC_SPEDELETE, OnSpedelete)
	ON_BN_CLICKED(IDC_SPESUBMIT, OnSpesubmit)
	ON_BN_CLICKED(IDC_SPECIALITYNEW, OnSpecialitynew)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSpeciality message handlers
BOOL CSpeciality::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	deleteItem = false;  //初始操作失效
	insertItem = false;
	modifyItem = false;
    queryItem = false;

 	m_ctrlspecode.SetWindowText(""); //清空Eidt框
	m_ctrlspedepa.SetWindowText("");
	m_ctrlspename.SetWindowText("");
	m_ctrlspecribes.SetWindowText("");

	m_ctrlspecode.EnableWindow(false);//Edit框失效
	m_ctrlspename.EnableWindow(false);
	m_ctrlspecribes.EnableWindow(false);
	m_ctrlspedepa.EnableWindow(false);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CSpeciality::OnSpequire() 
{
	// TODO: Add your control notification handler code here
	deleteItem = false;  //查询操作有效
	insertItem = false;
	modifyItem = false;
    queryItem = true;
 	m_ctrlspecode.SetWindowText(""); //清空edit 框
	m_ctrlspedepa.SetWindowText("");
	m_ctrlspename.SetWindowText("");
	m_ctrlspecribes.SetWindowText("");

	m_ctrlspecode.EnableWindow(true);
	m_ctrlspename.EnableWindow(false);
	m_ctrlspecribes.EnableWindow(false);
	m_ctrlspedepa.EnableWindow(false);
	m_ctrlspecode.SetFocus();
}

void CSpeciality::OnSpemodify() 
{
	// TODO: Add your control notification handler code here
	deleteItem = false;//修改有效
	insertItem = false;
	modifyItem = true;
    queryItem = false;
 	
	m_ctrlspecode.SetWindowText(""); //清空Eidt框
	m_ctrlspedepa.SetWindowText("");
	m_ctrlspename.SetWindowText("");
	m_ctrlspecribes.SetWindowText("");

	m_ctrlspecode.EnableWindow(true); //置相应的Edit 框
	m_ctrlspename.EnableWindow(true);
	m_ctrlspecribes.EnableWindow(true);
	m_ctrlspedepa.EnableWindow(true);
    m_ctrlspecode.SetFocus();
}

void CSpeciality::OnSpeinsert() 
{
	// TODO: Add your control notification handler code here
	deleteItem = false; //插入有效
	insertItem = true;
	modifyItem = false;
    queryItem = false;
 	m_ctrlspecode.SetWindowText(""); //清空Edit框
	m_ctrlspedepa.SetWindowText("");
	m_ctrlspename.SetWindowText("");
	m_ctrlspecribes.SetWindowText("");

	m_ctrlspecode.EnableWindow(true);
	m_ctrlspename.EnableWindow(true);
	m_ctrlspecribes.EnableWindow(true);
	m_ctrlspedepa.EnableWindow(true);
    m_ctrlspecode.SetFocus();
}

void CSpeciality::OnSpedelete() 
{
	// TODO: Add your control notification handler code here
	deleteItem = true; //删除有效
	insertItem = false;
	modifyItem = false;
    queryItem = false;
 
	m_ctrlspecode.SetWindowText(""); //清空Edit框
	m_ctrlspedepa.SetWindowText("");
	m_ctrlspename.SetWindowText("");
	m_ctrlspecribes.SetWindowText("");

	m_ctrlspecode.EnableWindow(true); 
	m_ctrlspename.EnableWindow(false);
	m_ctrlspecribes.EnableWindow(false);
	m_ctrlspedepa.EnableWindow(false);
    m_ctrlspecode.SetFocus();
}
void CSpeciality::RefreshInfo()
{
	m_ctrlspecode.SetWindowText(""); //清空Edit框
	m_ctrlspedepa.SetWindowText("");
	m_ctrlspename.SetWindowText("");
	m_ctrlspecribes.SetWindowText("");

	m_spegrid.SetRefDataSource(NULL);

	CString sql;
	sql = "select speID as 专业代号,depID as 所属学院,spename as 专业名称,describs as 专业描述 from speciality";

    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_spegrid.SetRefDataSource((LPUNKNOWN)theApp.record->DataSource);    //将查询结果显示到DataGrid控件
		m_spegrid.Refresh();
	}
}
bool CSpeciality::isRepeated(CString str)
{
	CString sql; //查询记录
	sql.Format("select * from speciality where speid = \'%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 CSpeciality::notExist(CString str)
{	
	CString sql; //查询记录
	sql.Format("select * from speciality where speID = \'%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 CSpeciality::OnSpesubmit() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	

	if(deleteItem) //如果是删除
	{
		if(m_specode.GetLength() == 0)
		{
			MessageBox("专业代码不能为空!");
			m_ctrlspecode.SetFocus();
			return;
		}
		if(notExist(m_specode))
		{
			MessageBox("不存在此专业!");
			m_ctrlspecode.SetFocus();
			return;
		}
		CString sql;  //删除记录
		sql.Format("delete from speciality where speID = \'%s\'",m_specode);
		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_ctrlspecode.SetFocus(); 
	}
	if(insertItem) //插入
	{
		if(m_specode.GetLength() == 0)
		{
			MessageBox("专业代码不能为空!");
			m_ctrlspecode.SetFocus();
			return;
		}
		if(m_spedepa.GetLength() == 0)
		{
			MessageBox("所属学院不能为空!");
			m_ctrlspedepa.SetFocus();
			return;
		}
		if(m_spename.GetLength() == 0)
		{
			MessageBox("专业姓名不能为空!");
			m_ctrlspename.SetFocus();
			return;
		}
		if(isRepeated(m_specode))
		{
			MessageBox("专业已存在!");
			m_ctrlspecode.SetFocus();
			return;
		}
		CString sql; //插入信息
		sql.Format("insert into speciality values(\'%s',\'%s',\'%s',\'%s')",m_specode,m_spedepa,m_spename,m_spedescribes);
	
        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_ctrlspecode.SetFocus(); 
	}
	if(queryItem) //查询 
	{
		if(m_specode.GetLength() == 0)
		{
			MessageBox("专业代码不能为空!");
			m_ctrlspecode.SetFocus();
			return;
		}
		if(notExist(m_specode))
		{
			MessageBox("专业不存在!");
			m_ctrlspecode.SetFocus();
			return;
		}
		CString sql; //查询记录
		sql.Format("select * from speciality where speID= \'%s'",m_specode);

		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_spegrid.SetRefDataSource((LPUNKNOWN)theApp.record->DataSource);    //将查询结果显示到DataGrid控件
			m_spegrid.Refresh();
		} 
		m_ctrlspecode.SetFocus();
	}
	if(modifyItem) //修改
	{ 
		if(m_specode.GetLength() == 0)
		{
			MessageBox("专业代码不能为空!");
			m_ctrlspecode.SetFocus();
			return;
		}
		if(m_spedepa.GetLength() == 0)
		{
			MessageBox("所属学院不能为空!");
			m_ctrlspedepa.SetFocus();
			return;
		}
		if(m_spename.GetLength() == 0)
		{
			MessageBox("专业姓名不能为空!");
			m_ctrlspename.SetFocus();
			return;
		}
	    if(notExist(m_specode))
		{
			MessageBox("专业不存在!");
			m_ctrlspecode.SetFocus();
			return;
		}
		CString sql; //修改记录
		sql.Format("update speciality set depID = '%s',spename ='%s',describs='%s' where speid='%s'",m_spedepa,m_spename,m_spedescribes,m_specode);
		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_ctrlspecode.SetFocus();
	}
}



void CSpeciality::OnSpecialitynew() 
{
	// TODO: Add your control notification handler code here
	this->RefreshInfo(); //刷新
}

⌨️ 快捷键说明

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