mfc + access 用户验证程序(初级数据库编程) 企业即时通讯 软件流程:弹出对话框,要求输入用户及密码,正确则跳到主对话框,错误则要求重新输入,关闭则什么也不做退出。三无程序,有用的或刚学ado的看看。 ////////////////////////////////////////// s
mfc + access 用户验证程序(初级数据库编程)
企业即时通讯
软件流程:弹出对话框,要求输入用户及密码,正确则跳到主对话框,错误则要求重新输入,关闭则什么也不做退出。三无程序,有用的或刚学ado的看看。
////////////////////////////////////////// stdafx.h
// stdafx.h : include file for standard system include files,// or project specific include files that are used frequently, but// are changed infrequently//
#if !defined(afx_stdafx_h__b170e8a6_57b5_4b19_a471_c368c30871f1__included_)#define afx_stdafx_h__b170e8a6_57b5_4b19_a471_c368c30871f1__included_
#if _msc_ver > 1000#pragma once#endif // _msc_ver > 1000
#define vc_extralean // exclude rarely-used stuff from windows headers
#include // mfc core and standard components#include // mfc extensions#include // mfc automation classes#include // mfc support for internet explorer 4 common controls#ifndef _afx_no_afxcmn_support#include // mfc support for windows common controls#endif // _afx_no_afxcmn_support
#include #import c:/program files/common files/system/ado/msado15.dll no_namespace rename(eof,adoeof)
//{{afx_insert_location}}// microsoft visual c++ will insert additional declarations immediately before the previous line.
#endif // !defined(afx_stdafx_h__b170e8a6_57b5_4b19_a471_c368c30871f1__included_)---------------------------------------------------------------------------------------------------------------------------------------------------
#if !defined(afx_logindlg_h__c757dd49_652b_41a1_84c3_ec0d3c40a07f__included_)#define afx_logindlg_h__c757dd49_652b_41a1_84c3_ec0d3c40a07f__included_
#if _msc_ver > 1000#pragma once#endif // _msc_ver > 1000// logindlg.h : header file//
/////////////////////////////////////////////////////////////////////////////// clogindlg dialog
class clogindlg : public cdialog{// constructionpublic: bool uservalid(); clogindlg(cwnd* pparent = null); // standard constructor
// dialog data //{{afx_data(clogindlg) enum { idd = idd_dialog1 }; // note: the classwizard will add data members here //}}afx_data
// overrides // classwizard generated virtual function overrides //{{afx_virtual(clogindlg) protected: virtual void dodataexchange(cdataexchange* pdx); // ddx/ddv support //}}afx_virtual
// implementationprotected:
// generated message map functions //{{afx_msg(clogindlg) virtual void onok(); virtual void oncancel(); virtual bool oninitdialog(); //}}afx_msg declare_message_map()};
//{{afx_insert_location}}// microsoft visual c++ will insert additional declarations immediately before the previous line.
#endif // !defined(afx_logindlg_h__c757dd49_652b_41a1_84c3_ec0d3c40a07f__included_)
---------------------------------------------------------------------------------------------------------------------------------------------------
// logindlg.cpp : implementation file//
#include stdafx.h#include userlogin.h#include logindlg.h#include userlogindlg.h
#ifdef _debug#define new debug_new#undef this_filestatic char this_file[] = __file__;#endif
/////////////////////////////////////////////////////////////////////////////// clogindlg dialog
clogindlg::clogindlg(cwnd* pparent /*=null*/) : cdialog(clogindlg::idd, pparent){ //{{afx_data_init(clogindlg) // note: the classwizard will add member initialization here //}}afx_data_init}
void clogindlg::dodataexchange(cdataexchange* pdx){ cdialog::dodataexchange(pdx); //{{afx_data_map(clogindlg) // note: the classwizard will add ddx and ddv calls here //}}afx_data_map}
begin_message_map(clogindlg, cdialog) //{{afx_msg_map(clogindlg) //}}afx_msg_mapend_message_map()
/////////////////////////////////////////////////////////////////////////////// clogindlg message handlers
void clogindlg::onok() { // todo: add extra validation here/* char *szbuf = new char[128]; getdlgitemtext(idc_edit1, szbuf, 128); messagebox(szbuf);*/ if (! uservalid()) return;
cdialog::onok();}
void clogindlg::oncancel() { // todo: add extra cleanup here
cdialog::oncancel();}
bool clogindlg::uservalid(){ _connectionptr &cptr = ((cuserlogindlg*)getparent())->m_pconn; char* szusername = (char*)((cuserlogindlg*)getparent())->m_szusername;
_recordsetptr prs; prs.createinstance(__uuidof(recordset));
// 确保用户名输入不为空 char szname[128]; getdlgitemtext(idc_edit1, szname, 128); if (null == szname[0]) { afxmessagebox(用户名不能为空!); return false; } char szpw[128]; getdlgitemtext(idc_edit2, szpw, 128); if (null == szpw[0]) { afxmessagebox(密码不能为空!); return false; }
// 从 account 表里读出用户信息 char szsql[256]; sprintf(szsql, %s%s, select * from account where username=', szname); strcat(szsql, ');
try { hresult hr = prs->open(szsql, cptr.getinterfaceptr(), adopendynamic, adlockoptimistic, adcmdtext);
// 确保用户存在 if (prs->adoeof) { afxmessagebox(用户名无效,请重新输入!); prs->close(); prs.release(); return false; } }
catch (_com_error e) { afxmessagebox(e.errormessage()); }
cstring l_str; variant_t var;
try { var = prs->getcollect(_variant_t(password)); if (var.vt != vt_null) { l_str = (lpctstr)_bstr_t(var); if (l_str == szpw) { // afxmessagebox(密码正确); strcpy(szusername, szname); } else { afxmessagebox(密码错误,请重新输入!); return false; } } else { l_str = _t(none); afxmessagebox(l_str); return false; } } catch (_com_error e) { afxmessagebox(e.description()); } prs->close(); prs.release();}
bool clogindlg::oninitdialog() { cdialog::oninitdialog(); // todo: add extra initialization here return true; // return true unless you set the focus to a control // exception: ocx property pages should return false}
--------------------------------------------------------------------------------------------------------------------------------------------------
// userlogin.h : main header file for the userlogin application//
#if !defined(afx_userlogin_h__433a5c98_5c19_49a2_8aca_5ad5ff116d77__included_)#define afx_userlogin_h__433a5c98_5c19_49a2_8aca_5ad5ff116d77__included_
#if _msc_ver > 1000#pragma once#endif // _msc_ver > 1000
#ifndef __afxwin_h__ #error include 'stdafx.h' before including this file for pch#endif
#include resource.h // main symbols
/////////////////////////////////////////////////////////////////////////////// cuserloginapp:// see userlogin.cpp for the implementation of this class//
class cuserloginapp : public cwinapp{public: cuserloginapp();
// overrides // classwizard generated virtual function overrides //{{afx_virtual(cuserloginapp) public: virtual bool initinstance(); //}}afx_virtual
// implementation
//{{afx_msg(cuserloginapp) // note - the classwizard will add and remove member functions here. // do not edit what you see in these blocks of generated code ! //}}afx_msg declare_message_map()};
/////////////////////////////////////////////////////////////////////////////
//{{afx_insert_location}}// microsoft visual c++ will insert additional declarations immediately before the previous line.
#endif // !defined(afx_userlogin_h__433a5c98_5c19_49a2_8aca_5ad5ff116d77__included_)----------------------------------------------------------------------------------------------------------------------------------------------
// userlogin.cpp : defines the class behaviors for the application.//
#include stdafx.h#include userlogin.h#include userlogindlg.h
#ifdef _debug#define new debug_new#undef this_filestatic char this_file[] = __file__;#endif
/////////////////////////////////////////////////////////////////////////////// cuserloginapp
begin_message_map(cuserloginapp, cwinapp) //{{afx_msg_map(cuserloginapp) // note - the classwizard will add and remove mapping macros here. // do not edit what you see in these blocks of generated code! //}}afx_msg on_command(id_help, cwinapp::onhelp)end_message_map()
/////////////////////////////////////////////////////////////////////////////// cuserloginapp construction
cuserloginapp::cuserloginapp(){ // todo: add construction code here, // place all significant initialization in initinstance}
/////////////////////////////////////////////////////////////////////////////// the one and only cuserloginapp object
cuserloginapp theapp;
/////////////////////////////////////////////////////////////////////////////// cuserloginapp initialization
bool cuserloginapp::initinstance(){ oleinitialize(null); afxenablecontrolcontainer();
// standard initialization // if you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.
#ifdef _afxdll enable3dcontrols(); // call this when using mfc in a shared dll#else enable3dcontrolsstatic(); // call this when linking to mfc statically#endif
cuserlogindlg dlg; m_pmainwnd = &dlg; int nresponse = dlg.domodal(); if (nresponse == idok) { // todo: place code here to handle when the dialog is // dismissed with ok } else if (nresponse == idcancel) { // todo: place code here to handle when the dialog is // dismissed with cancel }
// since the dialog has been closed, return false so that we exit the // application, rather than start the application's message pump. return false;}