⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookgldlg.cpp

📁 VC+Access图书管理系统
💻 CPP
字号:
// BookglDlg.cpp : implementation file
//

#include "stdafx.h"
#include "libmis.h"
#include "BookglDlg.h"

#include "AddbookDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBookglDlg dialog


CBookglDlg::CBookglDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBookglDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBookglDlg)
	m_strVal = _T("");
	//}}AFX_DATA_INIT
}


void CBookglDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBookglDlg)
	DDX_Control(pDX, IDC_LIST1, m_cList);
	DDX_Control(pDX, IDC_COMBO1, m_cCombo);
	DDX_Text(pDX, IDC_EDIT1, m_strVal);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBookglDlg, CDialog)
	//{{AFX_MSG_MAP(CBookglDlg)
	ON_BN_CLICKED(IDC_BUTSEARCH, OnButsearch)
	ON_BN_CLICKED(ID_ADD, OnAdd)
	ON_BN_CLICKED(ID_DEL, OnDel)
	ON_BN_CLICKED(ID_UPDATE, OnUpdate)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBookglDlg message handlers
BOOL CBookglDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString strSql;
	m_cList.SetExtendedStyle (LVS_EX_TRACKSELECT|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_cList.InsertColumn (0,"书ID",LVCFMT_CENTER,70);
	m_cList.InsertColumn (1,"书名",LVCFMT_CENTER,170);
	m_cList.InsertColumn (2,"类别",LVCFMT_CENTER,60);
	m_cList.InsertColumn (3,"数量",LVCFMT_CENTER,60);
	m_cList.InsertColumn (4,"出版社",LVCFMT_CENTER,260);
	
	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 book";
		m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
			adOpenStatic,adLockOptimistic,adCmdText);
		_variant_t vName,vPsw;
		int nIndex=0;
		while (!m_pRecordset->adoEOF)
		{
			m_cList.InsertItem (nIndex,(_bstr_t)(m_pRecordset->GetCollect ("ID")));
			m_cList.SetItemText (nIndex,1,(_bstr_t)(m_pRecordset->GetCollect ("name")));
			m_cList.SetItemText (nIndex,2,(_bstr_t)(m_pRecordset->GetCollect ("type")));
			m_cList.SetItemText (nIndex,3,(_bstr_t)(m_pRecordset->GetCollect ("num")));
			m_cList.SetItemText (nIndex,4,(_bstr_t)(m_pRecordset->GetCollect ("pub")));
			nIndex++;
			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 CBookglDlg::OnButsearch() 
{
	// TODO: Add your control notification handler code here
	UpdateData ();
	m_strVal.TrimLeft ();
	m_strVal.TrimRight ();
	int nIndex=m_cCombo.GetCurSel ();
	CString strSql="SELECT * FROM book WHERE ";
	switch (nIndex)
	{
	case 0:	strSql+="ID LIKE '%"+m_strVal+="%'";break;
	case 1: strSql+="name LIKE '%"+m_strVal+="%'";break;
	case 2: strSql+="type="+m_strVal+="";break;
	case 3: strSql+="pub LIKE '%"+m_strVal+="%'";break;
	default: return;
	}
	m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
		adOpenStatic,adLockOptimistic,adCmdText);
	m_cList.DeleteAllItems ();
	nIndex=0;
	while (!m_pRecordset->adoEOF)
	{
		m_cList.InsertItem (nIndex,(_bstr_t)(m_pRecordset->GetCollect ("ID")));
		m_cList.SetItemText (nIndex,1,(_bstr_t)(m_pRecordset->GetCollect ("name")));
		m_cList.SetItemText (nIndex,2,(_bstr_t)(m_pRecordset->GetCollect ("type")));
		m_cList.SetItemText (nIndex,3,(_bstr_t)(m_pRecordset->GetCollect ("num")));
		m_cList.SetItemText (nIndex,4,(_bstr_t)(m_pRecordset->GetCollect ("pub")));
		nIndex++;
		m_pRecordset->MoveNext ();
	}
	m_pRecordset->Close ();

}

BEGIN_EVENTSINK_MAP(CBookglDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CBookglDlg)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()


void CBookglDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	CAddbookDlg dlg;
	if (IDOK==dlg.DoModal ())
	{
		try
		{
			m_pRecordset->Open ("SELECT * FROM book",m_pConnection.GetInterfacePtr (),
				adOpenStatic,adLockOptimistic,adCmdText);
			m_pRecordset->AddNew ();
			m_pRecordset->PutCollect ("ID",(_variant_t)dlg.m_strId);
			m_pRecordset->PutCollect ("name",(_variant_t)dlg.m_strName);
			m_pRecordset->PutCollect ("type",(_variant_t)dlg.m_strType);
			m_pRecordset->PutCollect ("num",(_variant_t)dlg.m_strNum);
			m_pRecordset->PutCollect ("pub",(_variant_t)dlg.m_strPub);
			m_pRecordset->Update();
			m_pRecordset->Close ();
		}
		catch (_com_error e)
		{
			if (3105==e.WCode ())
				MessageBox ("此书已入库!");
			return;
		}
		AfxMessageBox ("添加成功!");
	}
}

void CBookglDlg::OnDel() 
{
	// TODO: Add your control notification handler code here
	int nIndex;
	CString strSql;
	try
	{
		if (!m_cList.GetFirstSelectedItemPosition ())
			return;
		if (2==MessageBox ("确认要删除吗?","删除",MB_OKCANCEL|MB_ICONWARNING))
			return;
		for (nIndex=m_cList.GetItemCount ()-1;nIndex>=0;nIndex--)
		{
			if (m_cList.GetItemState (nIndex,LVIS_SELECTED))	
			{
				strSql="SELECT * FROM book WHERE id='"+m_cList.GetItemText (nIndex,0)+"'";
				m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
					adOpenStatic,adLockOptimistic,adCmdText);
				m_pRecordset->Delete (adAffectCurrent);
				m_pRecordset->Update();
				m_pRecordset->Close ();
				m_cList.DeleteItem (nIndex);
			}
		}
	}
	catch (_com_error e)
	{
		CString errormessage;
		errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
		AfxMessageBox (errormessage);
		return;
	}
}

void CBookglDlg::OnUpdate() 
{
	// TODO: Add your control notification handler code here
	CAddbookDlg dlg;
	int nIndex;
	POSITION pos=m_cList.GetFirstSelectedItemPosition ();
	CString strSql;
	try
	{
		nIndex=m_cList.GetNextSelectedItem (pos);
		dlg.m_nType=1;
		dlg.m_strId=m_cList.GetItemText (nIndex,0);
		dlg.m_strName=m_cList.GetItemText (nIndex,1);
		dlg.m_strType=m_cList.GetItemText (nIndex,2);
		dlg.m_strNum=m_cList.GetItemText (nIndex,3);
		dlg.m_strPub=m_cList.GetItemText (nIndex,4);
		if (IDOK==dlg.DoModal ())
		{
			strSql="SELECT * FROM book WHERE id='"+m_cList.GetItemText (nIndex,0)+"'";			
			m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
				adOpenStatic,adLockOptimistic,adCmdText);
			m_pRecordset->PutCollect ("ID",(_variant_t)dlg.m_strId);
			m_pRecordset->PutCollect ("name",(_variant_t)dlg.m_strName);
			m_pRecordset->PutCollect ("type",(_variant_t)dlg.m_strType);
			m_pRecordset->PutCollect ("num",(_variant_t)dlg.m_strNum);
			m_pRecordset->PutCollect ("pub",(_variant_t)dlg.m_strPub);
			m_pRecordset->Update();  
			m_pRecordset->Close ();
			
		}
		m_cList.SetItemText (nIndex,0,dlg.m_strId);
		m_cList.SetItemText (nIndex,1,dlg.m_strName);
		m_cList.SetItemText (nIndex,2,dlg.m_strType);
		m_cList.SetItemText (nIndex,3,dlg.m_strNum);
		m_cList.SetItemText (nIndex,4,dlg.m_strPub);		
	}
	catch (_com_error e)
	{
		CString errormessage;
		errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
		AfxMessageBox (errormessage);
		return;
	}
}

void CBookglDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	m_pConnection->Close ();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -