首先参照另一篇http://blog.csdn.net/etwdone/article/details/8484206建立MFC与数据库的连接
接着创见如下界面
接着直接将头文件和CPP放上
ClerkMDlg.h
#pragma once
#include "stdafx.h"
#include "Library.h"
#include "afxdialogex.h"
#include "MainFrm.h"
// CClerkMDlg 对话框
class CClerkMDlg : public CDialog
{
DECLARE_DYNAMIC(CClerkMDlg)
public:
CClerkMDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~CClerkMDlg();
_RecordsetPtr pRst,p,pfirst,plast,pp;
//CoInitialize(NULL);
//_RecordsetPtr pRst(Recordset);
// 对话框数据
enum { IDD = IDD_DIALOG1 };
CString m_strUserNameQ;
CString m_strUserNameQQ;
CString m_strUserIDQ;
CString m_strUserID;
CString m_strUserName;
CString m_strPassWord;
CString m_strIDCard;
bool sts;
void DisplayRecord(_RecordsetPtr pRst );
BOOL OnInitDialog();
void SetTextSate();
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedFirst();
afx_msg void OnBnClickedLast();
afx_msg void OnBnClickedPrior();
afx_msg void OnBnClickedNest();
afx_msg void OnBnClickedNew();
afx_msg void OnBnClickedEdit();
afx_msg void OnBnClickedEnquery();
afx_msg void OnBnClickedDelete();
afx_msg void OnBnClickedSave();
afx_msg void OnBnClickedCancelRec();
afx_msg void OnBnClickedExit();
};
ClerkMDlg.cpp
// ClerkMDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Library.h"
#include "ClerkMDlg.h"
#include "afxdialogex.h"
#include "MainFrm.h"
// CClerkMDlg 对话框
IMPLEMENT_DYNAMIC(CClerkMDlg, CDialog)
CClerkMDlg::CClerkMDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClerkMDlg::IDD, pParent)
{
m_strUserNameQ = _T("");
m_strUserName = _T("");
m_strUserIDQ = _T("");
m_strUserID = _T("");
m_strPassWord = _T("");
m_strIDCard = _T("");
sts=true;
p=NULL;
pfirst=NULL;
plast=NULL;
pp=NULL;
//_RecordsetPtr pRst(__uuidof(Recordset));
//UpdateData(TRUE);
}
BOOL CClerkMDlg::OnInitDialog()
{
CWnd *pWnd;
pWnd = GetDlgItem(IDC_POSITION);
// ((CComboBox *)pWnd)->AddString("主席");
// ((CComboBox *)pWnd)->AddString("经理");
// ((CComboBox *)pWnd)->AddString("主任");
// ((CComboBox *)pWnd)->AddString("员工");
((CComboBox *)pWnd)->InsertString(0,"员工");
((CComboBox *)pWnd)->InsertString(1,"经理");
((CComboBox *)pWnd)->InsertString(2,"主任");
((CComboBox *)pWnd)->InsertString(3,"主席");
/*CoInitialize(NULL);
_RecordsetPtr pRst(__uuidof(Recordset));
pRst = ((CMainFrame*)(AfxGetMainWnd()))->pConn->Execute("select 'm_strUserNameQ' from CLERK ",NULL,adCmdText);
if(pRst->rsEOF)
{ MessageBox(_T("用户名不存在,请重新输入")); }
pRst->Close();
pRst.Release();
CoUninitialize();*/
return TRUE;
}
CClerkMDlg::~CClerkMDlg()
{
if ( pRst != NULL )
{
pRst ->Close( );
pRst.Release();
}
p=NULL;
}
void CClerkMDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX,IDC_USERID_Q,m_strUserIDQ);
DDV_MaxChars(pDX,m_strUserIDQ,8);
DDX_Text(pDX,IDC_USERID,m_strUserID);
DDX_Text(pDX,IDC_USERNAME,m_strUserName);
DDX_Text(pDX,IDC_USERNAME_Q,m_strUserNameQ);
DDX_Text(pDX,IDC_PASSWORD,m_strPassWord);
DDX_Text(pDX,IDC_CARDID,m_strIDCard);
}
BEGIN_MESSAGE_MAP(CClerkMDlg, CDialog)
ON_BN_CLICKED(IDC_FIRST, &CClerkMDlg::OnBnClickedFirst)
ON_BN_CLICKED(IDC_LAST, &CClerkMDlg::OnBnClickedLast)
ON_BN_CLICKED(IDC_PRIOR, &CClerkMDlg::OnBnClickedPrior)
ON_BN_CLICKED(IDC_NEST, &CClerkMDlg::OnBnClickedNest)
ON_BN_CLICKED(IDC_NEW, &CClerkMDlg::OnBnClickedNew)
ON_BN_CLICKED(IDC_EDIT, &CClerkMDlg::OnBnClickedEdit)
ON_BN_CLICKED(IDC_ENQUERY, &CClerkMDlg::OnBnClickedEnquery)
ON_BN_CLICKED(IDC_DELETE, &CClerkMDlg::OnBnClickedDelete)
ON_BN_CLICKED(IDC_SAVE, &CClerkMDlg::OnBnClickedSave)
ON_BN_CLICKED(IDC_CANCEL_REC, &CClerkMDlg::OnBnClickedCancelRec)
ON_BN_CLICKED(IDC_EXIT, &CClerkMDlg::OnBnClickedExit)
END_MESSAGE_MAP()
// CClerkMDlg 消息处理程序
void CClerkMDlg::OnBnClickedFirst()
{
// TODO: 在此添加控件通知处理程序代码
SetTextSate();
_RecordsetPtr pRst(__uuidof(Recordset));
pRst->Open("SELECT * FROM CLERK",((CMainFrame*)(AfxGetMainWnd()))->pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRst->MoveFirst();
p=pRst;
DisplayRecord(pRst );
}
void CClerkMDlg::OnBnClickedLast()
{
// TODO: 在此添加控件通知处理程序代码
SetTextSate();
_RecordsetPtr pRst(__uuidof(Recordset));
pRst->Open("SELECT * FROM CLERK",((CMainFrame*)(AfxGetMainWnd()))->pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRst->MoveLast();
p=pRst;
DisplayRecord(pRst );
}
void CClerkMDlg::OnBnClickedPrior()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
SetTextSate();
_RecordsetPtr pRst(__uuidof(Recordset));
pRst->Open("SELECT * FROM CLERK",((CMainFrame*)(AfxGetMainWnd()))->pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRst->MoveLast();
if(p==NULL) p=pRst;
p->MovePrevious();
if((p->BOF)==FALSE) DisplayRecord(p);
else MessageBox("已经是第一条了!");
UpdateData(FALSE);
}
void CClerkMDlg::OnBnClickedNest()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
SetTextSate();
_RecordsetPtr pRst(__uuidof(Recordset));
pRst->Open("SELECT * FROM CLERK",((CMainFrame*)(AfxGetMainWnd()))->pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRst->MoveFirst();
if(p==NULL) p=pRst;
else p->MoveNext();
if((p->rsEOF)==FALSE) DisplayRecord(p );
else MessageBox("已经是最后一条了!");
}
void CClerkMDlg::OnBnClickedNew()
{
// TODO: 在此添加控件通知处理程序代码
sts=false;
SetTextSate();
pRst.CreateInstance(__uuidof( Recordset ));
//创建实例
pRst->Open("SELECT * FROM CLERK",((CMainFrame*)(AfxGetMainWnd()))->pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRst->MoveLast();
p=pRst;
m_strUserNameQ = _T("");
m_strUserName = _T("");
m_strUserIDQ = _T("");
m_strUserID = _T("");
m_strPassWord = _T("");
m_strIDCard = _T("");
sts=true;
UpdateData(FALSE);
}
void CClerkMDlg::OnBnClickedDelete()
{
// TODO: 在此添加控件通知处理程序代码
SetTextSate();
if((p->rsEOF)==FALSE)
{ pp=p;
pp->Delete(adAffectCurrent);
p->Update();
p->MoveNext();
if((p->rsEOF)==FALSE) DisplayRecord(p );
else {MessageBox("删除的是最后一条了!");
p->MovePrevious();
DisplayRecord(p);
}}
else MessageBox("已经是最后一条了");
UpdateData(FALSE);
}
void CClerkMDlg::OnBnClickedEdit()
{
// TODO: 在此添加控件通知处理程序代码
sts=false;
SetTextSate();
sts=true;
}
void CClerkMDlg::OnBnClickedEnquery()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
SetTextSate();
//CoInitialize(NULL);
//_RecordsetPtr pRst(__uuidof(Recordset));
CString mSqlStr;
mSqlStr="select * from CLERK where NAME='";
mSqlStr+=m_strUserNameQ;
mSqlStr+="'";
pRst = ((CMainFrame*)(AfxGetMainWnd()))->pConn->Execute((_bstr_t)mSqlStr,NULL,adCmdText);
p=pRst;
if(pRst->rsEOF)
{
m_strUserName = _T("");
m_strUserIDQ = _T("");
m_strUserID = _T("");
m_strPassWord = _T("");
m_strIDCard = _T("");
UpdateData(FALSE);
MessageBox(_T("用户名不存在,请重新输入"));
}
else DisplayRecord(pRst );
//pRst->Close();
//pRst.Release();
CoUninitialize();
UpdateData(FALSE);
}
void CClerkMDlg::OnBnClickedSave()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
SetTextSate();
CoInitialize(NULL);
//_RecordsetPtr pRst(__uuidof(Recordset));
// CString mSqlStr;
// mSqlStr="insert into CLERK values( '";
// mSqlStr+=m_strUserID;
// mSqlStr+="','";
// mSqlStr+=m_strUserName;
// mSqlStr+="','";
// mSqlStr+=m_strPassWord;
// mSqlStr+="','";
// mSqlStr+=m_strIDCard;
// mSqlStr+="')";
// pRst=((CMainFrame*)(AfxGetMainWnd()))->pConn->Execute((_bstr_t)mSqlStr,NULL,adCmdText);
//记录集方法二
//_RecordsetPtr m_pRecordset;
pRst.CreateInstance(__uuidof( Recordset ));
//创建实例
pRst->Open("SELECT * FROM CLERK",((CMainFrame*)(AfxGetMainWnd()))->pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
pRst->MoveFirst();
pfirst=pRst;
m_strUserNameQQ="";
BOOL sp=TRUE;
while((pfirst->rsEOF)==FALSE)
{
m_strUserNameQQ=pfirst->GetCollect("NAME");
if(m_strUserNameQQ==m_strUserName)
sp=FALSE;
pfirst->MoveNext();
}
if(sp)
{
//p->AddNew(); 本句是在表的末尾增加新纪录
//pRst->AddNew("NAME",_variant_t(m_strUserName));
p->PutCollect("CLERK_ID",_variant_t(m_strUserID));
p->PutCollect("NAME",_variant_t(m_strUserName));
p->PutCollect("PASSWORD",_variant_t(m_strPassWord));
p->PutCollect("IDCARD",_variant_t(m_strIDCard));
//更新数据库-将新纪录存入数据库
p->Update();
}
else MessageBox("已存在此用户!");
//pRst->Close();
//pRst.Release();
UpdateData(FALSE);
CoUninitialize();
}
void CClerkMDlg::OnBnClickedCancelRec()
{
// TODO: 在此添加控件通知处理程序代码
m_strUserNameQ = _T("");
m_strUserName = _T("");
m_strUserIDQ = _T("");
m_strUserID = _T("");
m_strPassWord = _T("");
m_strIDCard = _T("");
UpdateData(FALSE);
}
void CClerkMDlg::OnBnClickedExit()
{
// TODO: 在此添加控件通知处理程序代码
CDialog::OnCancel();
}
void CClerkMDlg::DisplayRecord(_RecordsetPtr pRst )
{
m_strUserName=pRst->GetCollect("NAME");
m_strUserID=pRst->GetCollect("IDCARD");
m_strPassWord=pRst->GetCollect("PASSWORD");
m_strIDCard=pRst->GetCollect("CLERK_ID");
UpdateData(FALSE);
}
void CClerkMDlg::SetTextSate()
{
CWnd *pWnd;
if (sts)
{
pWnd=GetDlgItem(IDC_USERNAME);
pWnd->EnableWindow(FALSE);
pWnd=GetDlgItem(IDC_USERID);
pWnd->EnableWindow(FALSE);
pWnd=GetDlgItem(IDC_PASSWORD);
pWnd->EnableWindow(FALSE);
pWnd=GetDlgItem(IDC_CARDID);
pWnd->EnableWindow(FALSE);
pWnd=GetDlgItem(IDC_USERNAME_Q);
pWnd->EnableWindow(TRUE);
pWnd=GetDlgItem(IDC_USERID_Q);
pWnd->EnableWindow(TRUE);
}
else
{
pWnd=GetDlgItem(IDC_USERNAME);
pWnd->EnableWindow(TRUE);
pWnd=GetDlgItem(IDC_USERID);
pWnd->EnableWindow(TRUE);
pWnd=GetDlgItem(IDC_PASSWORD);
pWnd->EnableWindow(TRUE);
pWnd=GetDlgItem(IDC_CARDID);
pWnd->EnableWindow(TRUE);
pWnd=GetDlgItem(IDC_USERNAME_Q);
pWnd->EnableWindow(FALSE);
pWnd=GetDlgItem(IDC_USERID_Q);
pWnd->EnableWindow(FALSE);
}
}