📄 borrowerdlg.cpp
字号:
// BorrowerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "db.h"
#include "BorrowerDlg.h"
#include "AddBorrowerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define VIEW_COLCOUNT 2
static char s_view_colname[VIEW_COLCOUNT][10] = {"读者号", "读者姓名"};
static int s_view_colwidth[VIEW_COLCOUNT] = {120,120};
/////////////////////////////////////////////////////////////////////////////
// CBorrowerDlg dialog
CBorrowerDlg::CBorrowerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBorrowerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBorrowerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CBorrowerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBorrowerDlg)
DDX_Control(pDX, IDC_LIST, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBorrowerDlg, CDialog)
//{{AFX_MSG_MAP(CBorrowerDlg)
ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
ON_BN_CLICKED(IDC_MODIFY_BUTTON, OnModifyButton)
ON_BN_CLICKED(IDC_DEL_BUTTON, OnDelButton)
ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBorrowerDlg message handlers
void CBorrowerDlg::InsertData(CString strBorrowerID, CString strBorrowerName)
{
CString strSQL("");
try
{
strSQL.Format ("INSERT INTO BorrowerInfo(BorrowerID,BorrowerName)\
VALUES('%s','%s')", strBorrowerID,strBorrowerName);
_bstr_t vSQL =strSQL;
m_AdoConn.ExecuteSQL(vSQL);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("自定义错误信息3:%s",e.ErrorMessage());
AfxMessageBox(errormessage);//显示错误信息
}
}
void CBorrowerDlg::DeleteData(CString strBorrowerID)
{
CString strSQL("");
try
{
strSQL.Format("Delete from BorrowerInfo where BorrowerID = '%s'",strBorrowerID);
_bstr_t vSQL =strSQL;
m_AdoConn.ExecuteSQL(vSQL);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("自定义错误信息2:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
void CBorrowerDlg::ModifyData(CString strOldBorrowerID,CString strBorrowerID,CString strBorrowerName)
{
CString strSQL("");
try
{
strSQL.Format("update BorrowerInfo SET BorrowerID = '%s',BorrowerName= '%s' WHERE BorrowerID ='%s'",\
strBorrowerID,strBorrowerName,strOldBorrowerID);
_bstr_t vSQL =strSQL;
m_AdoConn.ExecuteSQL(vSQL);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("自定义错误信息3:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
BOOL CBorrowerDlg::JudgeBorrowerID(CString strBorrowerID)
{
if(strBorrowerID.GetLength()==0)
return false;
_variant_t var;
//设置SELECT语句
CString strSQL;
strSQL.Format( "SELECT * FROM BorrowerInfo WHERE BorrowerID = '%s'",strBorrowerID);
_bstr_t vSQL =strSQL;
_RecordsetPtr pRecordset;
pRecordset = m_AdoConn.GetRecordSet(vSQL);
BOOL bRet;
if (pRecordset->adoEOF != VARIANT_TRUE)
{
bRet = false;
}
else
{
bRet = true;
}
return bRet;
}
void CBorrowerDlg::OnAddButton()
{
// TODO: Add your control notification handler code here
CAddBorrowerDlg dlg;
dlg.SetTitle("添加读者");
if ( IDCANCEL == dlg.DoModal() ) return;
if(dlg.GetBorrowerID().GetLength()==0)
{
AfxMessageBox("读者号为空!");
return;
}
if(!JudgeBorrowerID(dlg.GetBorrowerID()))
{
AfxMessageBox("读者已经存在!");
return;
}
if(dlg.GetBorrowerName().GetLength() == 0)
{
AfxMessageBox("名称不能为空");
return;
}
InsertData(dlg.GetBorrowerID(),dlg.GetBorrowerName());
AddList(dlg.GetBorrowerID(),dlg.GetBorrowerName());
}
void CBorrowerDlg::OnModifyButton()
{
again:
CAddBorrowerDlg dlg;
dlg.SetTitle("更改读者信息");
int n= m_list.GetSelectionMark();
if(n == -1)
{
AfxMessageBox("请选择您要修改的信息!");
return ;
}
dlg.SetBorrowerID(m_list.GetItemText(n,0));
dlg.SetBorrowerName(m_list.GetItemText(n,1));
if (IDCANCEL == dlg.DoModal() ) return;
if(dlg.GetBorrowerID().GetLength() == 0)
{
AfxMessageBox("编号不能为空");
return;
}
if((dlg.GetBorrowerID() !=m_list.GetItemText(n,0)) &&!JudgeBorrowerID(dlg.GetBorrowerID()))
{
AfxMessageBox("读者号已经存在!");
//return;
goto again;
}
if(dlg.GetBorrowerName().GetLength() == 0)
{
AfxMessageBox("名称不能为空");
return;
}
ModifyData(m_list.GetItemText(n,0),dlg.GetBorrowerID(),dlg.GetBorrowerName());
ModifyList(dlg.GetBorrowerID(),dlg.GetBorrowerName());
}
void CBorrowerDlg::OnDelButton()
{
int n = m_list.GetSelectionMark();
if(n == -1)
{
MessageBox("请选择一条记录!");
return;
}
DeleteData(m_list.GetItemText(n,0));
m_list.DeleteItem(n);
}
void CBorrowerDlg::OnExitButton()
{
m_AdoConn.ExitConnect();
CDialog::OnCancel();
}
BOOL CBorrowerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
InitList();
m_AdoConn.OnInitADOConn(".");
ShowList();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/*******************************
* InitList
*
* Remark: 初始化List控件
******************************/
void CBorrowerDlg::InitList()
{
LV_COLUMN lvC;
int i;
//ListView_SetImageList(m_list.m_hWnd, ImageList_Create(1, 16, ILC_COLOR, 1, 1), LVSIL_SMALL);
ListView_SetExtendedListViewStyle(m_list.m_hWnd, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvC.fmt = LVCFMT_LEFT;
for (i = 0; i < VIEW_COLCOUNT; i++)
{
lvC.pszText = s_view_colname[i];
lvC.cx = s_view_colwidth[i];
lvC.iSubItem = i;
if (ListView_InsertColumn(m_list.m_hWnd, i, &lvC) == -1) return;
}
ListView_SetItemCount(m_list.m_hWnd, VIEW_COLCOUNT);
//m_list.SetSelectionMark(1);
}
/***********************************************
* 函数名: CPhoneDlg::ShowList
*
* 功 能: 显示
*
* 返回值: void
***********************************************/
void CBorrowerDlg::ShowList()
{
_variant_t var;
CString strCount("");
//ADOConn m_AdoConn;
//m_AdoConn.OnInitADOConn(m_strDataBase);
//设置SELECT语句
CString strSQL;
strSQL.Format("select * from BorrowerInfo ASEC");
_bstr_t vSQL =strSQL;
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
int n = 0;
m_list.DeleteAllItems();
m_list.Invalidate(TRUE);
while(!m_pRecordset->adoEOF)
{
m_list.InsertItem(n,"");
var = m_pRecordset->GetCollect("BorrowerID");
if(var.vt != VT_NULL)
m_list.SetItemText(n,0,(LPCSTR)_bstr_t(var));
var = m_pRecordset->GetCollect("BorrowerName");
if(var.vt != VT_NULL)
m_list.SetItemText(n,1,(LPCSTR)_bstr_t(var));
n++;
m_pRecordset->MoveNext();
}
//m_AdoConn.ExitConnect();
}
void CBorrowerDlg::AddList(CString strBorrowerID,CString strBorrowerName)
{
int Count = m_list.GetItemCount();
m_list.InsertItem(Count,"");
m_list.SetItemText(Count,0,strBorrowerID);
m_list.SetItemText(Count,1,strBorrowerName);
}
void CBorrowerDlg::ModifyList(CString strBorrowerID,CString strBorrowerName)
{
int n = m_list.GetSelectionMark();
m_list.SetItemText(n,0,strBorrowerID);
m_list.SetItemText(n,1,strBorrowerName);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -