📄 studentdlg.cpp
字号:
// StudentDlg.cpp : implementation file
//
#include "stdafx.h"
#include "StuManage.h"
#include "StudentDlg.h"
#include "Columns.h"
#include "Column.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStudentDlg dialog
extern CStuManageApp theApp;
CStudentDlg::CStudentDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStudentDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStudentDlg)
m_strNativePlace = _T("");
m_nAge = 0;
m_strName = _T("");
m_strSex = _T("");
m_strID = _T("");
//}}AFX_DATA_INIT
}
void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStudentDlg)
DDX_Control(pDX, IDC_EDIT_STUDENT_ID, m_ID);
DDX_Control(pDX, IDC_DATETIMEPICKER1, m_timectrlTime);
DDX_Text(pDX, IDC_EDIT_NATIVE_PLACE, m_strNativePlace);
DDX_Text(pDX, IDC_EDIT_STUDENT_AGE, m_nAge);
DDX_Text(pDX, IDC_EDIT_STUDENT_NAME, m_strName);
DDX_Control(pDX, IDC_DATAGRID_STUDENTINFO, m_dgStudent);
DDX_CBString(pDX, IDC_COMBO_SEX, m_strSex);
DDX_Text(pDX, IDC_EDIT_STUDENT_ID, m_strID);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
//{{AFX_MSG_MAP(CStudentDlg)
ON_BN_CLICKED(IDC_ADD_STUDENT_BTN, OnAddStudentBtn)
ON_BN_CLICKED(IDC_MODIFY_STUDENT_BTN, OnModifyStudentBtn)
ON_BN_CLICKED(IDC_DEL_STUDENT_BTN, OnDelStudentBtn)
ON_BN_CLICKED(IDC_LOOKFOR_STUDENT_BTN, OnLookforStudentBtn)
ON_EN_KILLFOCUS(IDC_EDIT_STUDENT_ID, OnKillfocusEditStudentId)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudentDlg message handlers
BOOL CStudentDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SelectType=0;
m_pRecordset_stu.CreateInstance("ADODB.Recordset");
m_pRecordset_stu->Open("SELECT * FROM StudentInfo",_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
m_dgStudent.SetRefDataSource(NULL);
m_dgStudent.SetRefDataSource((LPUNKNOWN)m_pRecordset_stu);
m_dgStudent.SetColumnHeaders(2) ;
_variant_t var;
var = long(0);
m_dgStudent.GetColumns().GetItem(var).SetCaption("学号");
m_dgStudent.GetColumns().GetItem(var).SetWidth(60);
var = long(1);
m_dgStudent.GetColumns().GetItem(var).SetCaption("姓名");
m_dgStudent.GetColumns().GetItem(var).SetWidth(50);
var = long(2);
m_dgStudent.GetColumns().GetItem(var).SetCaption("性别");
m_dgStudent.GetColumns().GetItem(var).SetWidth(30);
var = long(3);
m_dgStudent.GetColumns().GetItem(var).SetCaption("年龄");
m_dgStudent.GetColumns().GetItem(var).SetWidth(30);
var = long(4);
m_dgStudent.GetColumns().GetItem(var).SetCaption("籍贯");
m_dgStudent.GetColumns().GetItem(var).SetWidth(50);
var = long(5);
m_dgStudent.GetColumns().GetItem(var).SetCaption("入学时间");
m_dgStudent.GetColumns().GetItem(var).SetWidth(100);
m_dgStudent.Refresh();
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CStudentDlg::OnAddStudentBtn()
{
// TODO: Add your control notification handler code here
if(theApp.m_Level != 2)
{
AfxMessageBox("您无权添加记录");
return;
}
UpdateData(TRUE);
if(m_strID.IsEmpty())
{
AfxMessageBox("请输入新生的学号");
return;
}
if ((m_strID=="") || (m_strName=="" ) ||(m_strSex=="")||(m_strNativePlace==""))
{
MessageBox("请将学生信息输入完整","基本信息",MB_OK);
return;
}
else
{
CString sql,str;
sql = "select * from StudentInfo ";
m_pRecordset_stu.CreateInstance("ADODB.Recordset");
m_pRecordset_stu->Open((_variant_t)sql,_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
if(m_pRecordset_stu->GetRecordCount()!= 0)
{
while(!m_pRecordset_stu->adoEOF)
{
str = m_pRecordset_stu->GetCollect("ID").bstrVal;
if(str.CompareNoCase(m_strID) == 0)
{
AfxMessageBox("该编号的学生记录已存在");
return;
}
m_pRecordset_stu->MoveNext();
}
}
m_pRecordset_stu->AddNew();
m_pRecordset_stu->PutCollect("ID",(_variant_t)m_strID);
m_pRecordset_stu->PutCollect("Name",(_variant_t)m_strName);
m_pRecordset_stu->PutCollect("Sex",(_variant_t)m_strSex);
str.Format("%d",m_nAge);
m_pRecordset_stu->PutCollect("Age",(_variant_t)str);
m_pRecordset_stu->PutCollect("NativePlace",(_variant_t)m_strNativePlace);
GetDlgItemText(IDC_DATETIMEPICKER1,str);
m_pRecordset_stu->PutCollect("MatriculationTime",(_variant_t)str);
m_pRecordset_stu->Update();
MessageBox("学生信息保存完毕 ","基本信息",MB_OK);
}
}
void CStudentDlg::OnModifyStudentBtn()
{
// TODO: Add your control notification handler code here
if(theApp.m_Level != 2)
{
AfxMessageBox("您无权修改记录");
return;
}
UpdateData(TRUE);
CString sql,str;
sql = "select * from StudentInfo where ID = '"+m_strID+"'";
m_pRecordset_stu.CreateInstance("ADODB.Recordset");
m_pRecordset_stu->Open((_variant_t)sql,_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
if(m_pRecordset_stu->GetRecordCount() == 0)
{
AfxMessageBox("该编号的学生记录不存在");
return;
}
m_pRecordset_stu->PutCollect("Name",(_variant_t)m_strName);
m_pRecordset_stu->PutCollect("Sex",(_variant_t)m_strSex);
str.Format("%d",m_nAge);
m_pRecordset_stu->PutCollect("Age",(_variant_t)str);
m_pRecordset_stu->PutCollect("NativePlace",(_variant_t)m_strNativePlace);
GetDlgItemText(IDC_DATETIMEPICKER1,str);
m_pRecordset_stu->PutCollect("MatriculationTime",(_variant_t)str);
m_pRecordset_stu->Update();
}
void CStudentDlg::OnOK()
{
// TODO: Add extra validation here
m_pRecordset_stu.CreateInstance("ADODB.Recordset");
m_pRecordset_stu->Open("SELECT * FROM StudentInfo",_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
m_dgStudent.SetRefDataSource(NULL);
m_dgStudent.SetRefDataSource((LPUNKNOWN)m_pRecordset_stu);
m_dgStudent.SetColumnHeaders(2) ;
m_dgStudent.Refresh();
UpdateData(FALSE);
// CDialog::OnOK();
}
void CStudentDlg::OnDelStudentBtn()
{
// TODO: Add your control notification handler code here
if(theApp.m_Level != 2)
{
AfxMessageBox("您无权删除记录");
return;
}
UpdateData(TRUE);
CString sql,str;
sql = "select * from StudentInfo where ID = '"+m_strID+"'";
m_pRecordset_stu.CreateInstance("ADODB.Recordset");
m_pRecordset_stu->Open((_variant_t)sql,_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
if(m_pRecordset_stu->GetRecordCount() == 0)
{
AfxMessageBox("该编号的学生记录不存在");
return;
}
m_pRecordset_stu->Delete(adAffectCurrent);
m_pRecordset_stu->Update();
}
void CStudentDlg::OnLookforStudentBtn()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
MessageBox("请在学号输入框中输入您要查询的学号","基本信息");
SelectType=1;
m_strNativePlace = _T("");
m_nAge = 0;
m_strName = _T("");
m_strSex = _T("");
m_strID = _T("");
m_ID.SetFocus();
UpdateData(FALSE);
}
void CStudentDlg::OnKillfocusEditStudentId()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString sql,str;
sql = "select * from StudentInfo where ID = '"+m_strID+"'";
m_pRecordset_stu.CreateInstance("ADODB.Recordset");
m_pRecordset_stu->Open((_variant_t)sql,_variant_t((IDispatch *)theApp.m_pConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
if(m_pRecordset_stu->GetRecordCount()!= 0)
{
m_strName = m_pRecordset_stu->GetCollect("Name").bstrVal;
m_strSex = m_pRecordset_stu->GetCollect("Sex").bstrVal;
m_nAge = m_pRecordset_stu->GetCollect("Age").lVal;
m_strNativePlace = m_pRecordset_stu->GetCollect("NativePlace").bstrVal;
str = m_pRecordset_stu->GetCollect("MatriculationTime").bstrVal;
m_timectrlTime.SetFormat(str) ;
}
else
{
if (SelectType==1)
{
MessageBox("该学号的学生记录不存在","基本信息");
}
m_strNativePlace = _T("");
m_nAge = 0;
m_strName = _T("");
m_strSex = _T("");
m_strID = _T("");
}
SelectType=0;
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -