////////////////////////////////////////////////////////////////////// //功能:弹出修改记录窗口 //日期:2003.12 ////////////////////////////////////////////////////////////////////// void CMainDlg::OnRadioMod() { // TODO: Add your control notification handler code here m_database.Close();//本对话框断开与数据库的连接 CModifyDlg dlg; dlg.m_database.Open(_T("addresslist")); int i=m_ctrlperson.GetSelectionMark(); CString strSQL; int id=atoi(m_ctrlperson.GetItemText(i,0)); CPersonSet m_recordset; CDBVariant varValue; if(i==-1) { MessageBox("请选择一条要修改的记录!","提示",MB_OK|MB_ICONINFORMATION); } else { int temp=0; strSQL.Format("select * from person where ID=%d",id); m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL); m_recordset.GetFieldValue(temp,varValue); dlg.m_modid=varValue.m_lVal;
m_recordset.GetFieldValue(1,varValue); dlg.m_modname=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(2,varValue); dlg.m_modsex=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(3,varValue); dlg.m_modrelation=varValue.m_pstring->GetBuffer(1); m_recordset.GetFieldValue(4,varValue); dlg.m_modtelephone=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(5,varValue); dlg.m_modhandphone=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(6,varValue); dlg.m_modaddress=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(7,varValue); dlg.m_modworkplace=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(8,varValue); dlg.m_modemail=varValue.m_pstring->GetBuffer(1);
m_recordset.GetFieldValue(9,varValue); dlg.m_modoicq=varValue.m_pstring->GetBuffer(1); //m_database.Close();//此处不能断开与数据库的连接 dlg.DoModal(); RefreshData(); } }
void CMainDlg::RefreshData() { //首先确保数据库打开 if(!m_database.IsOpen()) { m_database.Open(_T("addresslist")); } //对列表控件的内容更新,清空原来的内容 m_ctrlperson.DeleteAllItems(); //创建记录集 CPersonSet m_personset(&m_database); m_personset.Open(AFX_DB_USE_DEFAULT_TYPE,m_query); CDBVariant varValue; char buf[20]; //用来记录当前记录的序号 int i=0; //如果表中有记录,打开后将游标定在第一位,使记录集中的第一条记录成为当前记录 if(m_personset.GetRecordCount()!=0) m_personset.MoveFirst();
while(!m_personset.IsEOF()) { int temp=0; //对整型数字的处理 m_personset.GetFieldValue(temp,varValue); sprintf(buf,"%d",varValue.m_lVal);m_ctrlperson.InsertItem(i,buf); //对字符串显示处理 //m_personset.GetFieldValue(0,varValue); //m_ctrlperson.SetItemText(i,0,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(1,varValue); m_ctrlperson.SetItemText(i,1,varValue.m_pstring->GetBuffer(1)); ///第一个参数是表示行,第二个参数表示 列,第三个表示 值 m_personset.GetFieldValue(2,varValue); m_ctrlperson.SetItemText(i,2,varValue.m_pstring->GetBuffer(1)); m_personset.GetFieldValue(3,varValue); m_ctrlperson.SetItemText(i,3,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(4,varValue); m_ctrlperson.SetItemText(i,4,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(5,varValue); m_ctrlperson.SetItemText(i,5,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(6,varValue); m_ctrlperson.SetItemText(i,6,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(7,varValue); m_ctrlperson.SetItemText(i,7,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(8,varValue); m_ctrlperson.SetItemText(i,8,varValue.m_pstring->GetBuffer(1));
m_personset.GetFieldValue(9,varValue); m_ctrlperson.SetItemText(i,9,varValue.m_pstring->GetBuffer(1)); m_personset.MoveNext(); i++; } //在标题栏中显示共有记录条数 int counts=m_personset.GetRecordCount(); CString str; str.Format("通讯录 V1.0 试用版 目前共有记录数: %d",counts); this->SetWindowText(str); }
在修改对话框中的代码;;
// ModifyDlg.cpp : implementation file //
#include "stdafx.h" #include "Address.h" #include "ModifyDlg.h" #include "PersonSet.h" #include "MainDlg.h"
#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CModifyDlg dialog
CModifyDlg::CModifyDlg(CWnd* pParent /*=NULL*/) : CDialog(CModifyDlg::IDD, pParent) { //{{AFX_DATA_INIT(CModifyDlg) m_modname = _T(""); m_modsex = _T("");///此处表示字符串为空,可以写成m_modsex=""; m_modtelephone = _T(""); m_modhandphone = _T(""); m_modaddress = _T(""); m_modworkplace = _T(""); m_modemail = _T(""); m_modoicq = _T(""); m_modid = 0; m_modrelation = _T(""); //}}AFX_DATA_INIT }
void CModifyDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CModifyDlg) DDX_Text(pDX, IDC_EDIT8, m_modid); DDX_Text(pDX, IDC_EDIT1, m_modname); DDX_CBString(pDX, IDC_COMBO1, m_modsex); DDX_Text(pDX, IDC_EDIT9, m_modrelation); DDX_Text(pDX, IDC_EDIT2, m_modtelephone); DDX_Text(pDX, IDC_EDIT3, m_modhandphone); DDX_Text(pDX, IDC_EDIT4, m_modaddress); DDX_Text(pDX, IDC_EDIT5, m_modworkplace); DDX_Text(pDX, IDC_EDIT6, m_modemail); DDX_Text(pDX, IDC_EDIT7, m_modoicq); //}}AFX_DATA_MAP }
BEGIN_MESSAGE_MAP(CModifyDlg, CDialog) //{{AFX_MSG_MAP(CModifyDlg) ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton) //}}AFX_MSG_MAP END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CModifyDlg message handlers void CModifyDlg::OnOK() { //TODO: Add extra validation here UpdateData(TRUE); CPersonSet m_recordset;// 此处为记录的实体 CString strSQL; CMainDlg dlg; if(!m_modname.IsEmpty()) { strSQL.Format("update person set NAME='%s',SEX='%s',RELATION='%s',TELEPHONE='%s',HANDPHONE='%s',ADDRESS='%s',WORKPLACE='%s',EMAIL='%s',OICQ='%s' where ID=%d" ,m_modname,m_modsex,m_modrelation,m_modtelephone,m_modhandphone,m_modaddress,m_modworkplace,m_modemail,m_modoicq,m_modid); m_database.ExecuteSQL(strSQL);//cibu m_database.Close(); CDialog::OnOK(); } else { MessageBox("“姓名”项不可以为空!","提示",MB_OK|MB_ICONINFORMATION); } }
void CModifyDlg::OnClearButton() { // TODO: Add your control notification handler code here if(MessageBox("是否真的要清空?","提示",MB_YESNO|MB_ICONQUESTION)==IDYES) { CEdit* m_pmodid=(CEdit*)GetDlgItem(IDC_EDIT8); //m_pmodid->SetWindowText(""); //m_modname.Empty(); //m_modsex.Empty(); m_modrelation.Empty(); m_modtelephone.Empty(); m_modhandphone.Empty(); m_modemail.Empty(); m_modaddress.Empty(); m_modworkplace.Empty(); m_modoicq.Empty(); UpdateData(FALSE); } }
void CModifyDlg::OnCancel() { // TODO: Add extra cleanup here //m_database.Close(); CDialog::OnCancel(); }
BOOL CModifyDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here UpdateData(FALSE);//将选中的 数据显示在对话框中 return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }

|