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

📄 dlgloggin.cpp

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgLoggin dialog


CDlgLoggin::CDlgLoggin(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLoggin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgLoggin)
	m_psw = _T("");
	m_userid = _T("");
	//}}AFX_DATA_INIT
}


void CDlgLoggin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgLoggin)
	DDX_Text(pDX, IDC_EDIT_PSW, m_psw);
	DDV_MaxChars(pDX, m_psw, 14);
	DDX_Text(pDX, IDC_EDIT_USERID, m_userid);
	DDV_MaxChars(pDX, m_userid, 14);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgLoggin, CDialog)
	//{{AFX_MSG_MAP(CDlgLoggin)
	ON_BN_CLICKED(IDC_BUTTON_REGIST, OnButtonRegist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgLoggin message handlers



void CDlgLoggin::OnOK() 
{	

	// TODO: Add extra validation here
	UpdateData(true);
	if(m_psw.GetLength()==0||m_userid.GetLength()==0)
	{
		AfxMessageBox("请输入用户名或密码");
		return;
	}
	for( int i = 0 ; i< m_psw.GetLength(); i++) 
	{	
		if(!((m_psw[i]>='a' && m_psw[i]<='z')||(m_psw[i]>='A' && m_psw[i]<='Z')|| (m_psw[i]>='0' && m_psw[i]<='9')))
		{
		//	GetDlgItem(IDC_EDIT_PSW)->SetWindowText("");
		//	GetDlgItem(IDC_EDIT_USERID)->SetWindowText("");
			AfxMessageBox("不接受特殊字符!");
			return;
		}
	}

	for( int n = 0 ; n< m_userid.GetLength(); n++) 
	{	
		if(!((m_userid[n]>='a' && m_userid[n]<='z')||(m_userid[n]>='A' && m_userid[n]<='Z')|| (m_userid[n]>='0' && m_userid[n]<='9')))
		{
			//GetDlgItem(IDC_EDIT_PSW)->SetWindowText("");
			//GetDlgItem(IDC_EDIT_USERID)->SetWindowText("");
			AfxMessageBox("不接受特殊字符!");
			return;
		}
	}

	CString strSQL;										// 声明MFC之字符串类的对象,存放用于数据库查询的SQL语句
	CString str;
//	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++;
	}
	n = i;

	if (n > 0)
	{	
		m_pRecordset_Std->MoveFirst();
		for(i=0;i<n;i++)
		{	
			_variant_t nTemp;
			nTemp=m_pRecordset_Std->GetCollect("用户名ID");
			str=(LPCSTR)_bstr_t(nTemp);
			userid=str;
			nTemp=m_pRecordset_Std->GetCollect("密码ID");
			str=(LPCSTR)_bstr_t(nTemp);
			psw=str;
			nTemp=m_pRecordset_Std->GetCollect("姓名");
			str=(LPCSTR)_bstr_t(nTemp);
			username=str;
			if (m_userid==userid && m_psw==psw)
			{
				SuccessLog=1;
				m_pRecordset_Std->Close();
				break;
			}
			else
			{
				m_pRecordset_Std->MoveNext();
			}
		}
	}
	UpdateData(false);
	if (SuccessLog!=1)
	{	
		m_pRecordset_Std->Close();
		AfxMessageBox("无此用户或密码错误!");
		return;
	}
	CDialog::OnOK();
}


void CDlgLoggin::OnCancel() 
{
	// TODO: Add extra cleanup here
	m_pConnection_Std->Close();
	exit(1);
}

BOOL CDlgLoggin::OnInitDialog() 
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
//	CString strSQL;
	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);///显示错误信息 
	}							
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgLoggin::OnButtonRegist() 
{
	// TODO: Add your control notification handler code here

	CDlgRegist DlgReg;
	CDialog::OnOK();
	DlgReg.DoModal();
	
}

⌨️ 快捷键说明

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