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

📄 searchtansaction.cpp

📁 企业办公管理系统
💻 CPP
字号:
// SearchTansaction.cpp : implementation file
//

#include "stdafx.h"
#include "office.h"
#include "SearchTansaction.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSearchTansaction dialog


CSearchTansaction::CSearchTansaction(CWnd* pParent /*=NULL*/)
	: CDialog(CSearchTansaction::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSearchTansaction)
	m_beginTime = COleDateTime::GetCurrentTime();
	m_endTime = COleDateTime::GetCurrentTime();
	m_content = _T("");
	m_subject = _T("");
	m_type = _T("");
	//}}AFX_DATA_INIT
}


void CSearchTansaction::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSearchTansaction)
	DDX_Control(pDX, IDC_SEARCH, m_search);
	DDX_Control(pDX, IDC_LIST, m_list);
	DDX_DateTimeCtrl(pDX, IDC_BEGINDATE, m_beginTime);
	DDX_DateTimeCtrl(pDX, IDC_ENDDATE, m_endTime);
	DDX_CBString(pDX, IDC_CONTENT, m_content);
	DDX_CBString(pDX, IDC_SUBJECT, m_subject);
	DDX_CBString(pDX, IDC_TYPE, m_type);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSearchTansaction, CDialog)
	//{{AFX_MSG_MAP(CSearchTansaction)
	ON_BN_CLICKED(IDC_SEARCH, OnSearch)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSearchTansaction message handlers

BOOL CSearchTansaction::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	DWORD  style;
	style=m_list.GetExStyle();//读取样式;
	style=(style|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT)&(~LVS_EX_CHECKBOXES);//设置样式;
	m_list.SetExtendedStyle(style);
	m_list.InsertColumn(0,"分类",LVCFMT_LEFT,60);
	m_list.InsertColumn(1,"日期",LVCFMT_LEFT,70);
	m_list.InsertColumn(2,"主题",LVCFMT_LEFT,150);
	m_list.InsertColumn(3,"内容",LVCFMT_LEFT,350);
    ::CoInitialize(NULL);
	try{
	   _ConnectionPtr m_pConn("ADODB.Connection");
	   m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\DataSource\\office1.mdb;Persist Security Info=False","","",adConnectUnspecified);
	   CString strSQL;
	   strSQL="select distinct 事务类型 from 事务表";
	   _RecordsetPtr m_set("ADODB.Recordset");
	   //添加事务类型
	   m_set->Open(	(LPTSTR)strSQL.GetBuffer(130),_variant_t(m_pConn,true),adOpenDynamic,adLockPessimistic,adCmdText);
	   while(!m_set->EndOfFile)
	   {
		   ((CComboBox*)GetDlgItem(IDC_TYPE))->AddString((_bstr_t)m_set->GetCollect("事务类型"));
		   m_set->MoveNext();
	   }
	   m_set->Close();
	   strSQL="select distinct 事务主题 from 事务表";
	   m_set->Open(	(LPTSTR)strSQL.GetBuffer(130),_variant_t(m_pConn,true),adOpenDynamic,adLockPessimistic,adCmdText);
	   //添加事务主题
	   while(!m_set->EndOfFile)
	   {
		   ((CComboBox*)GetDlgItem(IDC_SUBJECT))->AddString((_bstr_t)m_set->GetCollect("事务主题"));
		   m_set->MoveNext();
	   }
	   m_set->Close();
	}
	catch(_com_error&e)
	{
		MessageBox(e.ErrorMessage());
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
/*
	CButton	m_search;
	CListCtrl	m_list;
	COleDateTime	m_beginTime;
	COleDateTime	m_endTime;
	CString	m_content;
	CString	m_subject;
	CString	m_type;
*/
void CSearchTansaction::OnSearch() 
{
	// TODO: Add your control notification handler code here
	//在这里我又花了很长时间,接受了血的教训,字段名一定要和数据库中的对应,别以为自己做的对,
	//还就是写SQL语句的格式要正确
	//改进:为了对错误进行隔离,防止影响正个程序的正常运行,我使用了异常捕获。如下
	UpdateData();
	CString m_searchString;
	m_searchString="select*from 事务表 where ";
	CString str;
	int year=m_beginTime.GetYear();
    int month=m_beginTime.GetMonth();
    int day=m_beginTime.GetDay();
	str.Format("'%d-%d-%d'",year,month,day);
	m_searchString=m_searchString+"事务日期 >"+str;
	//因为在写事务日期的时候很有可能误写为事物时间
//	m_searchString=m_searchString+"事务时间 >"+str;

	year=m_endTime.GetYear();
    month=m_endTime.GetMonth();
    day=m_endTime.GetDay();
	str.Format("'%d-%d-%d'",year,month,day);
	m_searchString+="and 事务日期<"+str;

	
	
	if(m_type!="")
	{
		
		m_searchString=m_searchString+"and 事务类型='"+m_type+"'";
	}

	
	if(m_subject!="")
	{
		
		m_searchString=m_searchString+"and 事务主题='"+m_subject+"'";
	}
	try
	{
		_ConnectionPtr m_pConn("ADODB.Connection");
		m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\DataSource\\office1.mdb;Persist Security Info=False","","",adConnectUnspecified);
	   _RecordsetPtr m_set("ADODB.Recordset");
		m_set->Open((LPTSTR)m_searchString.GetBuffer(130),_variant_t(m_pConn,true),adOpenDynamic,adLockPessimistic,adCmdText);
		int i=0;
		m_list.DeleteAllItems();
		while(!m_set->EndOfFile)
		{
			m_list.InsertItem(i,(_bstr_t)m_set->GetCollect("事务类型"));
			m_list.SetItemText(i,1,(_bstr_t)m_set->GetCollect("事务日期"));
			m_list.SetItemText(i,2,(_bstr_t)m_set->GetCollect("事务主题"));
			m_list.SetItemText(i,3,(_bstr_t)m_set->GetCollect("事务内容"));
			i++;
			m_set->MoveNext();
		}
	}
	catch(_com_error&e)
	{
		MessageBox(e.ErrorMessage());
	}
}
//经验:连接,读取数据库时候要使用异常技术,以免整个程序遭殃

⌨️ 快捷键说明

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