📄 userindlg.cpp
字号:
// UserInDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UserIn.h"
#include "UserInDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUserInDlg dialog
CUserInDlg::CUserInDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUserInDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUserInDlg)
m_ID = 0;
m_name = _T("");
m_password = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
str="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=user.mdb;Persist Security Info=False";
curItem=0;//List控件中记录的序号
strValue="";//strValue变量存储从_variant_t类型中获得的字符串
SQL="";
}
void CUserInDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUserInDlg)
DDX_Control(pDX, IDC_LIST, m_List);
DDX_Text(pDX, IDC_ID, m_ID);
DDX_Text(pDX, IDC_NAME, m_name);
DDX_Text(pDX, IDC_PASSWORD, m_password);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUserInDlg, CDialog)
//{{AFX_MSG_MAP(CUserInDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SHOW, OnShow)
ON_BN_CLICKED(IDC_CHECK, OnCheck)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUserInDlg message handlers
BOOL CUserInDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_List.InsertColumn (0,"ID",LVCFMT_LEFT,100,-1);
m_List.InsertColumn (1,"用户名",LVCFMT_LEFT,100,-1);
m_List.InsertColumn (2,"密码",LVCFMT_LEFT,100,-1);
m_List.SetExtendedStyle (LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
connect.Open (str);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CUserInDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUserInDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CUserInDlg::OnShow()
{
// record.re ->MoveFirst ();
m_List.DeleteAllItems ();
SQL="SELECT * FROM UserInfo";
// connect.Open (str);
record.Open (SQL,&connect);
while(!record.re ->adoEOF )
{
InsertInfo(record.re );
record.re->MoveNext ();
curItem++;
}
curItem=0;
strValue="";
SQL="";
// connect.Close ();
record.Close ();
}
void CUserInDlg::OnCheck()
{
m_List.DeleteAllItems ();
UpdateData(true);
SQL.Format ("select * from UserInfo where UserName='%s'",(LPCTSTR)m_name);
record.re =connect.con ->Execute ((_bstr_t)SQL,&RecordsetAffected,adCmdText);
if(!record.re->BOF )
{
while(!record.re->adoEOF )
{
InsertInfo(record.re );
record.re->MoveNext ();
curItem++;
}
curItem=0;
strValue="";
SQL="";
}
else
{
MessageBox("没有该用户!");
}
record.Close ();
}
void CUserInDlg::OnAdd()
{
m_List.DeleteAllItems ();
UpdateData(true);
CString strID,strPass;
strID.Format ("%d",m_ID);
strPass.Format ("%d",m_password);
long lid,lpass;
lid=(long)m_ID;
lpass=(long)m_password;
SQL="SELECT * FROM UserInfo";
AfxMessageBox("open");
record.Open (SQL,&connect);
if(!record.re ->BOF )
{
AfxMessageBox("AddNew");
record.re->AddNew ();
record.re->Fields ->Item ["ID"]->Value=(_variant_t)lid;
record.re->Fields ->Item ["UserName"]->Value=(_variant_t)m_name;
record.re->Fields ->Item ["PassWord"]->Value=(_variant_t)lpass;
AfxMessageBox("updateDate");
record.re->Update ();
InsertInfo(record.re );
curItem=0;
strValue="";
SQL="";
}
else
{
AfxMessageBox("没有插入记录!");
}
//record.Close ();
}
void CUserInDlg::OnDelete()
{
int sel=m_List.GetSelectionMark ();
if(sel>=0)
{
int tempid=atoi(m_List.GetItemText (sel,0));
SQL.Format ("DELETE from UserInfo where ID=%d",tempid);
record.Open (SQL,&connect);
OnShow();
}
else
{
AfxMessageBox("没有用户记录");
}
}
void CUserInDlg::InsertInfo (_RecordsetPtr re)
{
var=record.re->GetCollect ("ID");
if(var.vt !=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_List.InsertItem (curItem,strValue);
var=record.re->GetCollect ("UserName");
if(var.vt !=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText (curItem,1,strValue);
var=record.re->GetCollect ("PassWord");
if(var.vt !=VT_NULL)
strValue=(LPCSTR)_bstr_t(var);
m_List.SetItemText (curItem,2,strValue);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -