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

📄 depatels.cpp

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

#include "stdafx.h"
#include "StuChgManager.h"
#include "DepaTels.h"

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

extern CStuChgManagerApp theApp; //引入外部变量
/////////////////////////////////////////////////////////////////////////////
// CDepaTels dialog


CDepaTels::CDepaTels(CWnd* pParent /*=NULL*/)
	: CDialog(CDepaTels::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDepaTels)
	m_deptels = _T("");
	m_tels = _T("");
	m_newtel = _T("");
	//}}AFX_DATA_INIT
}


void CDepaTels::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDepaTels)
	DDX_Control(pDX, IDC_NEW, m_ctrlnewtel);
	DDX_Control(pDX, IDC_TELS, m_ctrltels);
	DDX_Control(pDX, IDC_DEPTELID, m_ctrldeptels);
	DDX_Text(pDX, IDC_DEPTELID, m_deptels);
	DDX_Text(pDX, IDC_TELS, m_tels);
	DDX_Control(pDX, IDC_DATAGRID1, m_grid);
	DDX_Text(pDX, IDC_NEW, m_newtel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDepaTels, CDialog)
	//{{AFX_MSG_MAP(CDepaTels)
	ON_BN_CLICKED(IDC_TELenqu, OnTELenqu)
	ON_BN_CLICKED(IDC_TELchg, OnTELchg)
	ON_BN_CLICKED(IDC_TELins, OnTELins)
	ON_BN_CLICKED(IDC_TELdele, OnTELdele)
	ON_BN_CLICKED(IDC_TSUBMIT, OnTsubmit)
	ON_BN_CLICKED(IDC_DEPTELNEW, OnDeptelnew)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDepaTels message handlers

void CDepaTels::OnTELenqu() 
{
	// TODO: Add your control notification handler code here
	insertItem = false; //初始查询操作有效
	deleteItem = false;
	modifyItem = false;
	queryItem  = true;
	m_ctrldeptels.SetWindowText("");
	m_ctrltels.SetWindowText("");   //将Edit框清空

	m_ctrldeptels.EnableWindow(true); //初始相应的编辑框
	m_ctrltels.EnableWindow(false);
	m_ctrlnewtel.EnableWindow(false);

    m_ctrldeptels.SetFocus(); //设置焦点
}

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

	m_ctrltels.EnableWindow(false); //初始编辑框失效
	m_ctrldeptels.EnableWindow(false);
	m_ctrlnewtel.EnableWindow(false);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDepaTels::OnTELchg() 
{
	// TODO: Add your control notification handler code here
	insertItem = false; //修改有效
	deleteItem = false;
	modifyItem = true;
	queryItem  = false;
	m_ctrldeptels.SetWindowText("");
	m_ctrltels.SetWindowText("");   //将Edit框清空

	m_ctrltels.EnableWindow(true); 
	m_ctrldeptels.EnableWindow(true);
	m_ctrlnewtel.EnableWindow(true);
	m_ctrldeptels.SetFocus();
}

void CDepaTels::OnTELins() 
{
	// TODO: Add your control notification handler code here
	insertItem = true; //插入有效
	deleteItem = false;
	modifyItem = false;
	queryItem  = false;

	m_ctrldeptels.SetWindowText("");
	m_ctrltels.SetWindowText("");   //将Edit框清空

	m_ctrltels.EnableWindow(true);
	m_ctrldeptels.EnableWindow(true);
	m_ctrlnewtel.EnableWindow(false);
	m_ctrldeptels.SetFocus();
}

void CDepaTels::OnTELdele() 
{
	// TODO: Add your control notification handler code here
	insertItem = false; //删除有效
	deleteItem = true;
	modifyItem = false;
	queryItem  = false;
	m_ctrldeptels.SetWindowText("");
	m_ctrltels.SetWindowText("");   //将Edit框清空

	m_ctrltels.EnableWindow(true);
	m_ctrldeptels.EnableWindow(true);
	m_ctrlnewtel.EnableWindow(false);
	m_ctrldeptels.SetFocus();
}
bool CDepaTels::isRepeated(CString str1,CString str2)
{
	CString sql;  //查询记录
	sql.Format("select * from DepTel where DepID = \'%s'and deptels = \'%s'",str1,str2);

	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 CDepaTels::notExist(CString str1,CString str2)
{
	CString sql; //查询记录
	sql.Format("select * from DepTel where DepID = \'%s'and deptels = \'%s'",str1,str2);
  
	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;
}
bool CDepaTels::notExist(CString str1)
{
	CString sql; //查询记录
	sql.Format("select * from DepTel where DepID = \'%s'",str1);
    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;
}

void CDepaTels::RefreshInfo()
{
	m_ctrldeptels.SetWindowText("");
	m_ctrltels.SetWindowText("");   //将Edit框清空

	m_grid.SetRefDataSource(NULL);

	CString sql;  
	sql = "select DepID as 学院代号,Deptels as 学院电话 from deptel";

    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_grid.SetRefDataSource((LPUNKNOWN)theApp.record->DataSource);    //将查询结果显示到DataGrid控件
		m_grid.Refresh();
	}
}

void CDepaTels::OnTsubmit() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);

	if(insertItem) //如果是插入
	{
		if(m_deptels.GetLength() == 0)
		{
			MessageBox("院系代号不能为空!");
			m_ctrldeptels.SetFocus();
			return;
		}
		if(m_tels.GetLength() == 0)
		{
			MessageBox("电话号码不能为空!");
			m_ctrltels.SetFocus();
			return;
		}
		if(isRepeated(m_deptels,m_tels))
		{
			MessageBox("已经存在!");
			m_ctrldeptels.SetSel(0,-1);
			m_ctrldeptels.SetFocus();
			return;
		}
        
		CString sql;  //插入记录
		sql.Format("Insert into deptel values(\'%s\',\'%s\')",m_deptels,m_tels); 

		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_ctrldeptels.SetFocus();
	}

	if(deleteItem) //如果是删除
	{
		if(m_deptels.GetLength() == 0)
		{
			MessageBox("院系代号不能为空!");
			m_ctrldeptels.SetFocus();
			return;
		}
		if(m_tels.GetLength() == 0)
		{
			MessageBox("电话号码不能为空!");
			m_ctrltels.SetFocus();
			return;
		}
		if(notExist(m_deptels,m_tels))
		{
			MessageBox("不存在!");
			m_ctrldeptels.SetFocus();
			return;
		}
		CString sql; //删除记录
		sql.Format("delete from deptel where depID = \'%s\' and deptels = \'%s\'",m_deptels,m_tels);
	    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_ctrldeptels.SetFocus();
	}
	if(queryItem) //如果是查询
	{
		if(m_deptels.GetLength() == 0)
		{
			MessageBox("院系代号不能为空!");
			m_ctrldeptels.SetFocus();
			return;
		}
		if(notExist(m_deptels))
		{
			MessageBox("不存在!");
			m_ctrldeptels.SetFocus();
			return;
		}

		CString sql;
		sql.Format("select * from deptel where depID = \'%s\'",m_deptels);
	    
		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();
		if(theApp.record->RecordCount > 0)   //非空集合
		{ 
			m_grid.SetRefDataSource((LPUNKNOWN)theApp.record->DataSource);    //将查询结果显示到DataGrid控件
			m_grid.Refresh();
		} 
        
		m_ctrldeptels.SetFocus();
	}
	if(modifyItem) //如果是修改
	{
		if(m_deptels.GetLength() == 0)
		{
			MessageBox("院系代号不能为空!");
			m_ctrldeptels.SetFocus();
			return;
		}
		if(m_tels.GetLength() == 0)
		{
			MessageBox("电话号码不能为空!");
			m_ctrltels.SetFocus();
			return;
		}
		if(m_newtel.GetLength() == 0)
		{
			MessageBox("新的电话号码不能为空!");
			m_ctrlnewtel.SetFocus();
			return;
		}
		if(notExist(m_deptels,m_tels))
		{
			MessageBox("不存在!");
			m_ctrldeptels.SetFocus();
			return;
		}
		CString sql; //修改记录
		sql.Format("Update deptel set deptels = \'%s\' where depID =\'%s\' and deptels=\'%s\'",m_newtel,m_deptels,m_tels);
        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_ctrldeptels.SetFocus();
	}
}


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

⌨️ 快捷键说明

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