📄 admingldlg.cpp
字号:
// AdminglDlg.cpp : implementation file
//
#include "stdafx.h"
#include "libmis.h"
#include "AdminglDlg.h"
#include "AdminnewDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAdminglDlg dialog
CAdminglDlg::CAdminglDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdminglDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdminglDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CAdminglDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdminglDlg)
DDX_Control(pDX, IDC_LIST2, m_cList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAdminglDlg, CDialog)
//{{AFX_MSG_MAP(CAdminglDlg)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTADD, OnButadd)
ON_BN_CLICKED(IDC_BUTDEL, OnButdel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdminglDlg message handlers
BOOL CAdminglDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CString strSql;
try
{
if (!m_pConnection.CreateInstance(__uuidof(Connection)))
m_pConnection->Open("Provider=Microsoft.JET.OLEDB.4.0;Data Source=lib.mdb","","",adModeUnknown);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return FALSE;
}
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
strSql="SELECT * FROM admin";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
while (!m_pRecordset->adoEOF)
{
m_cList.AddString ((_bstr_t)m_pRecordset->GetCollect ("name"));
m_pRecordset->MoveNext ();
}
m_pRecordset->Close ();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAdminglDlg::OnDestroy()
{
// TODO: Add your message handler code here
m_pConnection->Close();
CDialog::OnDestroy();
}
void CAdminglDlg::OnButadd()
{
// TODO: Add your control notification handler code here
CAdminnewDlg dlg;
if (IDOK==dlg.DoModal ())
{
try
{
CString strSql="SELECT * FROM admin";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->AddNew ();
m_pRecordset->PutCollect ("name",(_variant_t)dlg.m_strName);
m_pRecordset->PutCollect ("password",(_variant_t)dlg.m_strPsw);
m_pRecordset->Update();
m_cList.AddString (dlg.m_strName);
}
catch (_com_error e)
{
if (3105==e.WCode ())
MessageBox ("用户名重名!");
return;
}
m_pRecordset->Close ();
}
}
void CAdminglDlg::OnButdel()
{
// TODO: Add your control notification handler code here
CString strName;
int nIndex=m_cList.GetCurSel ();
m_cList.GetText (nIndex,strName);
MessageBox (strName);
try
{
if (m_cList.GetCurSel ()<0)
return;
if (2==MessageBox ("确认要删除吗?","删除",MB_OKCANCEL|MB_ICONWARNING))
return;
CString strSql="SELECT * FROM admin WHERE name='"+strName+"'";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->Delete (adAffectCurrent);
m_pRecordset->Update();
m_cList.DeleteString (nIndex);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
m_pRecordset->Close ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -