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

📄 dlgregist.cpp

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

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

/////////////////////////////////////////////////////////////////////////////
// CDlgRegist dialog

	
CDlgRegist::CDlgRegist(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgRegist::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgRegist)
	m_rname = _T("");
	m_ruserid = _T("");
	m_rpsw = _T("");
	//}}AFX_DATA_INIT
}


void CDlgRegist::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgRegist)
	DDX_Text(pDX, IDC_EDIT_RNAME, m_rname);
	DDV_MaxChars(pDX, m_rname, 10);
	DDX_Text(pDX, IDC_EDIT_RUSERID, m_ruserid);
	DDV_MaxChars(pDX, m_ruserid, 14);
	DDX_Text(pDX, IDC_EDIT_RPSW, m_rpsw);
	DDV_MaxChars(pDX, m_rpsw, 14);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgRegist, CDialog)
	//{{AFX_MSG_MAP(CDlgRegist)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgRegist message handlers

void CDlgRegist::OnOK() 
{
	// TODO: Add extra validation here
	CString strSQL1,strSQL2;
	CString ruserid,rpsw,rname;
	CString str;
	CDlgLoggin DlgLog;

	UpdateData(true);
	if(m_rpsw.GetLength()==0||m_ruserid.GetLength()==0||m_rname.GetLength()==0)
	{
		AfxMessageBox("请输入用户名、密码和姓名");
		return;
	}
	for( int n = 0 ; n< m_ruserid.GetLength(); n++) 
	{	
		if(!((m_ruserid[n]>='a' && m_ruserid[n]<='z')||(m_ruserid[n]>='A' && m_ruserid[n]<='Z')|| (m_ruserid[n]>='0' && m_ruserid[n]<='9')))
		{
			AfxMessageBox("用户名只能是字母或数字!");
			return;
		}
	}
	for( int i = 0 ; i< m_rpsw.GetLength(); i++) 
	{	
		if(!((m_rpsw[i]>='a' && m_rpsw[i]<='z')||(m_rpsw[i]>='A' && m_rpsw[i]<='Z')|| (m_rpsw[i]>='0' && m_rpsw[i]<='9')))
		{
			AfxMessageBox("密码只能是字母或数字!");
			return;
		}
	}
	ruserid=m_ruserid;
	rpsw=m_rpsw;
	rname=m_rname;
	_variant_t nTemp;
	UpdateData(false);
	strSQL1="INSERT INTO 用户信息(用户名ID,密码ID,姓名) VALUES('"+ruserid+"','"+rpsw+"','"+rname+"')";
	strSQL2="SELECT 用户信息.用户名ID, 用户信息.密码ID FROM 用户信息";

	try
	{

		m_pRecordset_Std->Open(_bstr_t(strSQL2), 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();
	while(!m_pRecordset_Std->adoEOF)
	{
		
		nTemp=m_pRecordset_Std->GetCollect("用户名ID");
		str=(LPCSTR)_bstr_t(nTemp);
		if (str==m_ruserid)
		{
			ExistID=true;
			break;
		}
		else
			m_pRecordset_Std->MoveNext();
	}
	if(ExistID==true)
	{
		AfxMessageBox("用户名已存在,请重新输入!");	
		return;
	}
	else
	{	
		try
		{
		m_pConnection_Std->Execute((_bstr_t)strSQL1,&nTemp,ADODB::adCmdText); 
		}
		catch (_com_error e)
		{
		CString strError;
		strError.Format("警告:打开数据表时发生异常。错误信息:%s",e.ErrorMessage());
		}
		AfxMessageBox("注册成功!");
	}
	CDialog::OnOK();
	DlgLog.DoModal();
}

BOOL CDlgRegist::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 CDlgRegist::OnCancel() 
{
	// TODO: Add extra cleanup here
	CDlgLoggin DlgLog;
	m_database.Close();
	CDialog::OnOK();
	DlgLog.DoModal();
}

⌨️ 快捷键说明

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