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

📄 inhabitantsdoc.cpp

📁 这是一个关于数据库的例子
💻 CPP
字号:
// InhabitantsDoc.cpp : implementation of the CInhabitantsDoc class
//

#include "stdafx.h"
#include "Inhabitants.h"

#include "InhabitantsDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CInhabitantsDoc

IMPLEMENT_DYNCREATE(CInhabitantsDoc, CDocument)

BEGIN_MESSAGE_MAP(CInhabitantsDoc, CDocument)
	//{{AFX_MSG_MAP(CInhabitantsDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInhabitantsDoc construction/destruction

CInhabitantsDoc::CInhabitantsDoc()
{
	// TODO: add one-time construction code here

}

CInhabitantsDoc::~CInhabitantsDoc()
{

}

BOOL CInhabitantsDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	
	//打开数据库
	HRESULT result = m_dbHouse.Open();
	
	if(FAILED(result))
		AfxMessageBox("open database failed!");
	
	//设置程序标题
	SetTitle("小区居民管理系统");
	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CInhabitantsDoc serialization

void CInhabitantsDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CInhabitantsDoc diagnostics

#ifdef _DEBUG
void CInhabitantsDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CInhabitantsDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CInhabitantsDoc commands


int CInhabitantsDoc::AddUser(USER &user)
{	/*返回值:	0		已经存在
				-1		添加失败
				1		添加成功
	*/

	//判断是否存在该住户了
	CString strSql;
	strSql.Format("select * from house where sectionname = '%s' and buildingnum = %d and cellnum = %d and roomnum = %d",
		user.strSectionname,user.nBuildingnum,user.nCellnum,user.nRoomnum);
	CCommand<CAccessor<CHouseAccessor> > dbHouse;
	long* pCount = new long;
	if(dbHouse.Open(m_dbHouse.m_session,strSql,NULL,pCount) != S_OK)
	{
		AfxMessageBox("error");
		delete pCount;
		return -1;
	}
	if(dbHouse.MoveFirst() == S_OK)
	{
		delete pCount;
		dbHouse.Close();
		return 0;
	}
	delete pCount;
	dbHouse.Close();

	//增加住户到数据库中
	m_dbHouse.MoveLast();
	_tcscpy( m_dbHouse.m_sectionname,user.strSectionname );
	m_dbHouse.m_buildingnum = user.nBuildingnum;
	m_dbHouse.m_cellnum = user.nCellnum;
	m_dbHouse.m_roomnum = user.nRoomnum;
	_tcscpy(m_dbHouse.m_housemaster,user.strName);
	_tcscpy(m_dbHouse.m_beeppager,user.strBeeppager);
	_tcscpy(m_dbHouse.m_email,user.strEmail);
	_tcscpy(m_dbHouse.m_housetel,user.strHouseTel);
	_tcscpy(m_dbHouse.m_mobile,user.strMobile);
	_tcscpy(m_dbHouse.m_office,user.strOffice);
	_tcscpy(m_dbHouse.m_officetel,user.strOfficeTel);
	
	HRESULT hResult = m_dbHouse.Insert(); 
	if( FAILED( hResult ) )
	{
		AfxMessageBox( _T( "Error inserting the current record" ) );
		return -1;
	}
	return 1;
}

//删除符合条件的住户
BOOL CInhabitantsDoc::DeleteUser(CString strSql)
{
	CCommand<CAccessor<CHouseAccessor> > dbHouse;
	long* pCount = new long;
	if(dbHouse.Open(m_dbHouse.m_session,strSql,
		NULL,pCount,DBGUID_DEFAULT,FALSE) != S_OK)
	{
		AfxMessageBox("error");
		delete pCount;
		return FALSE;
	}
	delete pCount;
	dbHouse.Close();

	return TRUE;	
}

void CInhabitantsDoc::DeleteContents() 
{
	//关闭数据库
	m_dbHouse.Close();
	m_dbHouse.m_session.Close();

	CDocument::DeleteContents();
}

⌨️ 快捷键说明

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