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

📄 usermanagement.cpp

📁 学生成绩管理系统 使用C++语言加access数据库的一个小型软件
💻 CPP
字号:
// UserManagement.cpp : implementation file
//

#include "stdafx.h"
#include "Student.h"
#include "UserManagement.h"
#include "StudentDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUserManagement dialog


CUserManagement::CUserManagement(CWnd* pParent /*=NULL*/)
	: CDialog(CUserManagement::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUserManagement)
	
	//}}AFX_DATA_INIT

}


void CUserManagement::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUserManagement)
	DDX_Control(pDX, IDC_STATIC_OLDPSD, m_Static_OPsd);
	DDX_Control(pDX, IDC_STATIC_NEWPSD, m_Static_NPsd);
	DDX_Control(pDX, IDC_EDIT_AGAINPSD, m_AgainPsd);
	DDX_Control(pDX, IDC_EDIT_OLDPSD, m_OldPsd);
	DDX_Control(pDX, IDC_EDIT_NEWPSD, m_NewPsd);
	DDX_Control(pDX, IDC_EDIT_NAME, m_Name);
	DDX_Control(pDX, IDC_STATIC_NAME, m_Static_Name);
	DDX_Control(pDX, IDC_STATIC_AGAINPSD, m_Static_APsd);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CUserManagement, CDialog)
	//{{AFX_MSG_MAP(CUserManagement)
	ON_BN_CLICKED(IDC_RADIO_UPDATEPSD, OnRadioUpdatepsd)
	ON_BN_CLICKED(IDC_RADIO_UPDATENAME, OnRadioUpdatename)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUserManagement message handlers

void CUserManagement::OnOK() 
{
	// TODO: Add extra validation here
	CString strSQL;										// 声明MFC之字符串类的对象,存放用于数据库查询的SQL语句
	CString str,strName;
	int i;
	_variant_t nTemp;

	m_pRecordset_Std.CreateInstance(__uuidof(ADODB::Recordset));//创建记录集对象
	strSQL="SELECT 用户信息.用户名ID, 用户信息.密码ID,姓名 FROM 用户信息";
	try
	{

		m_pRecordset_Std->Open(_bstr_t(strSQL), m_pConnection_Std.GetInterfacePtr(),ADODB::adOpenDynamic,ADODB::adLockOptimistic,ADODB::adCmdText);
	}
	catch (_com_error e)
	{
		CString strError;
		strError.Format("警告:打开数据表时发生异常。错误信息:%s",e.ErrorMessage());
	}
	m_pRecordset_Std->MoveFirst();
	i=0;
	while(!m_pRecordset_Std->adoEOF)
	{
		m_pRecordset_Std->MoveNext();
		i++;
	}

	CStudentDlg *pDlg=(CStudentDlg *)CWnd::GetParent(); 
	pDlg->GetDlgItem(IDC_STATIC_USERID)->GetWindowText(UserID);

	if(i>0)
	{
		m_pRecordset_Std->MoveFirst();
		for (int n=0;n<i;n++)
		{
			nTemp=m_pRecordset_Std->GetCollect("用户名ID");
			str=(LPCSTR)_bstr_t(nTemp);
			if(UserID==str)
				break;
			else
				m_pRecordset_Std->MoveNext();
		}
	}
	UpdateData(true);
	switch (Update)
	{
	case 0:	
		m_pRecordset_Std->Close();
		m_pConnection_Std->Close();
		CDialog::OnOK();
	case 1:
		
		GetDlgItem(IDC_EDIT_NAME)->GetWindowText(strName);
		m_pRecordset_Std->PutCollect("姓名",_variant_t(strName));
		m_pRecordset_Std->Update();
		pDlg->GetDlgItem(IDC_STATIC_NAME)->SetWindowText(strName);
		break;
	case 2:
		CString temp,strTemp,strNew,strAgn;
		GetDlgItem(IDC_EDIT_OLDPSD)->GetWindowText(strTemp);
		nTemp=m_pRecordset_Std->GetCollect("密码ID");
		temp=(LPCSTR)_bstr_t(nTemp);
		if(strTemp==temp)
		{	
			GetDlgItem(IDC_EDIT_NEWPSD)->GetWindowText(strNew);
			GetDlgItem(IDC_EDIT_AGAINPSD)->GetWindowText(strAgn);
			if(strNew==strAgn)
			{
				m_pRecordset_Std->PutCollect("密码ID",_variant_t(strNew));
				m_pRecordset_Std->Update();
				break;
			}
			else
			{
				AfxMessageBox("两次输的密码不一致!");
				m_pRecordset_Std->Close();
				return;
			}
		}
		else
		{
			AfxMessageBox("密码不正确,请重新输入!");
			GetDlgItem(IDC_EDIT_OLDPSD)->SetWindowText("");
			GetDlgItem(IDC_EDIT_NEWPSD)->SetWindowText("");
			GetDlgItem(IDC_EDIT_AGAINPSD)->SetWindowText("");
			m_pRecordset_Std->Close();
			return;
		}
	}
	UpdateData(false);
	MessageBox("修改成功!");
	m_pRecordset_Std->Close();
	m_pConnection_Std->Close();
	CDialog::OnOK();
}

void CUserManagement::OnRadioUpdatename() 
{
	// TODO: Add your control notification handler code here
	m_Static_Name.ShowWindow(1);
	m_Name.ShowWindow(1);
	m_OldPsd.ShowWindow(0);
	m_NewPsd.ShowWindow(0);
	m_AgainPsd.ShowWindow(0);
	m_Static_OPsd.ShowWindow(0);
	m_Static_NPsd.ShowWindow(0);
	m_Static_APsd.ShowWindow(0);
	Update=1;
}

void CUserManagement::OnRadioUpdatepsd() 
{
	// TODO: Add your control notification handler code here
	m_Static_OPsd.ShowWindow(1);
	m_Static_NPsd.ShowWindow(1);
	m_Static_APsd.ShowWindow(1);
	m_OldPsd.ShowWindow(1);
	m_NewPsd.ShowWindow(1);
	m_AgainPsd.ShowWindow(1);
	m_Static_Name.ShowWindow(0);
	m_Name.ShowWindow(0);
	Update=2;
}

BOOL CUserManagement::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	HRESULT hr; 
	m_pRecordset_Std.CreateInstance(__uuidof(ADODB::Recordset)); 			
	try 
	{ 
		hr = m_pConnection_Std.CreateInstance("ADODB.Connection");//创建Connection对象 
		if(SUCCEEDED(hr)) 
		{ 
			hr = m_pConnection_Std->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Student.mdb","","",ADODB::adModeUnknown);///连接数据库
		}
	}
	catch(_com_error e)///捕捉异常 
	{ 
		CString errormessage; 
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); 
		AfxMessageBox(errormessage);///显示错误信息 
	}
	Update=0;
	return TRUE; 						
				//  unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE


}

⌨️ 快捷键说明

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