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

📄 operate.cpp

📁 用MFC和Access开发的数据库系统
💻 CPP
字号:
// OPERATE.cpp : implementation file
//

#include "stdafx.h"
#include "图书馆系统.h"
#include "OPERATE.h"
#include "BORROWLIST.h"

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

/////////////////////////////////////////////////////////////////////////////
// OPERATE dialog


OPERATE::OPERATE(CWnd* pParent /*=NULL*/)
	: CDialog(OPERATE::IDD, pParent)
{
	//{{AFX_DATA_INIT(OPERATE)
	m_optbid = _T("");
	m_optuid = _T("");
	//}}AFX_DATA_INIT
}


void OPERATE::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(OPERATE)
	DDX_Text(pDX, IDC_OPT_BOOKID, m_optbid);
	DDX_Text(pDX, IDC_OPT_USERID, m_optuid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(OPERATE, CDialog)
	//{{AFX_MSG_MAP(OPERATE)
	ON_BN_CLICKED(IDC_OPT, OnOpt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// OPERATE message handlers
BOOL OPERATE::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if(m_option==2)
	{
		CEdit* pEdit=(CEdit*)GetDlgItem(IDC_OPT_BOOKID);
		//当前对话框是否用来编辑,图书ID不允许编辑
	    pEdit->EnableWindow(FALSE);
	}
	if(m_option==3)
	{
		CEdit* pEdit=(CEdit*)GetDlgItem(IDC_OPT_USERID);
		//当前对话框是否用来编辑,图书ID不允许编辑
	    pEdit->EnableWindow(FALSE);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void OPERATE::OnOpt() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CString strSQL,strql;
    mbookname="";
    if(m_option==1)
    {
		strSQL.Format("select * from 图书信息情况 where 图书ID='%s' ",m_optbid);
		if(!OpenRecordSet(m_pRecordset,strSQL))
		{
			 AfxMessageBox("没有成功打开数据表");
		     return ;
		}	
		if(!m_pRecordset->adoEOF)
		{
			try
			{
				mbookname=pLeftView->VariantToCString(m_pRecordset->GetCollect("图书名称"));
			}
		    catch(_com_error e)
			{
				CString strError;
				strError.Format("警告:插入信息时发生异常。错误信息: %s",\
				e.ErrorMessage());
				AfxMessageBox(strError);
			}
		}
		if(mbookname=="")
		{ 
            AfxMessageBox("找不到该图书数据");
			return;
		}
	   	strSQL.Format("select * from 借阅人员表 where 借阅ID='%s' ",m_optuid);
        //打开记录集 选择表名
		if(!OpenRecordSet(m_pRecordset,strSQL))
		{
			AfxMessageBox("没有成功打开数据表");
		    return;
		}
		if(!m_pRecordset->adoEOF)
		{ 
			
			strSQL="select * from 借阅信息表";
		    if(!OpenRecordSet(m_pRecordset,strSQL))
			{
				AfxMessageBox("没有成功打开数据表");
				return;
			}
			try
			{
				//添加数据,姓名只允许添加不允许更改
				strSQL="1";
				m_pRecordset->AddNew();
				m_pRecordset->PutCollect("图书ID",_variant_t(m_optbid));
				m_pRecordset->PutCollect("图书名称",_variant_t(mbookname));
				m_pRecordset->PutCollect("借阅ID",_variant_t(m_optuid));
				m_pRecordset->PutCollect("借阅次数",_variant_t(strSQL));
				COleDateTime oleTime;
		        CDateTimeCtrl* pDtCtrl=(CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER);
		        pDtCtrl->GetTime(oleTime);
		        m_pRecordset->PutCollect("借阅日期",_variant_t(oleTime));
				//更新数据库
				m_pRecordset->Update();	
				m_pRecordset->MoveLast();
				//m_pRecordset->Close();
				//m_pRecordset=NULL;
			}
			catch(_com_error e)
			{
				CString strError;
				strError.Format("警告:该图书可能已经被借阅!  错误信息: %s",\
				e.ErrorMessage());
				AfxMessageBox(strError);
				return;
			}
			
			m_pRecordset->Close();	
			m_pRecordset=NULL;	
			AfxMessageBox("借阅成功!");	
			CDialog::OnCancel();
		}
		else
		{
		    AfxMessageBox("没有该用户");
			return;
		}
	}
	else if(m_option==2)
    {
	     BORROWLIST dlg;
         dlg.m_getblid=m_optuid;
	     dlg.DoModal();
	}
    else if(m_option==3)
    {
	    strSQL.Format("delete * from 借阅信息表 where 图书ID='%s' ",m_optbid);
		if (MessageBox("是否归还该图书?","确认",MB_YESNO|MB_ICONQUESTION)==IDNO)
		{
	     	return;	
		}
		if(!OpenRecordSet(m_pRecordset,strSQL))
		{
			 AfxMessageBox("没有成功打开数据表");
		     return ;
		}	
        AfxMessageBox("归还操作成功!该图书可能没有借阅记录!");
	}
	else
	    AfxMessageBox("Error!");
}

BOOL OPERATE::OpenRecordSet(_RecordsetPtr &recPtr, CString &strSQL)
{
	CMyApp* pApp=(CMyApp*)AfxGetApp();
	//创建记录集对象
	m_pRecordset.CreateInstance(__uuidof(Recordset));
	//在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
    //因为它有时会经常出现一些想不到的错误
	try
	{
		//从数据库中打开表
		recPtr->Open(strSQL.AllocSysString(), 
			pApp->m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch (_com_error e)
	{
		CString strError;
		strError.Format("警告:打开数据表时发生异常。 错误信息: %s",\
			e.ErrorMessage());
		AfxMessageBox(strError);
		return FALSE;
	}
	return TRUE;

}


⌨️ 快捷键说明

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