📄 viewuser.cpp
字号:
// ViewUser.cpp : implementation file
//
#include "stdafx.h"
#include "Kvip.h"
#include "ViewUser.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "MainFrm.H"
#include "DlgUserTx.H"
#include "DlgUserCopyTo.H"
/////////////////////////////////////////////////////////////////////////////
// CViewUser
IMPLEMENT_DYNCREATE(CViewUser, CFormView)
CViewUser::CViewUser()
: CFormView(CViewUser::IDD)
{
//{{AFX_DATA_INIT(CViewUser)
m_strUser = _T("");
//}}AFX_DATA_INIT
m_pApp = NULL;
m_strSQL = _T("");
}
CViewUser::~CViewUser()
{
}
void CViewUser::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CViewUser)
DDX_Control(pDX, IDC_MASTER, m_Master);
DDX_Text(pDX, IDC_USER, m_strUser);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CViewUser, CFormView)
//{{AFX_MSG_MAP(CViewUser)
ON_BN_CLICKED(IDC_RETRIEVE, OnRetrieve)
ON_BN_CLICKED(IDC_COPYTO, OnCopyto)
ON_BN_CLICKED(IDC_APPEND, OnAppend)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_EDIT, OnEdit)
ON_BN_CLICKED(IDC_CANCEL, OnCancel)
ON_NOTIFY(NM_DBLCLK, IDC_MASTER, OnDblclkMaster)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CViewUser diagnostics
#ifdef _DEBUG
void CViewUser::AssertValid() const
{
CFormView::AssertValid();
}
void CViewUser::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CViewUser message handlers
void CViewUser::OnRetrieve()
{
// TODO: Add your control notification handler code here
UpdateData();
//整理
m_strUser.MakeUpper();
m_strUser.TrimLeft();
m_strUser.TrimRight();
//合成SQL
if (m_strUser.GetLength())
{
m_strSQL.Format("Select * from kv_user Where us_name like '%%%s%%'",
m_strUser);
}
else
{
m_strSQL= "Select * from kv_user";
}
//检测用户
LoadData(m_strSQL);
}
void CViewUser::OnCopyto()
{
// TODO: Add your control notification handler code here
CDlgUserCopyTo dlg;
int nItem;
POSITION pos = m_Master.GetFirstSelectedItemPosition();
if (pos == 0)
{
MessageBox("提示:请先选择复制的用户!", "系统提示", MB_OK|MB_ICONEXCLAMATION);
return;
}
if(pos) nItem = m_Master.GetNextSelectedItem(pos);
if (nItem < 0 )
{
MessageBox("提示:请先选择复制的用户!", "系统提示", MB_OK|MB_ICONEXCLAMATION);
return;
}
dlg.m_pParent = this;
dlg.m_nItem = nItem;
//dlg.m_strCode = m_Master.GetItemText(nItem, 1);
dlg.DoModal();
}
void CViewUser::OnAppend()
{
// TODO: Add your control notification handler code here
CDlgUserTx dlg;
int nItem = 0;
CString strUser;
POSITION pos = m_Master.GetFirstSelectedItemPosition();
if(pos)
{
nItem = m_Master.GetNextSelectedItem(pos);
if (nItem < 0 ) nItem = 0;
}
dlg.m_nItem = nItem;
dlg.m_pParent = this;
dlg.m_bAppend = TRUE;
dlg.DoModal();
}
void CViewUser::OnDelete()
{
// TODO: Add your control notification handler code here
POSITION pos = m_Master.GetFirstSelectedItemPosition();
CString strCode;
CString strSQL;
if(pos) //如果选中一行,则生成动态的sql语句
{
if(MessageBox("你确定要删除此记录吗?","警告",MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2)==IDYES)
{
int nItem = m_Master.GetNextSelectedItem(pos);
_variant_t RecordsAffected;
strCode = m_Master.GetItemText(nItem,1);
m_Connection.BeginTrans();
strSQL.Format("DELETE FROM kv_user WHERE us_code ='%s'", strCode);
if(m_Connection.Execute((_bstr_t)strSQL,adCmdText)==FALSE) //执行此sql语句
{
m_Connection.RollbackTrans();
return ;
}
strSQL.Format("DELETE FROM kv_user_tx WHERE ut_code= '%s'", strCode);
if(m_Connection.Execute((_bstr_t)strSQL,adCmdText)==FALSE) //执行此sql语句
{
m_Connection.RollbackTrans();
return ;
}
m_Connection.CommitTrans();
LoadData(m_strSQL);
return ;
}
return ;
}
AfxMessageBox("请选择一行数据!");
}
void CViewUser::OnEdit()
{
// TODO: Add your control notification handler code here
CDlgUserTx dlg;
int nItem;
CString strUser;
POSITION pos = m_Master.GetFirstSelectedItemPosition();
if(pos) nItem = m_Master.GetNextSelectedItem(pos);
if (nItem < 0 ) return;
dlg.m_nItem = nItem;
dlg.m_pParent = this;
dlg.m_bAppend = FALSE;
dlg.DoModal();
// LoadData(m_strSQL);
}
void CViewUser::OnCancel()
{
// TODO: Add your control notification handler code here
GetParent()->PostMessage(WM_CLOSE);
}
void CViewUser::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
m_pApp = (CKvipApp *)AfxGetApp();
if(!m_Connection.ConnectDefault())
{
MessageBox("错误:MPW-000001连接数据库出错!", "系统提示", MB_OK|MB_ICONEXCLAMATION);
GetParent()->PostMessage(WM_CLOSE);
}
m_RecordSet.SetAdoConnection(&m_Connection); //连结数据库
m_Master.SetHeadings(_T("序号,1,60; 用户编号,1, 80; 用户名称,1, 90; 管理员, 1,60; 有效, 1, 60;备注,1, 300"));
m_Master.SetGridLines(TRUE);
// m_Master.SetCheckboxes(TRUE);
}
BOOL CViewUser::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
m_RecordSet.Close();
m_Connection.Close();
return CFormView::DestroyWindow();
}
BOOL CViewUser::LoadData(CString &strSQL)
{
BOOL bManager, bEnabled;
UINT nKey = 0;
CString sUser, sCode, sNote;
CString sManager, sEnabled, sKey;
m_Master.SetRedraw(FALSE);
m_Master.DeleteAllItems();
//检测用户
// m_RecordSet.SetCursorLocation(); //作者例程,不知有什么用
if (!m_RecordSet.Open(strSQL,adCmdText, //SQL为TXT
adOpenStatic, //静态游标
adLockReadOnly) || //只读
m_RecordSet.IsBOF() ||
m_RecordSet.IsEOF())
{//错误提示
MessageBox("注意:没有发现用户资料!", "系统提示", MB_OK|MB_ICONEXCLAMATION);
m_Master.SetRedraw(TRUE);
return FALSE;
}
while(m_RecordSet.IsEOF() == FALSE)
{
nKey++;
m_RecordSet.GetCollect("us_name", sUser);
m_RecordSet.GetCollect("us_code", sCode);
m_RecordSet.GetCollect("us_manager", bManager);
m_RecordSet.GetCollect("us_enabled", bEnabled);
m_RecordSet.GetCollect("us_note", sNote);
bManager ? sManager = _T("是") : sManager = _T("否");
bEnabled ? sEnabled = _T("是") : sEnabled = _T("否");
sKey.Format("%5d", nKey);
m_Master.InsertItemEx(0,sKey, sCode, sUser, sManager, sEnabled, sNote);
m_RecordSet.MoveNext();
}
m_Master.SetRedraw(TRUE);
m_RecordSet.Close();
return TRUE;
}
void CViewUser::OnDblclkMaster(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
OnEdit();
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -