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

📄 studedit.cpp

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

#include "stdafx.h"
#include "图书馆系统.h"
#include "STUDEDIT.h"

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

#define ChunkSize 1024
/////////////////////////////////////////////////////////////////////////////
// STUDEDIT dialog


STUDEDIT::STUDEDIT(CWnd* pParent /*=NULL*/)
	: CDialog(STUDEDIT::IDD, pParent)
{
	//{{AFX_DATA_INIT(STUDEDIT)
	m_bopt = _T("");
	m_boid = _T("");
	m_usptno = _T("");
	m_usadr = _T("");
	m_usname = _T("");
	m_usphone = _T("");
	m_uspt = _T("");
	m_uswps = _T("");
	//}}AFX_DATA_INIT
}


void STUDEDIT::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(STUDEDIT)
	DDX_Text(pDX, IDC_BORROW_PT, m_bopt);
	DDX_Text(pDX, IDC_BORROWID, m_boid);
	DDX_Text(pDX, IDC_PT_NO, m_usptno);
	DDX_Text(pDX, IDC_USER_ADR, m_usadr);
	DDX_Text(pDX, IDC_USER_NAME, m_usname);
	DDX_Text(pDX, IDC_USER_PHONE, m_usphone);
	DDX_Text(pDX, IDC_USER_PT, m_uspt);
	DDX_Text(pDX, IDC_USER_WPS, m_uswps);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(STUDEDIT, CDialog)
	//{{AFX_MSG_MAP(STUDEDIT)
	ON_BN_CLICKED(IDC_PHOTO, OnPhoto)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// STUDEDIT message handlers
void STUDEDIT::ResetCtrlValueStud()
{
	//初始化各控件值
	m_bopt = _T("");
	m_boid = _T("");
	m_usptno = _T("");
	m_usadr = _T("");
	m_usname = _T("");
	m_usphone = _T("");
	m_uspt = _T("");
	m_uswps = _T("");
	UpdateData(FALSE);
}
BOOL STUDEDIT::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;

}
void STUDEDIT::SetFileToDb(_RecordsetPtr &recPtr, CString strPath)
{
	VARIANT varChunk;
	SAFEARRAY *psa;
	SAFEARRAYBOUND rgsabound[1]; 	
	//VT_ARRAY | VT_UI1	
	CFile f(strPath.operator LPCTSTR(),CFile::modeRead);
	BYTE  bVal[ChunkSize+1];
	long uIsRead=0;
	//Create a safe array to store the array of BYTES  
	while(1)
	{
		//读取照片
		uIsRead=f.Read(bVal,ChunkSize);
		if(uIsRead==0)break;
		rgsabound[0].cElements =uIsRead;
		rgsabound[0].lLbound = 0;		
		///创建SAFEARRAY对象 
		psa = SafeArrayCreate(VT_UI1,1,rgsabound);
		
		for(long index=0;index<uIsRead;index++)          
		{
			if(FAILED(SafeArrayPutElement(psa,&index,&bVal[index])))
				::MessageBox(NULL,"错误。","提示",MB_OK | MB_ICONWARNING);
		}		
		//为varChunk变量赋值
		varChunk.vt = VT_ARRAY|VT_UI1;
		varChunk.parray = psa;		
		//加入BLOB类型的数据
		try{
			recPtr->Fields->GetItem("照片")->AppendChunk(varChunk);
		}
		catch (_com_error &e)
		{
			CString str=(char*)e.Description();
			::MessageBox(NULL,str+"\n错误。","提示",MB_OK | MB_ICONWARNING);
		}
		::VariantClear(&varChunk);
		::SafeArrayDestroyData( psa);
		if(uIsRead<ChunkSize)break;
	}
	f.Close();
}

BOOL STUDEDIT::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
    CString strTitle="添加用户信息";
	SetWindowText(strTitle);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void STUDEDIT::OnOK() 
{
	// TODO: Add extra validation here
	CString strSQL;
	{
		//更新控件变量的值
		UpdateData(TRUE);
		//打开记录集 选择表名
		strSQL="select * from 借阅人员表";
		if(!OpenRecordSet(m_pRecordset,strSQL))
		{
			AfxMessageBox("没有成功打开数据表");
			return;
		}
		m_boid.Remove(' ');
		m_usname.Remove(' ');
		m_bopt.Remove(' ');
		m_uspt.Remove(' ');
		m_usptno.Remove(' ');
		if(m_boid.IsEmpty()||m_usname.IsEmpty()||m_bopt.IsEmpty()||m_uspt.IsEmpty()||m_usptno.IsEmpty())
		{
			AfxMessageBox("用户姓名、借阅类型、借阅ID、证件类型、证件号等不能为空");
			return;
		}
	
		try
		{
			//添加数据,姓名只允许添加不允许更改
			m_pRecordset->AddNew();
			m_pRecordset->PutCollect("借阅ID",_variant_t(m_boid));
			m_pRecordset->PutCollect("借阅类型",_variant_t(m_bopt));
			m_pRecordset->PutCollect("用户姓名",_variant_t(m_usname));
			m_pRecordset->PutCollect("证件类型",_variant_t(m_uspt));
			m_pRecordset->PutCollect("证件号码",_variant_t(m_usptno));
			m_pRecordset->PutCollect("工作单位",_variant_t(m_uswps));
			m_pRecordset->PutCollect("联系地址",_variant_t(m_usadr));
			m_pRecordset->PutCollect("联系电话",_variant_t(m_usphone));
			//照片
			if(!m_strPhotoPath.IsEmpty())
			{
				SetFileToDb(m_pRecordset,m_strPhotoPath);
			}
			//更新数据库
			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;
		}
	    AfxMessageBox("插入成功!");
	}
	//将各控件值置空
	ResetCtrlValueStud();
	CDialog::OnOK();
}
void STUDEDIT::OnPhoto() 
{
	// TODO: Add your control notification handler code here
	CString strFilter="BMP Files(*.bmp)|*.bmp|All Files (*.*)|*.*||";
	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,strFilter);
	if (dlg.DoModal()!=IDOK)
	{
		return;
	}
	//获取照片路径名
	m_strPhotoPath=dlg.GetPathName();
}
void STUDEDIT::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}



⌨️ 快捷键说明

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