📄 login.cpp
字号:
// Login.cpp : implementation file
//
#include "stdafx.h"
#include "Des.h"
#include "Login.h"
#include "DesEncrypt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLogin dialog
CLogin::CLogin(CWnd* pParent /*=NULL*/)
: CDialog(CLogin::IDD, pParent)
{
//{{AFX_DATA_INIT(CLogin)
m_Name = _T("");
m_Pow = _T("");
//}}AFX_DATA_INIT
}
void CLogin::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLogin)
DDX_Text(pDX, IDC_EDIT1, m_Name);
DDX_Text(pDX, IDC_EDIT2, m_Pow);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLogin, CDialog)
//{{AFX_MSG_MAP(CLogin)
ON_BN_CLICKED(IDC_BUTTON1, OnOK)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLogin message handlers
void CLogin::OnInitADOConn()
{
try
{
//创建连接对象实例
m_pConnection.CreateInstance("ADODB.Connection");
//设置连接字符串
CString strConnect="DRIVER={Microsoft Access Driver (*.mdb)};\
uid=;pwd=;DBQ=shujuku.mdb;";
//使用Open方法连接数据库
m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}
void CLogin::ExitConnect()
{
//关闭记录集和连接
if(m_pRecordset!=NULL)
m_pRecordset->Close();
m_pConnection->Close();
}
BOOL CLogin::HaveName(CString name, CString pow)
{
//连接数据库
OnInitADOConn();
CString sql;
sql.Format("select * from user where 用户名 = '%s' and 密码 = '%s'",name,pow);
//创建记录集指针对象实例
m_pRecordset.CreateInstance(__uuidof(Recordset));
//打开记录集
m_pRecordset->Open((_bstr_t)sql,m_pConnection.GetInterfacePtr(),adOpenDynamic,
adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
return TRUE;
else
return FALSE;
//断开数据库连接
ExitConnect();
}
void CLogin::OnOK()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_Name.IsEmpty() || m_Pow.IsEmpty())
{
MessageBox("用户名或密码不能为空!");
return;
}
char buf[255];
long len = m_Pow.GetLength();
char key[]={1,2,3,4,5,6,7,8};
memset(buf, 0, sizeof(buf));
strcpy(buf, m_Pow.GetBuffer(0));
CDesEncrypt Des;
Des.DesGo(buf, buf, len, key, sizeof(key), TRUE);
CString str = buf;
BOOL result = HaveName(m_Name,str);
if(result)
CDialog::OnOK();
else
{
MessageBox("用户名或密码错误!");
return;
}
}
void CLogin::OnButton2()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -