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

📄 maindlg.cpp

📁 用VC+ADO+ACCESS 做的学生管理系统
💻 CPP
字号:
// MainDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DataBase.h"
#include "MainDlg.h"
#include "ModiDlg.h"
#include "StudentSet.h"
#include "ScSet.h"
#include "CourseSet.h"
#include "StudentDlg.h"
#include "CourseDlg.h"
#include "ScDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainDlg dialog


CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMainDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMainDlg)
	m_field = _T("");
	m_relation = _T("");
	m_content = _T("");
	//}}AFX_DATA_INIT
}


void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDlg)
	DDX_Control(pDX, IDC_LIST1, m_student);
	DDX_Control(pDX, IDC_LIST3, m_sc);
	DDX_Control(pDX, IDC_LIST2, m_course);
	DDX_CBString(pDX, IDC_COMBO1, m_field);
	DDX_CBString(pDX, IDC_COMBO2, m_relation);
	DDX_Text(pDX, IDC_EDIT1, m_content);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
	//{{AFX_MSG_MAP(CMainDlg)
	ON_BN_CLICKED(IDC_BUTTON_MODIPASS, OnButtonModipass)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainDlg message handlers

void CMainDlg::OnButtonModipass() 
{
	// TODO: Add your control notification handler code here
	CModiDlg modidlg;
	m_database.Close();
	modidlg.user.Format("%s",user);
	modidlg.m_database.Open(_T("data"));
	modidlg.DoModal();
}

BOOL CMainDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_student.InsertColumn(0,"学号");
	m_student.InsertColumn(1,"姓名");
	m_student.InsertColumn(2,"性别");
	m_student.InsertColumn(3,"年龄");
	m_student.InsertColumn(4,"所在系");
	RECT rectstudent;
	m_student.GetWindowRect(&rectstudent);
	int widstudent=rectstudent.right-rectstudent.left;
	m_student.SetColumnWidth(0,widstudent/6);
	m_student.SetColumnWidth(1,widstudent/5);
	m_student.SetColumnWidth(2,widstudent/6);
	m_student.SetColumnWidth(3,widstudent/6);
	m_student.SetColumnWidth(4,widstudent/4);
	m_student.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	m_course.InsertColumn(0,"课程号");
	m_course.InsertColumn(1,"课程名");
	m_course.InsertColumn(2,"先行课");
	m_course.InsertColumn(3,"学分");
	RECT rectcourse;
	m_course.GetWindowRect(&rectcourse);
	int widcourse=rectcourse.right-rectcourse.left;
	m_course.SetColumnWidth(0,widcourse/4);
	m_course.SetColumnWidth(1,widcourse/3);
	m_course.SetColumnWidth(2,widcourse/4);
	m_course.SetColumnWidth(3,widcourse/6);
	m_course.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	m_sc.InsertColumn(0,"学号");
	m_sc.InsertColumn(1,"课程号");
	m_sc.InsertColumn(2,"成绩");
	RECT rectsc;
	m_sc.GetWindowRect(&rectsc);
	int widsc=rectsc.right-rectsc.left;
	m_sc.SetColumnWidth(0,widsc/3);
	m_sc.SetColumnWidth(1,widsc/3);
	m_sc.SetColumnWidth(2,widsc/3);
	m_sc.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	p_query1.Format("select * from Student");
	RefreshData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMainDlg::RefreshData()
{
	if(!m_database.IsOpen())
	{
		m_database.Open(_T("data"));
	}
	m_student.DeleteAllItems();
	CStudentSet m_studentset(&m_database);
	CString strSQL;
	m_studentset.Open(AFX_DB_USE_DEFAULT_TYPE,p_query1);
	CDBVariant varValue;
	if(m_studentset.GetRecordCount()!=0)
		m_studentset.MoveFirst();
	char buf[20];
	int i=0;
	while(!m_studentset.IsEOF())
	{
		int temp=0;
		m_studentset.GetFieldValue(temp,varValue);
		sprintf(buf,"%d",varValue.m_lVal); 
		m_student.InsertItem(i,buf);
		
		m_studentset.GetFieldValue(1,varValue);
		m_student.SetItemText(i,1,varValue.m_pstring->GetBuffer(1));
		
		m_studentset.GetFieldValue(2,varValue);
		m_student.SetItemText(i,2,varValue.m_pstring->GetBuffer(1));
		
		m_studentset.GetFieldValue(3,varValue);
		sprintf(buf,"%d",varValue.m_chVal); 
		m_student.SetItemText(i,3,buf);
		
		m_studentset.GetFieldValue(4,varValue);
		m_student.SetItemText(i,4,varValue.m_pstring->GetBuffer(1));
		
		m_studentset.MoveNext();
		i++;
	}

	m_course.DeleteAllItems();
	CCourseSet m_courseset(&m_database);
	strSQL.Format("select * from Course");
	m_courseset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);
	if(m_courseset.GetRecordCount()!=0)
		m_courseset.MoveFirst();
	i=0;
	while(!m_courseset.IsEOF())
	{
		int temp=0;
		m_courseset.GetFieldValue(temp,varValue);
		sprintf(buf,"%d",varValue.m_chVal); 
		m_course.InsertItem(i,buf);
		
		m_courseset.GetFieldValue(1,varValue);
		m_course.SetItemText(i,1,varValue.m_pstring->GetBuffer(1));
		
		m_courseset.GetFieldValue(2,varValue);
		sprintf(buf,"%d",varValue.m_chVal); 
		m_course.SetItemText(i,2,buf);

		m_courseset.GetFieldValue(3,varValue);
		sprintf(buf,"%d",varValue.m_chVal); 
		m_course.SetItemText(i,3,buf);
		
		m_courseset.MoveNext();
		i++;
	}

	m_sc.DeleteAllItems();
	CScSet m_scset(&m_database);
	strSQL.Format("select * from Sc where Sno=%d",student_id);
	m_scset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL);
	if(m_scset.GetRecordCount()!=0) 
		m_scset.MoveFirst();
	i=0;
	while(!m_scset.IsEOF())
	{
		int temp=0;
		m_scset.GetFieldValue(temp,varValue);
		sprintf(buf,"%d",student_id); 
		m_sc.InsertItem(i,buf);
		
		m_scset.GetFieldValue(1,varValue);
		sprintf(buf,"%d",varValue.m_iVal); 
		m_sc.SetItemText(i,1,buf);
		
		m_scset.GetFieldValue(2,varValue);
		sprintf(buf,"%d",varValue.m_iVal); 
		m_sc.SetItemText(i,2,buf);
		
		m_scset.MoveNext();
		i++;
	}
	
}

void CMainDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	CDialog::CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO1);
}

void CMainDlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	CDialog::CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO2);
}

void CMainDlg::OnRadio3() 
{
	// TODO: Add your control notification handler code here
	CDialog::CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO3);
}

void CMainDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int i=m_student.GetSelectionMark();
	CString strSQL;
	if(i==-1)
	{
		MessageBox("请选择一条记录!");
	}
	else
	{
		student_id=atoi(m_student.GetItemText(i,0));
		m_student.SetHotItem(i);
		RefreshData();
	}
	*pResult = 0;	
}

void CMainDlg::OnButtonSelect() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString m_realfield;
	if(m_field.Compare("学号")==0)
	{
		m_realfield.Format("Sno");
		p_query1.Format("select * from Student where %s%s%d",m_realfield,m_relation,atoi(m_content));
	}
	if(m_field.Compare("姓名")==0)
	{
		m_realfield.Format("Sname");
		p_query1.Format("select * from Student where %s%s'%s'",m_realfield,m_relation,m_content);
	}
	if(m_field.Compare("性别")==0)
	{
		m_realfield.Format("Ssex");
		p_query1.Format("select * from Student where %s%s'%s'",m_realfield,m_relation,m_content);
	}
	if(m_field.Compare("年龄")==0)
	{
		m_realfield.Format("Sage");
		p_query1.Format("select * from Student where %s%s%d",m_realfield,m_relation,atoi(m_content));
	}
	if(m_field.Compare("所在系")==0)
	{
		m_realfield.Format("Sdept");
		p_query1.Format("select * from Student where %s%s'%s'",m_realfield,m_relation,m_content);
	}
	RefreshData();
}

void CMainDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	int choice=CDialog::GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3);
	if(choice==IDC_RADIO1)
	{
		m_database.Close();
		CStudentDlg StudentDlg;
		StudentDlg.m_database.Open(_T("data"));
		StudentDlg.DoModal();
		RefreshData();	
	}
	else if(choice==IDC_RADIO2)
	{
		m_database.Close();
		CCourseDlg CourseDlg;
		CourseDlg.m_database.Open(_T("data"));
		CourseDlg.DoModal();
		RefreshData();
	}
	else if(choice==IDC_RADIO3)
	{
		m_database.Close();
		CScDlg ScDlg;
		ScDlg.m_database.Open(_T("data"));
		ScDlg.DoModal();
		RefreshData();
		
	}
	else
	{
		MessageBox("请选择一个单选框!");
	}
}

void CMainDlg::OnButtonDel() 
{
	// TODO: Add your control notification handler code here
	int choice=CDialog::GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3);
	if(choice==IDC_RADIO1)
	{
		int i=m_student.GetHotItem();
		CString strSQL;
		if(i==-1)
		{
			MessageBox("请选择一条记录!");
		}
		else{
			int keyid=atoi(m_student.GetItemText(i,0));
			strSQL.Format("delete from Student where Sno=%d",keyid);
			m_database.ExecuteSQL(strSQL);
			strSQL.Format("delete from SC where Sno=%d",keyid);
			m_database.ExecuteSQL(strSQL);
			m_database.Close();
			RefreshData();
		}
	}
	else if(choice==IDC_RADIO2)
	{
		int i=m_course.GetSelectionMark();
		CString strSQL;
		if(i==-1)
		{
			MessageBox("请选择一条记录!");
		}
		else{
			int keyid=atoi(m_course.GetItemText(i,0));
			strSQL.Format("delete from Course where Cno=%d",keyid);
			m_database.ExecuteSQL(strSQL);
			strSQL.Format("delete from Sc where Cno=%d",keyid);
			m_database.ExecuteSQL(strSQL);
			m_database.Close();
			RefreshData();
		}	
	}
	else if(choice==IDC_RADIO3)
	{
		int i=m_sc.GetSelectionMark();
		CString strSQL;
		if(i==-1)
		{
			MessageBox("请选择一条记录!");
		}
		else
		{
			int keyid1=atoi(m_sc.GetItemText(i,1));
			int keyid2=atoi(m_sc.GetItemText(i,0));
			strSQL.Format("delete from Sc where Cno=%d and Sno=%d",keyid1,keyid2);
			m_database.ExecuteSQL(strSQL);
			m_database.Close();
			RefreshData();
		}
	}
	else
	{
		MessageBox("请选择一条记录!");
	}
}

void CMainDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	m_database.Close();
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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