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

📄 searchdlg.cpp

📁 图书馆管理系统是典型的信息管理系统(MIS),其开发主要包括后台数据库的建立和维护以及前端应用程序的开发两个方面。对于前者要求建立起数据一致性和完整性强.数据安全性好的库。而对于后者则要求应用程序功能
💻 CPP
字号:
// SearchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "librarym.h"
#include "SearchDlg.h"
#include "LibraryMDlg.h"


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

/////////////////////////////////////////////////////////////////////////////
// CSearchDlg dialog


CSearchDlg::CSearchDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSearchDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSearchDlg)
	m_Author = _T("");
	m_BookName = _T("");
	m_DateFrom = _T("");
	m_DateTo = _T("");
	m_Press = _T("");
	m_readerid = _T("");
	m_borrowdate = _T("");
	m_borrowno = _T("");
	m_returndate = _T("");
	//}}AFX_DATA_INIT
}


void CSearchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSearchDlg)
	DDX_Control(pDX, IDC_BOOKINFO_LIST, m_booksearchlistresult);
	DDX_Text(pDX, IDC_EDIT_AUTHOR, m_Author);
	DDX_Text(pDX, IDC_EDIT_BOOKNAME, m_BookName);
	DDX_Text(pDX, IDC_EDIT_DATEFROM, m_DateFrom);
	DDX_Text(pDX, IDC_EDIT_DATETO, m_DateTo);
	DDX_Text(pDX, IDC_EDIT_PRESS, m_Press);
	DDX_Text(pDX, IDC_EDIT_READERID, m_readerid);
	DDX_Text(pDX, IDC_EDIT_BORROWDATE, m_borrowdate);
	DDX_Text(pDX, IDC_EDIT_BORROWNO, m_borrowno);
	DDX_Text(pDX, IDC_EDIT_RETURNDATE, m_returndate);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSearchDlg, CDialog)
	//{{AFX_MSG_MAP(CSearchDlg)
	ON_BN_CLICKED(IDC_BTN_CANCEL, OnBtnCancel)
	ON_BN_CLICKED(IDC_BUTTON_SEARCH, OnButtonSearch)
	ON_BN_CLICKED(IDC_BUTTON_BORROW, OnButtonBorrow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSearchDlg message handlers


BOOL CSearchDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	//初始化数据库连接
	HRESULT result;
	try
	{
		//实例化连接对象
		result=m_pConnection.CreateInstance(_uuidof(Connection));
		if(SUCCEEDED(result))
		{
			//设置连接属性为UDL文件
			m_pConnection->ConnectionString="File Name=LIBRARYM.udl";
			//设置等待连接打开的时间为20s
			m_pConnection->ConnectionTimeout=20;
			result=m_pConnection->Open("","","",adConnectUnspecified);

			if(FAILED(result))
			{
				AfxMessageBox("open failed");
				return TRUE;
			}
		}
		else
		{
			AfxMessageBox("createinstance of connection failed!");
			return TRUE;
		}
	}
	catch(_com_error e)
	{
		//输出异常信息
		_bstr_t bstrSource(e.Source());
		_bstr_t bstrDescription(e.Description());
		AfxMessageBox(bstrSource+bstrDescription);
		return TRUE;
	}
	CDialog::OnInitDialog();
	//初始化藏书的信息列表
	m_booksearchlistresult.InsertColumn(0,"ID",LVCFMT_LEFT,100);
	m_booksearchlistresult.InsertColumn(1,"BookName",LVCFMT_CENTER,100);
	m_booksearchlistresult.InsertColumn(2,"TypeID",LVCFMT_CENTER,100);
	m_booksearchlistresult.InsertColumn(3,"Author",LVCFMT_CENTER,100);
	m_booksearchlistresult.InsertColumn(4,"Press",LVCFMT_CENTER,100);
	m_booksearchlistresult.InsertColumn(5,"PrintDate",LVCFMT_CENTER,100);
	m_booksearchlistresult.InsertColumn(6,"Price",LVCFMT_CENTER,70);
	m_booksearchlistresult.InsertColumn(7,"Page",LVCFMT_CENTER,70);
	m_booksearchlistresult.InsertColumn(8,"KeyWord",LVCFMT_CENTER,100);
	m_booksearchlistresult.InsertColumn(9,"Available",LVCFMT_CENTER,80);
	m_booksearchlistresult.InsertColumn(10,"PS",LVCFMT_CENTER,100);
	//选中列表的一行
	m_booksearchlistresult.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);

	return TRUE;
}

void CSearchDlg::OnBtnCancel() 
{
	CDialog::OnCancel();	
}

void CSearchDlg::OnButtonSearch() 
{
	UpdateData(TRUE);

	CString temp;
	bool bset=false;
	CString strSql="select * from BookInfo where ";
	if(!m_Author.IsEmpty())
	{
		temp.Format("Author='%s'",m_Author);
		bset=true;
	}
	if(!temp.IsEmpty())
		strSql+=temp;
	temp="";
	if(!m_BookName.IsEmpty() )
	{
		if(bset)
		{	
			temp.Format("and BookName='%s'",m_BookName);
		}
		else
		{
			temp.Format("BookName='%s'",m_BookName);
			bset=true;
		}
	}
	if(!temp.IsEmpty())
		strSql+=temp;
	temp="";
	if(!m_Press.IsEmpty())
	{
		if(bset)
		{
			temp.Format("and Press='%s'",m_Press);
		}
		else
		{
			temp.Format("Press='%s'",m_Press);
			bset=true;
		}
	}
	if(!temp.IsEmpty())
		strSql+=temp;
	temp="";
	if(!m_DateFrom.IsEmpty() || !m_DateTo.IsEmpty())
	{
		if(bset)
		{
			temp.Format("and PrintDate>'%s' and PrintDate<'%s'",m_DateFrom,m_DateTo);
		}
		else
		{
			temp.Format("PrintDate>'%s' and PrintDate<'%s'",m_DateFrom,m_DateTo);
			bset=true;
		}
	}
	if(!temp.IsEmpty())
		strSql+=temp;

	if(!bset)
	{
		AfxMessageBox("empty input!");
		return;
	}
	_RecordsetPtr pset;
	pset.CreateInstance(_uuidof(Recordset));
	try
	{
		HRESULT hr;
		//利用open函数执行SQL命令,获得查询结果记录集
		hr=pset->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),
					adOpenDynamic,adLockOptimistic,adCmdText);
		if(FAILED(hr))
		{
			AfxMessageBox("Here Failed !");
			return ;
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		return;
	}

	int curItem=0;
	_variant_t getvar;
	CString strValue;

	//判断有否记录
		if(!(pset->BOF) || !(pset->adoEOF))
		{
			while(!pset->adoEOF)
			{
				//获得记录集中当前的第一字段"BookID"的值
				getvar=pset->GetCollect("BookID");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else 
				{
					strValue=" ";
				}
				m_booksearchlistresult.InsertItem(curItem,strValue);
				
				//获得记录集中BookName的值
				getvar=pset->GetCollect("BookName");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,1,strValue);

				//获得记录集中BookTypeID的值
				getvar=pset->GetCollect("BookTypeID");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,2,strValue);

				//获得记录集中Author的值
				getvar=pset->GetCollect("Author");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,3,strValue);

				//获得记录集中Press的值
				getvar=pset->GetCollect("Press");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,4,strValue);

				//获得记录集中PrintDate的值
				getvar=pset->GetCollect("PrintDate");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,5,strValue);

				//获得记录集中Price的值
				getvar=pset->GetCollect("Price");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,6,strValue);

				//获得记录集中Page的值
				getvar=pset->GetCollect("Page");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,7,strValue);

				//获得记录集中Keyword的值
				getvar=pset->GetCollect("Keyword");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,8,strValue);

				//获得记录集中Available的值
				getvar=pset->GetCollect("Available");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,9,strValue);

				//获得记录集中PS的值
				getvar=pset->GetCollect("PS");
				if(getvar.vt!=VT_NULL)
				{
					strValue=(LPCSTR)_bstr_t(getvar);
				}
				else
				{
					strValue=" ";
				}
				m_booksearchlistresult.SetItemText(curItem,10,strValue);
				
				//移动当前记录到下一条记录
				pset->MoveNext();
				curItem++;
			}
		}
		else
		{
			AfxMessageBox("open recordset failed!");
		}
		pset->Close();
		pset=NULL;
	}

void CSearchDlg::OnButtonBorrow() 
{
	UpdateData(TRUE);

	CString booksql="select * from BookInfo where ";   //搜索书的信息SQL语句
	CString temp;
	CString bid;
	if(m_readerid.IsEmpty()|| m_borrowno.IsEmpty() ||m_returndate.IsEmpty()||
		m_borrowdate.IsEmpty())
	{
		AfxMessageBox("at least one form is empty!",MB_OK);
		return;
	}
	
	_RecordsetPtr pset,pset1;
	pset.CreateInstance(_uuidof(Recordset));
	
	try
	{
		int sel=m_booksearchlistresult.GetSelectionMark();  
		if(sel>=0)
		{
			if(AfxMessageBox("are you sure? ",MB_YESNO)==IDYES)
			{
				bid=m_booksearchlistresult.GetItemText(sel,0);  //得到书的ID
				temp.Format("BookID='%s'",bid);
				booksql+=temp;
				pset->Open(_variant_t(booksql),m_pConnection.GetInterfacePtr(),
				adOpenDynamic,adLockOptimistic,adCmdText);
				//得到书库存量
				_variant_t val=pset->GetCollect("Available");
				//转化成整型数后减一
				temp=(LPCSTR)_bstr_t(val);
				int minus=atoi(temp);
				if(minus<=0)
				{
					AfxMessageBox("the book is now empty stored! can't be borrowed",MB_OK);
					return;
				}
				else
					minus--;
				temp.Format("%d",minus);
				
				pset->PutCollect("Available",_variant_t(temp));
				pset->Update();
			}
			else return;
		}
		else if(sel<0)
		{
			AfxMessageBox("no record is selected!");
			return;
		}
		pset->Close();
		pset=NULL;

		CString borrowsql="select * from BorrowRelation "; //插入借书记录SQL语句


		pset1.CreateInstance(_uuidof(Recordset));
		pset1->Open(_variant_t(borrowsql),m_pConnection.GetInterfacePtr(),
		adOpenDynamic,adLockOptimistic,adCmdText);

		pset1->AddNew();
		pset1->PutCollect("BorrowNo",_variant_t(m_borrowno));
		pset1->PutCollect("BookID",_variant_t(bid));
		pset1->PutCollect("ReaderID",_variant_t(m_readerid));
		pset1->PutCollect("BorrowDate",_variant_t(m_borrowdate));
		pset1->PutCollect("ReturnDate",_variant_t(m_returndate));
		pset1->Update();
		
		AfxMessageBox("successfully borrowed the book!");
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	pset1->Close();
	pset1=NULL;
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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