3.8.1 设计员工模块(4)
(7) 因为树形控件中需要显示部门信息又要显示员工信息,而员工信息是根据部门来划分的,所以在这里使用了两层的循环语句。第一个whlie循环读取部门信息,根据读取到的部门信息再遍历记录集读取相应的员工信息。双击"删除"按钮添加消息响应函数,实现员工信息的删除,代码如下:
- void CEmpInfoManageDlg::OnDel()
- {
- if(theApp.m_strEmpID == "")
//员工编号不能为空 - {
- MessageBox("请选择一条信息!");
//信息提示 - return ;
- }
- CString strSQL;
//定义字符串变量 - strSQL.Format("delete from emp where id = %s",
theApp.m_strEmpID); - if(MessageBox("确定删除吗?", "注意", MB_YESNO)
== IDYES) //消息提示 - {
- try
- {
- theApp.m_pConnection->Execute((_bstr_t)
strSQL, NULL, adCmdText); - MessageBox("删除成功!");
- ShowTreeData();
- m_ListCrtl.DeleteAllItems();
- }
- CATCH_ERROR;
- }
- }
3. 修改员工信息
此部分与员工信息添加相类似,区别在于"修改"按钮的消息响应函数。"修改"按钮的消息响应函数OnUpdate( )代码如下:
- void CEmpUpdateDlg::OnUpdate()
- {
- UpdateData();
- //数据更新
- if(m_strName == "")
- //条件的判断
- {
- MessageBox("姓名不能为空!");
- //信息提示
- return ;
- }
- if(m_nAge == 0)
- {
- MessageBox("请设置年龄!");
- return ;
- }
- CString strSex, strDepart, strBirth, strAge;
- //定义字符串变量
- strBirth.Format("%d-%d-%d", m_Birth.GetYear(),
- m_Birth.GetMonth(), m_Birth. GetDay());
- GetDlgItem(IDC_COMBO1)->GetWindowText(strSex);
- //获取组合框文本
- GetDlgItem(IDC_COMBO2)->GetWindowText(strDepart);
- //获取组合框文本
- strAge.Format("%d", m_nAge);
- m_pRecordset->PutCollect("name",_variant_t(m_strName));
- //修改姓名
- m_pRecordset->PutCollect("age",_variant_t((long)m_nAge));
- //修改年龄
- m_pRecordset->PutCollect("sex",_variant_t(strSex));
- //修改性别
- m_pRecordset->PutCollect("addr",_variant_t(m_strAddr));
- //修改住址
- m_pRecordset->PutCollect("depart",_variant_t(strDepart));
- //修改部门
- m_pRecordset->PutCollect("birth",_variant_t(strBirth));
- //修改生日
- m_pRecordset->PutCollect("phone",_variant_t(m_strPhone));
- //修改电话
- m_pRecordset->PutCollect("more",_variant_t(m_strMore));
- //修改备注
- char *pBuf = m_pBMPBuffer;
- VARIANT varBLOB;
- SAFEARRAY *psa;
- SAFEARRAYBOUND rgsabound[1];
- if(pBuf)
- {
- rgsabound[0].lLbound = 0;
- rgsabound[0].cElements = m_nFileLen;
- psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
- for (long i = 0; i < (long)m_nFileLen; i++)
- SafeArrayPutElement (psa, &i, pBuf++);
- varBLOB.vt = VT_ARRAY | VT_UI1;
- varBLOB.parray = psa;
- m_pRecordset->GetFields()->GetItem("photo")-
- >AppendChunk(varBLOB);
- //修改图片数据
- }
- m_pRecordset->Update();
- //更新记录集
- MessageBox("修改成功!");
- }
在上述代码中,对数据库的修改并不是通过SQL语句进行的,而是先通过PutCollect( )函数修改相应的字段,然后调用Update( )函数完成数据库的更新。读者可以将其与员工信息添加的代码进行对比来掌握。
4. 员工查询
(1) 插入一个对话框资源,ID为IDD_EMP_INDEX_DLG,如图3-9所示。
|
| 图3-9 员工查询 |