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

📄 assetenrolview.cpp

📁 这是本人前不久为公司做的一个财产登记软件,实现中用到的是ADO Access数据库.登陆的初始密码为:111111,当然你可以进行更改.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AssetEnrolView.cpp : implementation of the CAssetEnrolView class
//

#include "stdafx.h"
#include "AssetEnrol.h"

#include "MainFrm.h"
#include "AssetEnrolDoc.h"
#include "AssetEnrolView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAssetEnrolView

IMPLEMENT_DYNCREATE(CAssetEnrolView, CFormView)

BEGIN_MESSAGE_MAP(CAssetEnrolView, CFormView)
	//{{AFX_MSG_MAP(CAssetEnrolView)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_COMMAND(ID_READ, OnRead)
	ON_COMMAND(ID_ADD, OnAdd)
	ON_COMMAND(ID_MODIFY, OnModify)
	ON_COMMAND(ID_DELDTE, OnDeldte)
	ON_COMMAND(ID_QUERY, OnQuery)
	ON_COMMAND(ID_NEXT, OnNext)
	ON_COMMAND(ID_CLEARITEM, OnClearitem)
	ON_COMMAND(ID_CLEARTABLE, OnCleartable)
	ON_WM_CLOSE()
	ON_COMMAND(IDD_ASSET, OnAsset)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAssetEnrolView construction/destruction

CAssetEnrolView::CAssetEnrolView()
	: CFormView(CAssetEnrolView::IDD)
{
	//{{AFX_DATA_INIT(CAssetEnrolView)
	m_Name = _T("");
	m_Day = _T("");
	m_Month = _T("");
	m_Year = _T("");
	m_Unit = _T("");
	m_Value = _T("");
	m_Custodian = _T("");
	m_Property = _T("");
	m_Number = _T("");
	m_SerialNo = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

	IsQuery=FALSE;
	IsOne=TRUE;

	}

CAssetEnrolView::~CAssetEnrolView()
{
}

void CAssetEnrolView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAssetEnrolView)
	DDX_Control(pDX, IDC_COMBO1, m_Combox);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT5, m_Name);
	DDX_Text(pDX, IDC_EDIT3, m_Day);
	DDX_Text(pDX, IDC_EDIT2, m_Month);
	DDX_Text(pDX, IDC_EDIT1, m_Year);
	DDX_Text(pDX, IDC_EDIT6, m_Unit);
	DDX_Text(pDX, IDC_EDIT7, m_Value);
	DDX_Text(pDX, IDC_EDIT9, m_Custodian);
	DDX_Text(pDX, IDC_EDIT10, m_Property);
	DDX_Text(pDX, IDC_EDIT8, m_Number);
	DDX_Text(pDX, IDC_EDIT4, m_SerialNo);
	//}}AFX_DATA_MAP
}

BOOL CAssetEnrolView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	
	return CFormView::PreCreateWindow(cs);
}

void CAssetEnrolView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
}

/////////////////////////////////////////////////////////////////////////////
// CAssetEnrolView printing

BOOL CAssetEnrolView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAssetEnrolView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing

}

void CAssetEnrolView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CAssetEnrolView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CAssetEnrolView diagnostics

#ifdef _DEBUG
void CAssetEnrolView::AssertValid() const
{
	CFormView::AssertValid();

}

void CAssetEnrolView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CAssetEnrolDoc* CAssetEnrolView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAssetEnrolDoc)));

	return (CAssetEnrolDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAssetEnrolView message handlers


//每次选择新的记录时,移动指针到新的记录位置并将其显示出来
void CAssetEnrolView::OnSelchangeList1() 
{
	// add...
    extern CAssetEnrolApp theApp;
	extern BOOL asset;
	_variant_t var;

	int nCurSel=m_list.GetCurSel();

	if(nCurSel<0)
		return;

	//如果是查询固定资产
	if(asset==TRUE)
	{
		m_SerialNo=m_Name
		   =m_Unit=m_Number=m_Custodian=m_Value=m_Property="";

		UpdateData(FALSE);

		return;

	}
	//创建一个数据库连接
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	try
	{
		//打开AssetEnrol表(所有字段)
		m_pRecordset->Open("SELECT * FROM AssetEnrol",theApp.m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic,adCmdText); 
		m_pRecordset->MoveFirst();

		m_pRecordset->Move(long(nCurSel));

		var=m_pRecordset->GetCollect("Year");
		if(var.vt!=VT_NULL)
				m_Year=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Month");
		if(var.vt!=VT_NULL)
				m_Month=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Day");
		if(var.vt!=VT_NULL)
				m_Day=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("SerialNo");
		if(var.vt!=VT_NULL)
				m_SerialNo=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Name");
		if(var.vt!=VT_NULL)
				m_Name=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Unit");
		if(var.vt!=VT_NULL)
				m_Unit=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Number");
		if(var.vt!=VT_NULL)
				m_Number=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Value");
		if(var.vt!=VT_NULL)
				m_Value=(LPCSTR)_bstr_t(var);
		else
			    m_Value="";
		var=m_pRecordset->GetCollect("Custodian");
		if(var.vt!=VT_NULL)
				m_Custodian=(LPCSTR)_bstr_t(var);
		var=m_pRecordset->GetCollect("Property");
		if(var.vt!=VT_NULL)
				m_Property=(LPCSTR)_bstr_t(var);

		UpdateData(FALSE);
	}

	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
}


void CAssetEnrolView::OnRead() 
{
	//add...
	extern CAssetEnrolApp theApp;
	extern BOOL asset;
	_variant_t var;
	CString strYear;
	CString strMonth;
	CString strDay;
	CString strSerialNo;
	CString strName;
	CString strNumber;
	CString strUnit;
	CString strValue;
	CString strCustodian;
	CString strProperty;

	asset=FALSE;

	UpdateData(TRUE);

	//清空列表
	m_list.ResetContent();

	//创建一个数据库连接
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	try 
	{ 
		// 第一个参数:查询student表中所有字段 
		// 第二个参数:获取库接库的IDispatch指针 
        m_pRecordset->Open("SELECT * FROM AssetEnrol",theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic, adLockOptimistic,adCmdText); 
	} 
    catch(_com_error *e) 
	{ 
        AfxMessageBox(e->ErrorMessage()); 
	}   


	//从数据库中读取数据并显示在链表中
	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			AfxMessageBox("表内数据为空!");
		    return;
		}

	    while(!m_pRecordset->adoEOF)
		{
			var=m_pRecordset->GetCollect("Year");
			if(var.vt!=VT_NULL)
				m_Year=strYear=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Month");
			if(var.vt!=VT_NULL)
				m_Month=strMonth=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Day");
			if(var.vt!=VT_NULL)
				m_Day=strDay=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("SerialNo");
			if(var.vt!=VT_NULL)
				strSerialNo=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Name");
			if(var.vt!=VT_NULL)
				m_Name=strName=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Unit");
			if(var.vt!=VT_NULL)
				m_Unit=strUnit=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Number");
			if(var.vt!=VT_NULL)
				m_Number=strNumber=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Value");
			if(var.vt!=VT_NULL)
				m_Value=strValue=(LPCSTR)_bstr_t(var);
			else
				m_Value="";
			var=m_pRecordset->GetCollect("Custodian");
			if(var.vt!=VT_NULL)
				m_Custodian=strCustodian=(LPCSTR)_bstr_t(var);
			var=m_pRecordset->GetCollect("Property");
			if(var.vt!=VT_NULL)
				m_Property=strProperty=(LPCSTR)_bstr_t(var);


			//固定长度
			//少则用空白补
			int yearLength=strYear.GetLength();
			int monthLength=strMonth.GetLength();
			int dayLength=strDay.GetLength();
			int serialNoLength=strSerialNo.GetLength();
			int nameLength=strName.GetLength();
			int unitLength=strUnit.GetLength();
			int numberLength=strNumber.GetLength();
			int valueLength=strValue.GetLength();
			int custodianLength=strCustodian.GetLength();
			int propertyLength=strProperty.GetLength();

			if(yearLength<=5)
			{
				for(int i=0;i<=(5-yearLength);i++)
				{
					strYear+=" ";
				}
			}
			
			if(monthLength<=2)
			{
				for(int i=0;i<=(2-monthLength);i++)
				{
					strMonth+=" ";
				}
			}

			if(dayLength<=5)
			{
				for(int i=0;i<=(5-dayLength);i++)
				{
					strDay+=" ";
				}
			}

			if(serialNoLength<=4)
			{
				for(int i=0;i<=(4-serialNoLength);i++)
				{
					strSerialNo+=" ";
				}
			}

			if(nameLength<=41)
			{
				for(int i=0;i<=(41-nameLength);i++)
				{
					strName+=" ";
				}
			}

			
			//名称太长,截取其中的一部分
			else if(nameLength>=41)
				strName=strName.Left(38)+"    ";

			if(unitLength<=6)
			{
				for(int i=0;i<=(6-unitLength);i++)
				{
					strUnit+=" ";
				}
			}

			if(numberLength<=6)
			{
				for(int i=0;i<=(6-numberLength);i++)
				{
					strNumber+=" ";
				}
			}

			if(valueLength<=8)
			{
				for(int i=0;i<=(8-valueLength);i++)
				{
					strValue+=" ";
				}
			}

			if(custodianLength<=7)
			{
				for(int i=0;i<=(7-custodianLength);i++)
				{
					strCustodian+=" ";
				}
			}

			if(propertyLength<=12)
			{
				for(int i=0;i<=(12-propertyLength);i++)
				{
					strProperty+=" ";
				}
			}
			m_list.AddString(" "+strYear+strMonth+strDay+strSerialNo
				+strName+strUnit+strNumber+strValue+strCustodian+strProperty);

			m_pRecordset->MoveNext();
		}
		
		//定位到第一行
		m_list.SetCurSel(0);
		
	    OnSelchangeList1();   

		UpdateData(FALSE);
	}

	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

}




void CAssetEnrolView::OnAdd() 
{
	// add...
	extern CAssetEnrolApp theApp;
	_variant_t var;
	CString IsFixed="";
	UpdateData();

	UpdateData(TRUE);

	if(m_Year=="" || m_Month==""||m_Day==""||
		m_SerialNo==""||m_Name==""||m_Unit==""||m_Number==""||
		   m_Custodian==""||m_Property=="")
	{
		AfxMessageBox("信息不完整!   ");
		return;
	}

	//创建一个数据库连接
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	try
	{
		//打开AssetEnrol表(所有字段)
		m_pRecordset->Open("SELECT * FROM AssetEnrol",theApp.m_pConnection.GetInterfacePtr(), 
			adOpenDynamic, adLockOptimistic,adCmdText); 

		while(!m_pRecordset->adoEOF)
		{
			var=m_pRecordset->GetCollect("SerialNo");
			if(var.vt!=VT_NULL)
			{
				CString serialNo;
				serialNo=(LPCSTR)_bstr_t(var);

				if(m_SerialNo==serialNo)
				{
					MessageBox("编号重复,请检查 ","提醒!");
					return;
				}
			}
			
			m_pRecordset->MoveNext();
		}


		if(m_Combox.GetCurSel()==0)
		{
			IsFixed="否";
		}
		else
			IsFixed="是";

		//写入各字段值
		m_pRecordset->AddNew();
		m_pRecordset->PutCollect("Year",_variant_t(m_Year));
		m_pRecordset->PutCollect("Month",_variant_t(m_Month));
		m_pRecordset->PutCollect("Day",_variant_t(m_Day));
		m_pRecordset->PutCollect("SerialNo",_variant_t(m_SerialNo));
		m_pRecordset->PutCollect("Name",_variant_t(m_Name));
		m_pRecordset->PutCollect("Unit",_variant_t(m_Unit));
		m_pRecordset->PutCollect("Number",_variant_t(m_Number));
		m_pRecordset->PutCollect("Value",_variant_t(m_Value));
		m_pRecordset->PutCollect("Custodian",_variant_t(m_Custodian));
		m_pRecordset->PutCollect("Property",_variant_t(m_Property));
		m_pRecordset->PutCollect("IsFixed",_variant_t(IsFixed));
		m_pRecordset->Update();

		AfxMessageBox("插入成功!");


		//下面更新数据库内容

		int nCurSel=m_list.GetCurSel();

		//从新读数据库
		OnRead();
		m_list.SetCurSel(nCurSel);
        OnSelchangeList1();

		m_SerialNo=m_Name
			=m_Unit=m_Number=m_Custodian=m_Value="";

		UpdateData(FALSE);
	}

	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
}




void CAssetEnrolView::OnModify() 
{
	// add...
	//全局变量
	extern CAssetEnrolApp theApp;
	CString IsFixed;
	//更新数据库
	UpdateData();

	UpdateData(TRUE);

	//创建一个数据库连接
	m_pRecordset.CreateInstance(__uuidof(Recordset));

	if(m_list.GetCount()==0 || m_Year=="" || m_Month=="" ||
		m_Day==""||m_SerialNo==""||m_Name==""||m_Unit==""||m_Number==""||
		  m_Custodian==""||m_Property=="")
	{
		AfxMessageBox("记录为空或信息不完整!\n\n请先读取数据库!");
		return;
	}

	int nID=MessageBox("确实要修改吗?","提示~",IDOK);
	if(nID==IDCANCEL)
		return;

	if(m_list.GetCurSel()<0 || m_list.GetCurSel() > m_list.GetCount())
	{
		m_list.SetCurSel(0);
	}

	if(m_Combox.GetCurSel()==0)
	{
		IsFixed="否";
	}
	else
		IsFixed="是";
	
	try
	{
		//打开student表(所有字段)
		m_pRecordset->Open("SELECT * FROM AssetEnrol",theApp.m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic,adCmdText); 

		m_pRecordset->MoveFirst(); 
        m_pRecordset->Move(m_list.GetCurSel());    // 从0开始 

        m_pRecordset->PutCollect("Year",_variant_t(m_Year));
		m_pRecordset->PutCollect("Month",_variant_t(m_Month));
		m_pRecordset->PutCollect("Day",_variant_t(m_Day));
		m_pRecordset->PutCollect("SerialNo",_variant_t(m_SerialNo));
		m_pRecordset->PutCollect("Name",_variant_t(m_Name));
		m_pRecordset->PutCollect("Unit",_variant_t(m_Unit));
		m_pRecordset->PutCollect("Number",_variant_t(m_Number));
		m_pRecordset->PutCollect("Value",_variant_t(m_Value));
		m_pRecordset->PutCollect("Custodian",_variant_t(m_Custodian));
		m_pRecordset->PutCollect("Property",_variant_t(m_Property));
		m_pRecordset->PutCollect("IsFixed",_variant_t(IsFixed));
		m_pRecordset->Update();

		int nCurSel=m_list.GetCurSel();
		OnRead();
		m_list.SetCurSel(nCurSel);
        OnSelchangeList1();

		UpdateData(FALSE);
	}

	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

}




void CAssetEnrolView::OnDeldte() 
{
	// add...
	//如果没有记录
	if(m_list.GetCount()==0)
	{
		AfxMessageBox("没有选定项!\n\n请先读取数据库!");
		return;

⌨️ 快捷键说明

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