DBOperator.cpp
上传用户:xiaoke98
上传日期:2014-06-29
资源大小:5718k
文件大小:40k
- // DBOperator.cpp: implementation of the CDBOperator class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "HomeFinanceManager.h"
- #include "DBOperator.h"
- #include "SysStatus.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDBOperator gDBOperator;
- CDBOperator::CDBOperator()
- {
- //获取系统目录
- char strModuleFileName[400];
- ::GetModuleFileName(NULL, strModuleFileName, 400);
- char* pChar = strModuleFileName;
- while(*(pChar++) != ' '){};
- while(*(pChar--) != '\'){};
- *(++pChar) = ' ';
- m_strAppPath = strModuleFileName;
- if(ConnectToDB() == FALSE)
- {
- AfxMessageBox("连接的数据库不存在!系统将会退出");
- ExitProcess(1);
- }
- }
- CDBOperator::~CDBOperator()
- {
- ::CoUninitialize();
- }
- BOOL CDBOperator::ConnectToDB(void)
- {
- ::CoInitialize(NULL);
- try
- {
- m_DBConnection.CreateInstance(__uuidof(Connection));
- CString strConnectStr;
- strConnectStr.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s\HomeFinanceManager.mdb",
- m_strAppPath);
-
- m_DBConnection->CursorLocation = adUseClient;
- m_DBConnection->Open(_bstr_t( strConnectStr.GetBuffer(0)), L"", L"", -1);
-
- m_Command.CreateInstance(__uuidof(Command));
- m_Command->ActiveConnection = m_DBConnection;
- m_Command->CommandType = adCmdText;
-
- m_Recordset.CreateInstance(__uuidof(Recordset));
-
-
- return TRUE;
- }catch(_com_error &e)
- {
-
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- return FALSE;
- }
-
- return FALSE;
- }
- //-------------------------------------------------------------------------------------------------
- BOOL CDBOperator::addUser(CString strUserName, CString strPassword, char* pImgData, int iDataLen)
- {
- CString strSQL;
- strSQL.Format("select * from userinfo");
- try
- {
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset->Open("SELECT * FROM UserInfo",_variant_t((IDispatch *)m_DBConnection,true),adOpenDynamic,adLockPessimistic,adCmdText);
- state = m_Recordset->GetState();
- m_Recordset->AddNew();
- m_Recordset->PutCollect("UserName",_variant_t(strUserName));
- m_Recordset->PutCollect("Passwd",_variant_t(strPassword));
- state = m_Recordset->GetState();
- //添加图片信息
- char *pBuf = pImgData;
- VARIANT varBLOB;
- SAFEARRAY *psa;
- SAFEARRAYBOUND rgsabound[1];
- if(pBuf)
- {
- rgsabound[0].lLbound = 0;
- rgsabound[0].cElements = iDataLen;
- psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
- for (long i = 0; i < (long)iDataLen; i++)
- SafeArrayPutElement (psa, &i, pBuf++);
- varBLOB.vt = VT_ARRAY | VT_UI1;
- varBLOB.parray = psa;
- m_Recordset->GetFields()->GetItem("UserImg")->AppendChunk(varBLOB);
- }
-
-
- m_Recordset->Update();
-
- }
- catch(_com_error &e)
- {
- bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- AfxMessageBox("成功添加新用户");
- return TRUE;
- }
- //-------------------------------------------------------------------------------------
- BOOL CDBOperator::getUserImg(CString strUserName, char** pImageData, int& iDataLen)
- {
- CString strSQL;
- strSQL.Format("select UserImg from userinfo where UserName='%s'",
- strUserName.GetBuffer(0));
-
- try
- {
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- if(m_Recordset->adoEOF)
- {
- return FALSE;
- }
- iDataLen = m_Recordset->GetFields()->GetItem("UserImg")->ActualSize;
-
- if(iDataLen > 0)
- {
- *pImageData = new char[iDataLen + 1];
- _variant_t varBLOB;
- varBLOB = m_Recordset->GetFields()->GetItem("UserImg")->GetChunk(iDataLen);
- if(varBLOB.vt == (VT_ARRAY | VT_UI1))
- {
- char *pBuf = NULL;
- SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
- memcpy(*pImageData,pBuf,iDataLen); ///复制数据到缓冲区m_pBMPBuffer
- SafeArrayUnaccessData (varBLOB.parray);
- }
-
- return TRUE;
- }
-
-
- return FALSE;
- }
-
- catch(_com_error &e)
- {
- bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
-
-
- return TRUE;
-
- }
- //-------------------------------------------------------------------------------------------------
- BOOL CDBOperator::VerifyUser(CString& strUser, CString& strPasswd)
- {
- try
- {
- CString strSQL;
- strSQL.Format("select * from userinfo where UserName='%s' and Passwd='%s'",
- strUser.GetBuffer(0),
- strPasswd.GetBuffer(0));
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- if(m_Recordset->adoEOF)
- {
- return FALSE;
- }
- return TRUE;
-
- }
- catch(_com_error &e)
- {
-
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- return FALSE;
- }
- return FALSE;
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::selectFinanceIn(CString& strStartTime, CString& strEndTime, CFinanceDBGrid& DBGrid, CString& strTotalIn, SELECTORDER order)
- {
- try
- {
- CString strSQL;
- CString strCondtionSQL;
- strCondtionSQL.Format("where MoneyIncome.InDate between #%s# and #%s#", strStartTime, strEndTime);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- strSQL.Format("select SUM(MoneyIncome.InMoney) from MoneyIncome %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalIn = var_currency.Format(0);
- }
-
- }
- if(enDate == order)
- {
- strCondtionSQL.Format("where MoneyIncome.InDate between #%s# and #%s# order by MoneyIncome.InDate asc", strStartTime, strEndTime);
- }
- CString strSearchSQL;
- strSearchSQL.Format("select MoneyIncome.id as 编号, MoneyIncome.InMoney as 金额, ClassInfo.classremark as 类别 , MoneyIncome.InDate as 日期, MoneyIncome.operator as 操作人 , MoneyIncome.remark as 备注 from MoneyIncome inner join ClassInfo on ClassInfo.classid = MoneyIncome.InClass ");
- strSQL = strSearchSQL + strCondtionSQL;
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
-
- DBGrid.setRecordSet(m_Recordset);
- DBGrid.Reflesh();
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::ReOrderFinanceIn(CString& strStartTime,
- CString& strEndTime,
- CFinanceDBGrid& DBGrid,
- CString& strTotalIn,
- CString& strTitle,
- CString& strOder)
- {
- try
- {
- CString strSQL;
- CString strCondtionSQL;
- strCondtionSQL.Format("where MoneyIncome.InDate between #%s# and #%s#", strStartTime, strEndTime);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- strSQL.Format("select SUM(MoneyIncome.InMoney) from MoneyIncome %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalIn = var_currency.Format(0);
- }
-
- }
- CString strOderTitle;
- if(strTitle.Compare("金额") == 0)
- {
- strOderTitle = "MoneyIncome.InMoney";
- }
- if(strTitle.Compare("类别") == 0)
- {
- strOderTitle = "ClassInfo.classremark";
- }
- if(strTitle.Compare("日期") == 0)
- {
- strOderTitle = "MoneyIncome.InDate";
- }
- if(strTitle.Compare("操作人") == 0)
- {
- strOderTitle = "MoneyIncome.operator";
- }
-
- strCondtionSQL.Format("where MoneyIncome.InDate between #%s# and #%s# order by %s %s", strStartTime, strEndTime, strOderTitle, strOder);
-
- CString strSearchSQL;
- strSearchSQL.Format("select MoneyIncome.id as 编号, MoneyIncome.InMoney as 金额, ClassInfo.classremark as 类别 , MoneyIncome.InDate as 日期, MoneyIncome.operator as 操作人 , MoneyIncome.remark as 备注 from MoneyIncome inner join ClassInfo on ClassInfo.classid = MoneyIncome.InClass ");
- strSQL = strSearchSQL + strCondtionSQL;
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
-
- DBGrid.setRecordSet(m_Recordset);
- DBGrid.Reflesh();
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::addFinanceIn(CString& strMoney, CString& strClass,
- CString& strDate, CString& strRemark)
- {
- try
- {
- CString strUser;
- strUser = gSysStatus.getUserName();
- CString strSQL;
- strSQL.Format("INSERT INTO MoneyIncome (InMoney,InClass,InDate,operator,remark) VALUES (%s,%s,'%s','%s','%s')",
- strMoney, strClass, strDate, strUser, strRemark);
-
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Command->Execute(&vra, vt1, adCmdText);
- AfxMessageBox("成功添加一笔收入");
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::addInClass(CString& strClass)
- {
- try
- {
- if(strClass.IsEmpty() || getClassID(strClass) > -1)
- {
- AfxMessageBox("该类别已经存在或为空, 无法添加!");
- return;
- }
- int iClassID;
- CString strSQL;
- strSQL.Format("select max(classid) from classinfo");
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- if(!m_Recordset->adoEOF)
- {
- _variant_t Value = m_Recordset->GetCollect((long)0);
- iClassID = (long)Value;
- }
- iClassID += 1;
- strSQL.Format("insert into classinfo(classid, classremark) values(%d, '%s')", iClassID, strClass.GetBuffer(0));
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- m_Command->Execute(&vra, vt1, adCmdText);
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- AfxMessageBox("成功添加新的收入类别");
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::addOutClass(CString& strClass)
- {
- try
- {
- if(strClass.IsEmpty() || getOutClassID(strClass) > -1)
- {
- AfxMessageBox("该类别已经存在或为空, 无法添加!");
- return;
- }
- int iClassID;
- CString strSQL;
- strSQL.Format("select max(classid) from OutClass");
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- if(!m_Recordset->adoEOF)
- {
- _variant_t Value = m_Recordset->GetCollect((long)0);
- iClassID = (long)Value;
- }
- iClassID += 1;
-
- strSQL.Format("insert into OutClass(classid, remark) values(%d, '%s')", iClassID, strClass.GetBuffer(0));
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- m_Command->Execute(&vra, vt1, adCmdText);
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- AfxMessageBox("成功添加新的支出类别");
- }
- //-------------------------------------------------------------------------------------------------
- int CDBOperator::getClassID(CString& strClass)
- {
- int iClassID = -1;
- try
- {
- CString strSQL;
- strSQL.Format("select classid from classinfo where classremark = '%s'", strClass.GetBuffer(0));
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- if(!m_Recordset->adoEOF)
- {
- _variant_t Value = m_Recordset->GetCollect((long)0);
- iClassID = (long)Value;
- }
- return iClassID;
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- return iClassID;
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getMoneyInClassInfo(CSysDataStruct::CClassInfo& ClassInfo)
- {
- try
- {
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- CString strSQL;
- strSQL.Format("select classid, classremark from ClassInfo");
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- ClassInfo.m_iClassNum = 0;
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- while(!m_Recordset->adoEOF)
- {
-
- _variant_t Value = m_Recordset->GetCollect((long)1);
- if(Value.vt == VT_BSTR)
- {
- ClassInfo.m_Classes[ClassInfo.m_iClassNum] = CString(Value.bstrVal);
- }
-
- ClassInfo.m_iClassNum++;
-
- m_Recordset->MoveNext();
- }
-
- }
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
-
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getInClassInfo(CListCtrl* pdbGrid)
- {
- try
- {
- CString strSQL = "Select * from ClassInfo";
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- int iColumNum = pdbGrid->GetHeaderCtrl()->GetItemCount();
- RECT rcWindow;
- pdbGrid->GetClientRect(&rcWindow);
- for(int i = 0; i < iColumNum; i++)
- {
- pdbGrid->DeleteColumn(i);
- }
- int iListWidth = (rcWindow.right - rcWindow.left);
- LONG lStyle = pdbGrid->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
- lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
- pdbGrid->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)lStyle);
-
- LV_COLUMN lvc;
- lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH /*| LVCF_FMT*/;
-
- lvc.iSubItem = 0;
- lvc.pszText = "编号";
- lvc.cx = iListWidth / 5;
- pdbGrid->InsertColumn(0,&lvc);
-
- lvc.iSubItem = 1;
- lvc.pszText = "类别";
- lvc.cx = iListWidth - iListWidth / 5;
- pdbGrid->InsertColumn(1,&lvc);
-
-
- pdbGrid->DeleteAllItems();
-
-
-
- int iPos = -1;
-
- m_Recordset->MoveFirst();
- while(!m_Recordset->adoEOF)
- {
-
- LV_ITEM lvitem;
- lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
- lvitem.state = 0;
- lvitem.stateMask = 0;
-
- CString strTemp;
- _variant_t Value;
-
-
- lvitem.iItem = ++iPos;
- lvitem.iSubItem = 0;
-
- Value = m_Recordset->GetCollect("classid");
- int iClassid = (int)Value.lVal;
- strTemp.Format("%d", iClassid);
- lvitem.pszText = strTemp.GetBuffer(0);
- pdbGrid->InsertItem(&lvitem);
-
- Value = m_Recordset->GetCollect("classremark");
- strTemp = Value.bstrVal;
- pdbGrid->SetItemText(iPos, 1, (LPCTSTR)strTemp.GetBuffer(0));
-
- m_Recordset->MoveNext();
- }
-
- }
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
-
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::selectFinanceOut(CString& strStartTime,
- CString& strEndTime,
- CFinanceDBGrid& DBGrid,
- CString& strTotalOut,
- SELECTORDER order)
- {
- try
- {
- CString strSQL;
- CString strCondtionSQL;
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s# ", strStartTime, strEndTime);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- long recordCount;
-
- strSQL.Format("select SUM(MoneyOut.OutMoney) from MoneyOut %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalOut = var_currency.Format(0);
- }
-
- }
-
- if( enDate == order)
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s# order by MoneyOut.Outdate asc", strStartTime, strEndTime);
- if( enMoney == order)
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s# order by MoneyOut.OutMoney desc", strStartTime, strEndTime);
- CString strSearchSQL;
- strSearchSQL.Format("select MoneyOut.id as 编号, MoneyOut.OutMoney as 金额, OutClass.remark as 类别 ,
- MoneyOut.remark as 备注, MoneyOut.Outdate as 日期 , MoneyOut.operator as 操作人
- from MoneyOut inner join OutClass on OutClass.classid = MoneyOut.OutClass ");
-
- /*strSearchSQL.Format("select MoneyOut.id as 编号, MoneyOut.OutMoney as 金额, MoneyOut.Outdate as 日期, MoneyOut.operator as 操作人 , MoneyOut.remark as 备注
- from MoneyOut ");*/
-
- strSQL = strSearchSQL + strCondtionSQL;
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
-
- DBGrid.setRecordSet(m_Recordset);
- DBGrid.Reflesh();
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::ReOrderFinanceOut(CString& strStartTime,
- CString& strEndTime,
- CFinanceDBGrid& DBGrid,
- CString& strTotalOut,
- CString& strTitle,
- CString& strOder)
- {
- try
- {
- CString strSQL;
- CString strCondtionSQL;
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s# ", strStartTime, strEndTime);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- long recordCount;
-
- strSQL.Format("select SUM(MoneyOut.OutMoney) from MoneyOut %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalOut = var_currency.Format(0);
- }
-
- }
- CString strOrderTitle;
- if(strTitle.Compare("金额") == 0)
- {
- strOrderTitle.Format("%s", "MoneyOut.OutMoney");
- }
- if(strTitle.Compare("类别") == 0)
- {
- strOrderTitle.Format("%s", "OutClass.remark");
- }
- if(strTitle.Compare("日期") == 0)
- {
- strOrderTitle.Format("%s", "MoneyOut.Outdate");
- }
- if(strTitle.Compare("操作人") == 0)
- {
- strOrderTitle.Format("%s", "MoneyOut.operator");
- }
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s# order by %s %s", strStartTime, strEndTime, strOrderTitle, strOder);
-
- CString strSearchSQL;
- strSearchSQL.Format("select MoneyOut.id as 编号, MoneyOut.OutMoney as 金额, OutClass.remark as 类别 ,
- MoneyOut.remark as 备注, MoneyOut.Outdate as 日期 , MoneyOut.operator as 操作人
- from MoneyOut inner join OutClass on OutClass.classid = MoneyOut.OutClass ");
-
- /*strSearchSQL.Format("select MoneyOut.id as 编号, MoneyOut.OutMoney as 金额, MoneyOut.Outdate as 日期, MoneyOut.operator as 操作人 , MoneyOut.remark as 备注
- from MoneyOut ");*/
-
- strSQL = strSearchSQL + strCondtionSQL;
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
-
- DBGrid.setRecordSet(m_Recordset);
- DBGrid.Reflesh();
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //支出相关函数
- //-------------------------------------------------------------------------------------------------
- int CDBOperator::getOutClassID(CString& strClass)
- {
- int iClassID = -1;
- try
- {
- CString strSQL;
- strSQL.Format("select classid from OutClass where remark = '%s'", strClass.GetBuffer(0));
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- if(!m_Recordset->adoEOF)
- {
- _variant_t Value = m_Recordset->GetCollect((long)0);
- iClassID = (long)Value;
- }
- return iClassID;
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- return iClassID;
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::addFinanceOut(CString& strMoney, CString& strClass, CString& strDate, CString& strRemark)
- {
- try
- {
- CString strUser;
- strUser = gSysStatus.getUserName();
- CString strSQL;
- strSQL.Format("INSERT INTO MoneyOut (OutMoney,OutClass,Outdate,operator,remark) VALUES (%s,%s,'%s','%s','%s')",
- strMoney, strClass, strDate, strUser, strRemark);
-
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- _variant_t vra;
- VARIANT *vt1 = NULL;
- m_Command->Execute(&vra, vt1, adCmdText);
- AfxMessageBox("成功添加一笔支出");
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getMoneyOutClassInfo(CSysDataStruct::CClassInfo& ClassInfo)
- {
- try
- {
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- CString strSQL;
- strSQL.Format("select classid, remark from OutClass");
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- ClassInfo.m_iClassNum = 0;
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- while(!m_Recordset->adoEOF)
- {
-
- _variant_t Value = m_Recordset->GetCollect((long)1);
- if(Value.vt == VT_BSTR)
- {
- ClassInfo.m_Classes[ClassInfo.m_iClassNum] = CString(Value.bstrVal);
- }
-
- ClassInfo.m_iClassNum++;
-
- m_Recordset->MoveNext();
- }
-
- }
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getOutClassInfo(CListCtrl* pdbGrid)
- {
- try
- {
- CString strSQL = "Select * from outclass";
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
-
- _variant_t vra;
- VARIANT *vt1 = NULL;
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- int iColumNum = pdbGrid->GetHeaderCtrl()->GetItemCount();
- RECT rcWindow;
- pdbGrid->GetClientRect(&rcWindow);
- for(int i = 0; i < iColumNum; i++)
- {
- pdbGrid->DeleteColumn(i);
- }
- int iListWidth = (rcWindow.right - rcWindow.left);
- LONG lStyle = pdbGrid->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
- lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
- pdbGrid->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)lStyle);
-
- LV_COLUMN lvc;
- lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH /*| LVCF_FMT*/;
-
- lvc.iSubItem = 0;
- lvc.pszText = "编号";
- lvc.cx = iListWidth / 5;
- pdbGrid->InsertColumn(0,&lvc);
-
- lvc.iSubItem = 1;
- lvc.pszText = "类别";
- lvc.cx = iListWidth - iListWidth / 5;
- pdbGrid->InsertColumn(1,&lvc);
-
-
- pdbGrid->DeleteAllItems();
-
-
-
- int iPos = -1;
-
- m_Recordset->MoveFirst();
- while(!m_Recordset->adoEOF)
- {
-
- LV_ITEM lvitem;
- lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
- lvitem.state = 0;
- lvitem.stateMask = 0;
-
- CString strTemp;
- _variant_t Value;
-
-
- lvitem.iItem = ++iPos;
- lvitem.iSubItem = 0;
-
- Value = m_Recordset->GetCollect("classid");
- int iClassid = (int)Value.lVal;
- strTemp.Format("%d", iClassid);
- lvitem.pszText = strTemp.GetBuffer(0);
- pdbGrid->InsertItem(&lvitem);
-
- Value = m_Recordset->GetCollect("remark");
- strTemp = Value.bstrVal;
- pdbGrid->SetItemText(iPos, 1, (LPCTSTR)strTemp.GetBuffer(0));
-
- m_Recordset->MoveNext();
- }
-
- }
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::DeleteFinanceIn(int iID)
- {
- try
- {
- CString strSQL;
- _variant_t vra;
- VARIANT *vt1 = NULL;
- strSQL.Format("delete from MoneyIncome where [id]=%d", iID);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- m_Command->Execute(&vra, vt1, adCmdText);
-
- AfxMessageBox("成功删除记录");
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::DeleteFinanceOut(int iID)
- {
- try
- {
- CString strSQL;
- _variant_t vra;
- VARIANT *vt1 = NULL;
- strSQL.Format("delete from MoneyOut where [id]=%d", iID);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- m_Command->Execute(&vra, vt1, adCmdText);
-
- AfxMessageBox("成功删除记录");
-
-
- }catch(_com_error &e)
- {
- _bstr_t bstrSource(e.Source());
- _bstr_t bstrDescription(e.Description());
- TRACE("Exception throw for classes generated by #import");
- TRACE("tCode=%08lxn", e.Error);
- TRACE("tCode meaning = %sn", e.ErrorMessage);
- TRACE("tSource = %sn", (LPCTSTR)bstrSource);
- TRACE("tDescription = %sn", (LPCTSTR)bstrDescription);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::UpdateRecord(int iID, CString& strSum, CString& strClass,
- CString& strDate, CString& strRemark, bool bIsMoneyOut)
- {
- CString strSQL;
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- if(bIsMoneyOut)
- {
- strSQL.Format("select classid from outclass where remark='%s'", strClass);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- int recordCount = m_Recordset->GetRecordCount();
- if(recordCount < 1)
- {
- ::MessageBox(NULL, "指定的类别不存在", "错误", MB_OK);
- return;
- }
- int iClassID;
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- iClassID = (long)Value;
-
-
-
- strSQL.Format("update moneyout set outmoney=%s, OutClass=%d, outdate='%s', remark='%s' where id = %d",
- strSum,
- iClassID,
- strDate,
- strRemark,
- iID);
-
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- m_Command->Execute(&vra, vt1, adCmdText);
- }
- else
- {
- strSQL.Format("select classid from ClassInfo where classremark='%s'", strClass);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- int recordCount = m_Recordset->GetRecordCount();
- if(recordCount < 1)
- {
- ::MessageBox(NULL, "指定的类别不存在", "错误", MB_OK);
- return;
- }
- int iClassID;
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- iClassID = (long)Value;
-
- strSQL.Format("update MoneyIncome set InMoney=%s, InClass=%d, InDate='%s', remark='%s' where id = %d",
- strSum,
- iClassID,
- strDate,
- strRemark,
- iID);
-
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- m_Command->Execute(&vra, vt1, adCmdText);
-
- }
-
-
- ::MessageBox(NULL, "成功更改指定记录", "提示", MB_OK);
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getBalance(CString& strStartTime,
- CString& strEndTime,
- CTreeCtrl& treeBalance,
- CString& strTotalIn,
- CString& strTotalOut)
- {
- //获取收入总额
- strTotalIn = "0";
- strTotalOut = "0";
- CString strSQL;
- CString strCondtionSQL;
- strCondtionSQL.Format("where MoneyIncome.InDate between #%s# and #%s#", strStartTime, strEndTime);
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- strSQL.Format("select SUM(MoneyIncome.InMoney) from MoneyIncome %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- long recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalIn = var_currency.Format(0);
- }
-
- }
- //获取支出总额
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s#", strStartTime, strEndTime);
-
- strSQL.Format("select SUM(MoneyOut.OutMoney) from MoneyOut %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- recordCount = m_Recordset->GetRecordCount();
- if(recordCount)
- {
- m_Recordset->MoveFirst();
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalOut = var_currency.Format(0);
- }
-
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getMoneyInClassTotal(CString& strStartTime, CString& strEndTime, CSysDataStruct::CClassInfo& ClassInfo)
- {
- getMoneyInClassInfo(ClassInfo);
- for(int iClass = 0; iClass < ClassInfo.m_iClassNum; iClass++)
- {
- CString strSQL;
- CString strCondtionSQL;
- CString strTotalIn = "0";
- strCondtionSQL.Format("where MoneyIncome.InDate between #%s# and #%s# and MoneyIncome.InClass=%d", strStartTime, strEndTime, iClass);
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- strSQL.Format("select SUM(MoneyIncome.InMoney) from MoneyIncome %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalIn = var_currency.Format(0);
- }
- ClassInfo.m_dTotal[iClass] = atof(strTotalIn);
- }
- }
- //-------------------------------------------------------------------------------------------------
- void CDBOperator::getMoneyOutClassTotal(CString& strStartTime, CString& strEndTime, CSysDataStruct::CClassInfo& ClassInfo)
- {
- getMoneyOutClassInfo(ClassInfo);
- for(int iClass = 0; iClass < ClassInfo.m_iClassNum; iClass++)
- {
- CString strSQL;
- CString strCondtionSQL;
- CString strTotalIn = "0";
- strCondtionSQL.Format("where MoneyOut.Outdate between #%s# and #%s# and MoneyOut.OutClass=%d", strStartTime, strEndTime, iClass);
- _variant_t vra;
- VARIANT *vt1 = NULL;
-
- strSQL.Format("select SUM(MoneyOut.OutMoney) from MoneyOut %s", strCondtionSQL);
- m_Command->CommandText = (_bstr_t) strSQL.GetBuffer(0);
- long state = m_Recordset->GetState();
- if(state)
- {
- m_Recordset->Close();
- }
- m_Recordset = m_Command->Execute(&vra, vt1, adCmdText);
- _variant_t Value = m_Recordset->GetCollect((long)0);
- COleCurrency var_currency;
- if(Value.vt == VT_CY)
- {
- var_currency = Value.cyVal;
- strTotalIn = var_currency.Format(0);
- }
- ClassInfo.m_dTotal[iClass] = atof(strTotalIn);
- }
- }