4.5.5 职工修改的实现
本节介绍职工信息修改的实现,此部分与职工信息添加相类似,区别只是【修改】按钮的消息响应函数。其他部分可参考职工信息添加的内容。【修改】按钮的消息响应函数OnUpdate()代码如下:
代码位置:见光盘中本章源代码的CEmpUpdateDlg类。
- 1 void CEmpUpdateDlg::OnUpdate()
- 2 {
- 3 UpdateData();
//数据更新 - 4 if(m_strName == "")
//条件的判断 - 5 {
- 6 MessageBox("姓名不能为空!");
//信息提示 - 7 return ;
- 8 }
- 9 if(m_nAge == 0)
- 10 {
- 11 MessageBox("请设置年龄!");
- 12 return ;
- 13 }
- 14 CString strSex, strDepart, strBirth, strAge;
//定义字符串变量 - 15 strBirth.Format("%d-%d-%d", m_Birth.GetYear(),
- m_Birth.GetMonth(), m_Birth. GetDay());
- 16 GetDlgItem(IDC_COMBO1)->GetWindowText(strSex);
//获取组合框文本 - 17 GetDlgItem(IDC_COMBO2)->GetWindowText(strDepart);
//获取组合框文本 - 18 strAge.Format("%d", m_nAge);
- 19 m_pRecordset->PutCollect("name",_variant_t(m_strName));
//修改姓名 - 20 m_pRecordset->PutCollect("age",_variant_t((long)m_nAge));
//修改年龄 - 21 m_pRecordset->PutCollect("sex",_variant_t(strSex));
//修改性别 - 22 m_pRecordset->PutCollect("addr",_variant_t(m_strAddr));
//修改住址 - 23 m_pRecordset->PutCollect("depart",_variant_t(strDepart));
//修改系别 - 24 m_pRecordset->PutCollect("birth",_variant_t(strBirth));
//修改生日 - 25 m_pRecordset->PutCollect("phone",_variant_t(m_strPhone));
//修改电话 - 26 m_pRecordset->PutCollect("more",_variant_t(m_strMore));
//修改备注 - 27 char *pBuf = m_pBMPBuffer;
- 28 VARIANT varBLOB;
- 29 SAFEARRAY *psa;
- 30 SAFEARRAYBOUND rgsabound[1];
- 31 if(pBuf)
- 32 {
- 33 rgsabound[0].lLbound = 0;
- 34 rgsabound[0].cElements = m_nFileLen;
- 35 psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
- 36 for (long i = 0; i < (long)m_nFileLen; i++)
- 37 SafeArrayPutElement (psa, &i, pBuf++);
- 38 varBLOB.vt = VT_ARRAY | VT_UI1;
- 39 varBLOB.parray = psa;
- 40 m_pRecordset->GetFields()->GetItem("photo")-
>AppendChunk(varBLOB);
//修改图片数据 - 41 }
- 42 m_pRecordset->Update();
//更新记录集 - 43 MessageBox("修改成功!");
- 44 }
第3~18行代码为修改数据的条件判断,不能为空,实现了初始数据获取。
第19~26行代码实现了对基本资料的修改操作。
第27~43行代码实现了对图片资料的修改操作。
上述代码对数据库的修改并不是通过SQL语句进行的,先通过PutCollect()函数修改相应的字段,然后调用Update()函数完成数据库的更新。读者可以将其与职工信息添加的代码进行对比,来掌握它们。
【责任编辑:
云霞 TEL:(010)68476606】