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

📄 family_financ_systemdlg.cpp

📁 运用ADO技术连接ACCESS数据库(改变连接字串就能连接SQL数据库)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// family_financ_systemDlg.cpp : implementation file
//

#include "stdafx.h"
#include "family_financ_system.h"
#include "family_financ_systemDlg.h"
#include "PLAIN.h"
#include "FAMILY.h"
#include "CHAXUN.h"

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


CComboBox *m_IO_TYP;
CComboBox *m_F_MENBER;
int this_year,this_month,this_day;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFamily_financ_systemDlg dialog

CFamily_financ_systemDlg::CFamily_financ_systemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFamily_financ_systemDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFamily_financ_systemDlg)
	m_money = 0.0f;
	m_beizhu = _T("");
	m_this_total_plain = 0.0f;
	m_this_total_out = 0.0f;
	m_this_l_plain = 0.0f;
	m_this_l_true_out = 0.0f;
	m_this_p_plain = 0.0f;
	m_this_p_true_out = 0.0f;
	m_last_total_plain = 0.0f;
	m_last_true_total_out = 0.0f;
	m_last_l_plain = 0.0f;
	m_last_l_true_out = 0.0f;
	m_last_p_plain = 0.0f;
	m_last_p_out = 0.0f;
	m_day = _T("");
	m_f_mb = _T("");
	m_io_t = _T("");
	m_money_sum_in = 0.0f;
	m_money_sum_out = 0.0f;
	m_money_sum_end = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFamily_financ_systemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFamily_financ_systemDlg)
	DDX_Control(pDX, ID_DELERT, m_delert);
	DDX_Control(pDX, IDC_INSERT, m_insert);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT2, m_money);
	DDX_Text(pDX, IDC_EDIT3, m_beizhu);
	DDX_Text(pDX, IDC_EDIT7, m_this_total_plain);
	DDX_Text(pDX, IDC_EDIT8, m_this_total_out);
	DDX_Text(pDX, IDC_EDIT9, m_this_l_plain);
	DDX_Text(pDX, IDC_EDIT10, m_this_l_true_out);
	DDX_Text(pDX, IDC_EDIT11, m_this_p_plain);
	DDX_Text(pDX, IDC_EDIT12, m_this_p_true_out);
	DDX_Text(pDX, IDC_EDIT13, m_last_total_plain);
	DDX_Text(pDX, IDC_EDIT14, m_last_true_total_out);
	DDX_Text(pDX, IDC_EDIT15, m_last_l_plain);
	DDX_Text(pDX, IDC_EDIT16, m_last_l_true_out);
	DDX_Text(pDX, IDC_EDIT17, m_last_p_plain);
	DDX_Text(pDX, IDC_EDIT18, m_last_p_out);
	DDX_Text(pDX, IDC_EDIT1, m_day);
	DDX_CBString(pDX, IDC_COMBO2, m_f_mb);
	DDX_CBString(pDX, IDC_COMBO1, m_io_t);
	DDX_Text(pDX, IDC_EDIT4, m_money_sum_in);
	DDX_Text(pDX, IDC_EDIT5, m_money_sum_out);
	DDX_Text(pDX, IDC_EDIT6, m_money_sum_end);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFamily_financ_systemDlg, CDialog)
	//{{AFX_MSG_MAP(CFamily_financ_systemDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INSERT, OnInsert)
	ON_BN_CLICKED(ID_F_MENBER, OnFMenber)
	ON_BN_CLICKED(ID_TONGJI, OnTongji)
	ON_BN_CLICKED(ID_PLAIN, OnPlain)
	ON_BN_CLICKED(ID_DELERT, OnDelert)
	ON_WM_CLOSE()
	ON_BN_CLICKED(ID_DELERT2, OnDelert2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFamily_financ_systemDlg message handlers

BOOL CFamily_financ_systemDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	// 初始化列表
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	m_list.InsertColumn(0,"时间",LVCFMT_LEFT,100,-1);
	m_list.InsertColumn(1,"种类",LVCFMT_LEFT,100,-1);
	m_list.InsertColumn(2,"金额(元)",LVCFMT_LEFT,100,-1);
	m_list.InsertColumn(3,"经手人",LVCFMT_LEFT,100,-1);
	m_list.InsertColumn(4,"备注(具体情况)",LVCFMT_LEFT,200,-1);
	//////////////////////////////////////////////////////////////////////
	COleDateTime dateNow;
	dateNow = COleDateTime::GetCurrentTime();     // 获取当前日期时间
	CString   str=dateNow.Format(VAR_DATEVALUEONLY); // 获取当前日
	m_day=str;
	sscanf(m_day,"%d-%d-%d",&this_year,&this_month,&this_day);  
	//dateNow(this_year,this_month,1,0,0,0);
	COleDateTime t_time(this_year,this_month,1,0,0,0);//strDate=time.Format("%Y-%m-%d");
	///////////////////////////////////////////////////////////////
	char   drive[4];//驱动器号   MAX_DRIVER   //得到数据库路径
	char   dir[MAX_PATH];//目录   
	char   title[MAX_PATH];//文件标题   
	char   ext[MAX_PATH];   //扩展名 
	char   dstpath[MAX_PATH];   
    _splitpath(_pgmptr,drive,dir,title,ext);
	_makepath(dstpath,drive,dir,"notes",".mdb");   
	Title1="Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=";
	Title1=Title1+dstpath;
	Title1=Title1+";Mode=Share Deny None;Extended Properties="";Jet OLEDB:Database Password="";";	
	////////////////////////////////////////////////////////////////////////////////////
	int i=0;
	int nstep;
	_bstr_t temp;
	
	////////////////////////////////////////////////////////////////////////////////
	CoInitialize(NULL);    //初始化COM组件
	_ConnectionPtr pConn(__uuidof(Connection));    //实例化一个connection对象pConn
	_RecordsetPtr pRst(__uuidof(Recordset));    //实例化一个Recordset对象pRst
	_CommandPtr pCmd(__uuidof(Command));    //实例化一个Command对象pCmd
	pConn->ConnectionString=(_bstr_t)Title1;
	//通过pConn对象连接字符串,连接到ACCESS数据库
	HRESULT hr = pConn->Open("","","",adConnectUnspecified);
	// 以同步的方式进行连接  open("连接字串","用户名","密码","连接方式") 
	// 连接方式有"adConnectUnspecified"(同步) 和 "adAsyncConnect" (异步)
	pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn));
	pCmd->CommandText="SELECT * FROM 家庭财务表";    //通过pCmd对象访问数据库
	pRst=pCmd->Execute(NULL,NULL,adCmdText);
	for(;!pRst->EndEOF; pRst->MoveNext()) //  在数据库中寻找	pRecord->MoveFirst()
	{		
		temp=pRst->GetCollect("时间");
		nstep=m_list.InsertItem(i,temp);
		temp=pRst->GetCollect("种类");
		m_list.SetItemText(nstep,1,temp);	
		temp=pRst->GetCollect("金额");
		m_list.SetItemText(nstep,2,temp);
		temp=pRst->GetCollect("经手人");
		m_list.SetItemText(nstep,3,temp);
		temp=pRst->GetCollect("备注(具体情况)");
		m_list.SetItemText(nstep,4,temp);
	}
	////////////////////////////////////////////////////////////////////
	m_IO_TYP=(CComboBox *)GetDlgItem(IDC_COMBO1);
	m_F_MENBER=(CComboBox *)GetDlgItem(IDC_COMBO2);
	///////////////////////////////////////////////////////////////////////
	pCmd->CommandText="SELECT * FROM 收支类别表";    //通过pCmd对象访问数据库
	pRst=pCmd->Execute(NULL,NULL,adCmdText);
	while(!pRst->EndEOF)
	{
		temp=pRst->GetCollect("种类");
		m_IO_TYP->AddString(temp);
		//把数据库中收支种类这列的内容放入控件中
		pRst->MoveNext();    //下移一个
	}
	m_io_t=(char *)temp;
	/////////////////////////////////////////////////////////////////////
	pCmd->CommandText="SELECT * FROM 家庭成员表";    //通过pCmd对象访问数据库
	pRst=pCmd->Execute(NULL,NULL,adCmdText);
	if(pRst->EndEOF)
	{	
		AfxMessageBox("请先建立家庭成员表!");	
		OnFMenber();
		if(is_ok=="OK")
		{

			_ConnectionPtr pConn1(__uuidof(Connection));    //实例化一个connection对象pConn
			_RecordsetPtr pRst1(__uuidof(Recordset));    //实例化一个Recordset对象pRst
			_CommandPtr pCmd1(__uuidof(Command));    //实例化一个Command对象pCmd
			pConn1->ConnectionString=(_bstr_t)Title1;
			//通过pConn对象连接字符串,连接到ACCESS数据库
			HRESULT hr = pConn1->Open("","","",adConnectUnspecified);
			// 以同步的方式进行连接  open("连接字串","用户名","密码","连接方式") 
			// 连接方式有"adConnectUnspecified"(同步) 和 "adAsyncConnect" (异步)
			pCmd1->put_ActiveConnection(_variant_t((IDispatch*)pConn));
			pCmd1->CommandText="SELECT * FROM 家庭成员表";    //通过pCmd对象访问数据库
			pRst1=pCmd->Execute(NULL,NULL,adCmdText);
			while(!pRst1->EndEOF)
			{
				temp=pRst1->GetCollect("家庭成员姓名");
				m_F_MENBER->AddString(temp);
				//把数据库中收支种类这列的内容放入控件中
				pRst1->MoveNext();    //下移一个
			}
			m_f_mb=(char *)temp;
			pRst1->Close();    //关闭对象
			pConn1->Close();
			pCmd1.Release();    //释放对象
			pRst1.Release();
			pConn1.Release();

		}
		else
		{
			CDialog::OnOK();
		}
	}	
	else
	{
		while(!pRst->EndEOF)
		{
			temp=pRst->GetCollect("家庭成员姓名");
			m_F_MENBER->AddString(temp);
			//把数据库中收支种类这列的内容放入控件中
			pRst->MoveNext();    //下移一个
		}
		m_f_mb=(char *)temp;
	}

	/////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////
	CString	string_temp;
	char *char_temp;
	int last_year,last_month;
	if(this_month!=1)
	{
		last_year=this_year;
		last_month=this_month-1;
	}
	else
	{
		last_year=this_year-1;
		last_month=12;
	}
	CString	sqlstr="SELECT * FROM 月统计表 where (年=";
				string_temp.Format("%d",last_year);
				sqlstr=sqlstr+string_temp;
				sqlstr=sqlstr+") and (月=";
				string_temp.Format("%d",last_month);
				sqlstr=sqlstr+string_temp;
				sqlstr=sqlstr+")";

	pCmd->CommandText=_bstr_t(sqlstr);    //通过pCmd对象访问数据库
	pRst=pCmd->Execute(NULL,NULL,adCmdText);
	if(!pRst->EndEOF)
	{
		temp=pRst->GetCollect("总收入");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_money_sum_in);
		temp=pRst->GetCollect("总支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_money_sum_out);
		temp=pRst->GetCollect("月支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_last_true_total_out);
		temp=pRst->GetCollect("总计划支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_last_total_plain);
		temp=pRst->GetCollect("生活计划支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_last_l_plain);
		temp=pRst->GetCollect("休闲计划支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_last_p_plain);
		temp=pRst->GetCollect("实际生活支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_last_l_true_out);
		temp=pRst->GetCollect("实际休闲支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_last_p_out);
	}
	else
	{
		/////////////////////////////////////////////////////////////////////
		pCmd->CommandText="SELECT * FROM 帮助表";    //通过pCmd对象访问数据库
		pRst=pCmd->Execute(NULL,NULL,adCmdText);
		if(!pRst->EndEOF)
		{
			temp=pRst->GetCollect("目前总收入");
			char_temp=temp;
			sscanf(char_temp,"%f",&m_money_sum_in);
			temp=pRst->GetCollect("目前总消费");
			char_temp=temp;
			sscanf(char_temp,"%f",&m_money_sum_out);
		}
	}
	/////////////////////////////////////////////////////////////////////	
				sqlstr="SELECT * FROM 月统计表 where (年=";
				string_temp.Format("%d",this_year);
				sqlstr=sqlstr+string_temp;
				sqlstr=sqlstr+") and (月=";
				string_temp.Format("%d",this_month);
				sqlstr=sqlstr+string_temp;
				sqlstr=sqlstr+")";
	//char_temp=sqlstr;
	pCmd->CommandText=_bstr_t(sqlstr);    //通过pCmd对象访问数据库
	pRst=pCmd->Execute(NULL,NULL,adCmdText);
	
	if(!pRst->EndEOF)
	{
		temp=pRst->GetCollect("总收入");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_money_sum_in);
		temp=pRst->GetCollect("总支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_money_sum_out);
		temp=pRst->GetCollect("月收入");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_this_money_in);
		temp=pRst->GetCollect("月支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_this_money_out);
		temp=pRst->GetCollect("总计划支出");
		char_temp=temp;
		sscanf(char_temp,"%f",&m_this_total_plain);

⌨️ 快捷键说明

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