⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usermanage.cpp

📁 这是本人的课程设计
💻 CPP
字号:
// UserManage.cpp : implementation file
//

#include "stdafx.h"
#include "Electric.h"
#include "UserManage.h"

#include "afxdb.h"
#include "AddUser.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUserManage dialog


CUserManage::CUserManage(CWnd* pParent /*=NULL*/)
	: CDialog(CUserManage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUserManage)
	//}}AFX_DATA_INIT
}


void CUserManage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUserManage)
	DDX_Control(pDX, IDC_USER_LIST, m_UserList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CUserManage, CDialog)
	//{{AFX_MSG_MAP(CUserManage)
	ON_BN_CLICKED(IDC_ADD_USER, OnAddUser)
	ON_BN_CLICKED(IDC_EDIT_USER, OnEditUser)
	ON_NOTIFY(NM_CLICK, IDC_USER_LIST, OnClickUserList)
	ON_BN_CLICKED(IDC_DELETE_UAER, OnDeleteUaer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUserManage message handlers

BOOL CUserManage::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//add the column for the list
	m_UserList.InsertColumn(0,"用户名",LVCFMT_CENTER,100);
	m_UserList.InsertColumn(1,"手机号",LVCFMT_CENTER,100);
	m_UserList.InsertColumn(2,"密码",LVCFMT_CENTER,100);
	m_UserList.InsertColumn(3,"身份证",LVCFMT_CENTER,100);

	//put the record into s
	CDBVariant s0,s1,s2,s3;
	//strTemp is for the record to string.
	CString strPhoneNo,strPhonePW,strIdentify,strName;	 
	/*get field from database*/
	CDatabase db;
	//count the line
	//int nIndex = 0;
	db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=ephone.mdb;");
	CRecordset *rs = new CRecordset(&db);
	rs->Open(CRecordset::snapshot,"select * from user",CRecordset::readOnly);
	while(!rs->IsEOF())
	{
		//get the name
		rs->GetFieldValue(short(0),s0);
		if(s0.m_dwType == DBVT_STRING)
		strName = *s0.m_pstring;
		//get the phoneNo.
		rs->GetFieldValue(short(1),s1);
		if(s1.m_dwType == DBVT_STRING)
		strPhoneNo = *s1.m_pstring;
		//get the phonePW
		rs->GetFieldValue(short(2),s2);
		if(s2.m_dwType == DBVT_STRING)
		strPhonePW = *s2.m_pstring;
		//get the identify no
		rs->GetFieldValue(short(3),s3);
		if(s3.m_dwType == DBVT_STRING)
		strIdentify = *s3.m_pstring;

		//add content for the list
		int nIndex = m_UserList.GetItemCount();
		LV_ITEM   lvitemAdd = {0};
		lvitemAdd.mask = LVIF_TEXT;
		lvitemAdd.iItem = nIndex ;
		lvitemAdd.iSubItem = 0;
		lvitemAdd.pszText =strName.GetBuffer(20);

		if (m_UserList.InsertItem(&lvitemAdd) != -1)
		{ 
		   //add the 2nd item
		   LV_ITEM lvitem = {0};
		   lvitem.mask = LVIF_TEXT;
		   lvitem.iItem = nIndex ;
		   lvitem.iSubItem = 1;
		   lvitem.pszText =strPhoneNo.GetBuffer(20);;
		   m_UserList.SetItem(&lvitem);   
	
		   //add the 3nd item
		   LV_ITEM lvitem1 = {0};
		   lvitem1.mask = LVIF_TEXT;
		   lvitem1.iItem = nIndex ;
		   lvitem1.iSubItem = 2;
		   lvitem1.pszText =strPhonePW.GetBuffer(20);;
		   m_UserList.SetItem(&lvitem1);   
		   //add the 4nd item
		   LV_ITEM lvitem2 = {0};
		   lvitem2.mask = LVIF_TEXT;
		   lvitem2.iItem = nIndex ;
		   lvitem2.iSubItem = 3;
		   lvitem2.pszText =strIdentify.GetBuffer(20);;
		   m_UserList.SetItem(&lvitem2);   
		}
		nIndex++;
		rs->MoveNext();
	}
    rs->Close();
	db.Close();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CUserManage::OnAddUser() 
{
	// TODO: Add your control notification handler code here
	CAddUser addUser;
	addUser.DoModal();
}

void CUserManage::OnEditUser() 
{
	// TODO: Add your control notification handler code here
	CAddUser addUser;
    addUser.DoModal();
	//get the data from db
	CDBVariant s0,s1,s2,s3;
	//strTemp is for the record to string.
	CString strPhoneNo,strPhonePW,strIdentify,strName,sql;	 
	/*get field from database*/
	CDatabase db;
	//get the selected item text
	LV_ITEM lvitem = {0};
	lvitem.iItem = m_nItem;
	lvitem.iSubItem = 0;
	lvitem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
	strPhoneNo = m_UserList.GetItemText(m_nItem,1);

	sql.Format("select * from user where phoneNo='%s'",strPhoneNo);
	//count the line
	db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=ephone.mdb;");
	CRecordset *rs = new CRecordset(&db);
	rs->Open(CRecordset::snapshot,sql,CRecordset::readOnly);
	if(!rs->IsEOF())
	{
		//get the name
		rs->GetFieldValue(short(0),s0);
		if(s0.m_dwType == DBVT_STRING)
		strName = *s0.m_pstring;
		//get the phoneNo.
		rs->GetFieldValue(short(1),s1);
		if(s1.m_dwType == DBVT_STRING)
		strPhoneNo = *s1.m_pstring;
		//get the phonePW
		rs->GetFieldValue(short(2),s2);
		if(s2.m_dwType == DBVT_STRING)
		strPhonePW = *s2.m_pstring;
		//get the identify no
		rs->GetFieldValue(short(3),s3);
		if(s3.m_dwType == DBVT_STRING)
		strIdentify = *s3.m_pstring;
		
	}
	rs->Close();
	db.Close();

	addUser.setContent(strName,strPhoneNo,strPhonePW,strIdentify);
	
}

void CUserManage::OnClickUserList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
   LPNMITEMACTIVATE lpNMItemActivate = (LPNMITEMACTIVATE)pNMHDR;
   if(lpNMItemActivate != NULL)
   {
      m_nItem = lpNMItemActivate->iItem;
   }
	m_UserList.SetItemState(m_nItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);

	*pResult = 0;
}

void CUserManage::OnDeleteUaer() 
{
	// TODO: Add your control notification handler code here

	//strTemp is for the record to string.
	CString strPhoneNo,sql;	 
	/*get field from database*/
	CDatabase db;
	//get the selected item text
	LV_ITEM lvitem = {0};
	lvitem.iItem = m_nItem;
	lvitem.iSubItem = 1;
	lvitem.mask = LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
	strPhoneNo = m_UserList.GetItemText(m_nItem,1);

	m_UserList.DeleteItem(m_nItem);
	
	sql.Format("delete from user where phoneNo='%s'",strPhoneNo);
	db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=ephone.mdb;");
	db.ExecuteSQL(sql);
	MessageBox("删除成功!");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -