3.8.4 用户管理模块
用户管理模块的功能是对系统内的用户信息进行管理。具体实现流程如下。
(1) 创建一个对话框资源IDD_USER_MANAGE_DLG,添加的控件如图3-15所示。
|
| 图3-15 用户管理设计界面 |
(2) 双击对话框,创建一个新类CUserManageDlg,对应代码如下:- class CUserManageDlg : public CDialog
- {
- // Construction
- public:
- CUserManageDlg(CWnd* pParent = NULL);
// standard constructor - _RecordsetPtr m_pRecordset;
- void RefushListBox();
(3) 给"添加"按钮添加消息响应函数,该函数的功能是向系统内添加新的用户信息。具体实现代码如下:
- void CUserManageDlg::OnAdd()
- {
- UpdateData(); //数据的更新
- if(m_strUsers == "") //条件的判断
- {
- MessageBox("请输入用户名!");
- return ;
- }
- if(m_strPwd1 == "")
- {
- MessageBox("请输入密码!");
- return ;
- }
- if(m_strPwd1 != m_strPwd2)
- {
- MessageBox("两次密码输入的不一致!");
- return ;
- }
- int nType = m_TyprCtrl.GetCurSel();//获取选中的索引
- CString strSQL;//定义字符串变量
- strSQL.Format("insert into users values ('%s',
'%s', %d)", m_strUsers, m_strPwd1, nType); - try
- {
- theApp.m_pConnection->Execute((_bstr_t)strSQL,
NULL, adCmdText); - }
- catch(...)
- {
- MessageBox("用户名已经存在!");
- return ;
- }
- RefushListBox();
- OnClean();
- }
(4) 给"注销"按钮添加消息响应函数,该函数的功能是注销某登录用户的信息。具体实现代码如下:
- void CUserManageDlg::OnDel()
- {
- UpdateData(); //数据的更新
- if(m_strUsers == "")
- {
- MessageBox("请选择一个用户!");
- return ;
- }
- CString strSQL; //定义字符串变量
- strSQL.Format("delete from users where users = '%s'", m_strUsers);
- if(MessageBox("确定删除吗?", "注意", MB_YESNO) == IDYES)
- {
- try
- {
- theApp.m_pConnection->Execute((_bstr_t)
strSQL, NULL, adCmdText); - }
- CATCH_ERROR;
- RefushListBox();
- OnClean();
- }
- }