3.9.5 用户管理界面的实现(2)
第4行代码实现打开用户信息表。
第8~17行代码实现列表框数据的显示,显示样式为"用户名+权限"。
当用户选中列表框某一行记录后,单击【注销用户】按钮,可实现对该用户的删除操作。按钮实现代码参考如下:
代码位置:见光盘中本章源代码的SYSDLG类。
- 1 void SYSDLG::OnDeluser()
- 2 {
- 3 if(m_list.GetCurSel()==-1) //如果没有选中
- 4 {
- 5 AfxMessageBox("请选择要删除的记录!");
- 6 return;
- 7 }
- 8 else
- 9 {
- 10 if(MessageBox("确定要删除吗?","提醒!",MB_YESNO)==IDYES)
- 11 {
- 12 CString tempstr;
- 13 _variant_t va;
- 14 m_list.GetText(m_list.GetCurSel(),tempstr); //获取列表框选项
- 15 CString temp=tempstr.Left(tempstr.GetLength()-8); //获取固定长度字符
- 16 CString str;
- 17 str.Format("delete from tb_operator where opername like'%s%%'",temp); //删除语句
- 18 m_pRecordset =m_pConnection->Execute(_bstr_t(str),&va,adCmdText); //执行删除
- 19 AfxMessageBox("删除成功!!");
- 20 ShowData(); //在列表框中显示数据
- 21 }
- 22 else
- 23 return;
- 24 }
- 25 }
第3~7行代码实现判断用户是否选中列表框,GetCurSel()返回值为 1表示没有选中记录。
第10行代码实现提醒对话框显示,让用户确认删除。
第11~21行代码实现根据用户名来删除用户。其中,第20行代码实现列表框中用户的更新显示。
用户选中列表框某一行记录后,输入原密码、新密码及确认密码也可以实现修改密码。实现代码参考如下:
代码位置:见光盘中本章源代码的SYSDLG类。
- 1 void SYSDLG::OnModipass()
- 2 {
- 3 UpdateData(); //保存数据
- 4 CString tempstr;
- 5 CString temp;
- 6 CString oldpass;
- 7 if(m_list.GetCurSel()==-1) //如果没有选中
- 8 {
- 9 MessageBox("请选择要修改的记录!");
- 10 return;
- 11 }
- 12 else
- 13 {
- 14 m_list.GetText(m_list.GetCurSel(),tempstr); //获取列表框选项
- 15 temp=tempstr.Left(tempstr.GetLength()-8); //取固定长度字符
- 16 _variant_t va;
- 17 m_pRecordset =m_pConnection->Execute("select * from tb_operator
- 18 order by opername",&va,adCmdText); //执行排序操作
- 19 while(!m_pRecordset->_EOF) //记录没移动到最后
- 20 {
- 21 CString str1 = ((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect ("opername"));
- 22 CString str2 = ((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("operlevel"));
- 23 str1.TrimRight(); //去掉字符右边空格
- 24 str2.TrimRight();
- 25 if(str1==temp) //查找到当前用户
- 26 {
- 27 UpdateData();
- 28 oldpass = ((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect
- ("operpassword"));
- 29 oldpass.TrimRight(); //去掉字符右边的空格
- 30 UpdateData(false);
- 31 break;
- 32
- 33 }
- 34 m_pRecordset->MoveNext(); //记录后移
- 35 }
- 36 }