insertreader.cpp

来自「图书馆管理系统是典型的信息管理系统(MIS),其开发主要包括后台数据库的建立和维」· C++ 代码 · 共 157 行

CPP
157
字号
// InsertReader.cpp : implementation file
//

#include "stdafx.h"
#include "librarym.h"
#include "InsertReader.h"
#include "ReaderList.h"

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

/////////////////////////////////////////////////////////////////////////////
// CInsertReader dialog


CInsertReader::CInsertReader(CWnd* pParent /*=NULL*/)
	: CDialog(CInsertReader::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInsertReader)
	m_address = _T("");
	m_email = _T("");
	m_gender = _T("");
	m_phone = _T("");
	m_ps = _T("");
	m_readerid = _T("");
	m_readername = _T("");
	m_readertypeid = _T("");
	m_workunit = _T("");
	//}}AFX_DATA_INIT
}


void CInsertReader::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInsertReader)
	DDX_Text(pDX, IDC_EDIT_ADDRESS, m_address);
	DDX_Text(pDX, IDC_EDIT_EMAIL, m_email);
	DDX_Text(pDX, IDC_EDIT_GENDER, m_gender);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_phone);
	DDX_Text(pDX, IDC_EDIT_PS, m_ps);
	DDX_Text(pDX, IDC_EDIT_READERID, m_readerid);
	DDX_Text(pDX, IDC_EDIT_READERNAME, m_readername);
	DDX_Text(pDX, IDC_EDIT_READERTYPEID, m_readertypeid);
	DDX_Text(pDX, IDC_EDIT_WORKUNIT, m_workunit);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CInsertReader, CDialog)
	//{{AFX_MSG_MAP(CInsertReader)
	ON_BN_CLICKED(IDC_BUTTON_INSERTREADERRECORDCANCEL, OnButtonInsertreaderrecordcancel)
	ON_BN_CLICKED(IDC_BUTTON_INSERTREADERRECORD, OnButtonInsertreaderrecord)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInsertReader message handlers


BOOL CInsertReader::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;
	}
	return TRUE;
}

void CInsertReader::OnButtonInsertreaderrecordcancel() 
{
	CDialog::OnCancel();		
}

void CInsertReader::OnButtonInsertreaderrecord() 
{
	UpdateData(TRUE); //得到输入
	//判断是否输入了要插入的数据项
	if(m_address.IsEmpty() || m_phone.IsEmpty() ||m_gender.IsEmpty()
		|| m_readerid.IsEmpty() || m_readername.IsEmpty() ||m_readertypeid.IsEmpty()
		)
	{
		AfxMessageBox("empty input!");
		return;
	}
	_RecordsetPtr pset;
	pset.CreateInstance(_uuidof(Recordset));
	HRESULT hr;
	try
	{
		hr=pset->Open("select * from ReaderInfo",m_pConnection.GetInterfacePtr(),
			adOpenDynamic,adLockOptimistic,adCmdText);
		//添加记录
		if(SUCCEEDED(hr))
		{
			pset->AddNew();
			//写入每个字段值
			pset->PutCollect("ReaderID",_variant_t(m_readerid));
			pset->PutCollect("ReaderName",_variant_t(m_readername));
			pset->PutCollect("ReaderTypeID",_variant_t(m_readertypeid));
			pset->PutCollect("Gender",_variant_t(m_gender));
			pset->PutCollect("WorkUnit",_variant_t(m_workunit));
			pset->PutCollect("Address",_variant_t(m_address));
			pset->PutCollect("Phone",_variant_t(m_phone));
			pset->PutCollect("Email",_variant_t(m_email));
			pset->PutCollect("PS",_variant_t(m_ps));
			pset->Update();
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
		return;
	}
	pset->Close();
	pset=NULL;
	AfxMessageBox("successful inserted!");
	CDialog::OnOK();
}


⌨️ 快捷键说明

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