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

📄 login.cpp

📁 本人工作中的一个软件开发实例。里面包含了数据库
💻 CPP
字号:
// Login.cpp : implementation file
//

#include "stdafx.h"
#include "oil.h"
#include "Login.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern  COILApp  theApp;
/////////////////////////////////////////////////////////////////////////////
// CLogin dialog


CLogin::CLogin(CWnd* pParent /*=NULL*/)
	: CDialog(CLogin::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLogin)
	m_strName = _T("");
	m_strPwd = _T("");
	//}}AFX_DATA_INIT
}


void CLogin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLogin)
	DDX_Text(pDX, IDC_NAME, m_strName);
	DDX_Text(pDX, IDC_PWD, m_strPwd);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CLogin message handlers

//登陆
void CLogin::OnOK() 
{
	UpdateData();
	{   CString sql="SELECT * FROM operator \
	                 where OperatorID='"+m_strName+"' and \
					       operatorpassword='"+m_strPwd+"'";
		try
		{ 		//打开记录集
		    	m_pRecordset.CreateInstance("ADODB.Recordset");
				m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
		
				//密码或用户名错误
				if(m_pRecordset->adoEOF)
				{   m_pRecordset->Close();				    
				    AfxMessageBox("请输入正确的用户名及密码");
					m_strPwd=_T("");
					UpdateData(false);
					GetDlgItem(IDC_PWD)->SetFocus();
					return;
				}
				else
				{   			
					//设置全局变量
					theGlobal.strUserID=m_strName;

					//关闭记录集
					if(m_pRecordset->State&&m_pRecordset!=NULL)
							m_pRecordset->Close();
					m_pRecordset=NULL;
					CDialog::OnOK();
				}
		}
		catch(_com_error *e)
		{   
			CString tmp;
			tmp.Format("数据库联接错误:%s",e->ErrorMessage());
			
			AfxMessageBox(tmp);
		}
		catch(...)
		{
			AfxMessageBox("数据库结构错误");
		}
    }
}

//退出系统
void CLogin::OnCancel() 
{
	// TODO: Add extra cleanup here
    ::PostMessage(::AfxGetMainWnd()->m_hWnd,WM_CLOSE,0,0);
	CDialog::OnCancel();
}

//初始化对话框
BOOL CLogin::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	GetDlgItem(IDC_NAME)->SetFocus();
    UpdateData(false);
	return TRUE;  // 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 + -