📄 account.cpp
字号:
// Account.cpp : Implementation of CAccount
#include "stdafx.h"
#include "Bank.h"
#include "Account.h"
/////////////////////////////////////////////////////////////////////////////
// CAccount
/*
STDMETHODIMP CAccount::Login(BSTR AccountID, BSTR Pswd, BOOL *pIsValid)
{
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CAccount::GetCurrentFund(BSTR AccountID, float *pCurFund)
{
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CAccount::Deposit(BSTR AccountID, float saving, float *pCurFund)
{
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CAccount::WithDraw(BSTR AccountID, float drawing, float *pCurFund)
{
// TODO: Add your implementation code here
return S_OK;
}
*/
BOOL CAccount::ConnectDBSource(/*BSTR AccountID,BSTR Pswd*/)
{
//----连接数据源---------------
try
{
CoInitialize(NULL); // 初始化COM.
m_pConnection.CreateInstance(__uuidof(Connection)); //实例化_ConnectionPtr对象,并调用Open方法
m_pConnection->Open("DSN=BankAccount;", _bstr_t(""), _bstr_t(""),adModeUnknown );
if (NULL== m_pConnection)
{
MessageBox(NULL,_T("连接数据源出错!"),_T("ERROR"),MB_OK);
return FALSE;
}
else
return TRUE;
}
catch(_com_error &e)
{
_bstr_t bstrError(e.ErrorMessage());
MessageBox(NULL,bstrError,_T("ERROR"),MB_OK);
}
catch (...)
{
MessageBox(NULL,_T("未知错误!"),_T("ERROR"),MB_OK);
}
}
//==接口函数=================================================================
STDMETHODIMP CAccount::Login(BSTR AccountID, BSTR Pswd, BOOL *pIsValid)
{
if(ConnectDBSource())
{
//---创建命令----------------------------------------
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConnection;
_bstr_t strSQL ="Select * From Account Where AccountID Like '";
strSQL+=AccountID;
strSQL+="' And Password Like'";
strSQL+=Pswd;
strSQL+="'";
pCommand->CommandText = strSQL ; //拼写查询字串
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open((IDispatch *) pCommand, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnknown);
//----------------------
if(pRecordset->adoEOF) //若未找到则返回FALSE
*pIsValid = FALSE;
else //若找到则返回TRUE
*pIsValid = TRUE;
CoUninitialize();
}
return S_OK;
}
//==获取当前存款余额===========================================
STDMETHODIMP CAccount::GetCurrentFund(BSTR AccountID, float *pCurFund)
{
if(ConnectDBSource())
{
//--创建命令--------------------
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConnection;
_bstr_t strSQL ="Select * From Account Where AccountID Like '";
strSQL+=AccountID;
strSQL+="'";
pCommand->CommandText = strSQL ; //拼写查询字串
//--获取数据集--------------------
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open((IDispatch *) pCommand, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnknown);
if(!pRecordset->adoEOF)
{
FieldsPtr pFields=pRecordset->Fields;
FieldPtr pValue = pFields->GetItem("CurrentFund"); //取CurrentFund字段的值
*pCurFund =pValue->Value;
}
CoUninitialize();
}
return S_OK;
}
//==存钱=====================================================
STDMETHODIMP CAccount::Deposit(BSTR AccountID, float saving, float *pCurFund)
{
if(ConnectDBSource())
{
//--创建命令--------------------
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConnection;
_bstr_t strSQL ="Select * From Account Where AccountID Like '";
strSQL+=AccountID;
strSQL+="'";
pCommand->CommandText = strSQL ; //拼写查询字串
//--获取数据集--------------------
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open((IDispatch *) pCommand, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnknown);
if(!pRecordset->adoEOF)
{
FieldsPtr pFields=pRecordset->Fields;
FieldPtr pValue = pFields->GetItem("CurrentFund"); //取CurrentFund字段的值
float curFund = pValue->Value;
curFund= curFund+saving;
float temp = curFund;
pCurFund = &temp;
//--更新纪录-------------
char buffer[30];
_gcvt( curFund, 30, buffer );
_variant_t vColumn,vValue;
vColumn.SetString("CurrentFund");
vValue.SetString(buffer);
pRecordset->Update(vColumn,vValue);
}
CoUninitialize();
}
return S_OK;
}
//==取钱=====================================================
STDMETHODIMP CAccount::WithDraw(BSTR AccountID, float drawing, float *pCurFund)
{
if(ConnectDBSource())
{
//--创建命令--------------------
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConnection;
_bstr_t strSQL ="Select * From Account Where AccountID Like '";
strSQL+=AccountID;
strSQL+="'"; //!!!
pCommand->CommandText = strSQL ; //拼写查询字串
//--获取数据集--------------------
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
pRecordset->Open((IDispatch *) pCommand, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnknown); //勿将adLockOptimistic设为adLockBatchOptimistic
if(!pRecordset->adoEOF)
{
FieldsPtr pFields=pRecordset->Fields;
FieldPtr pValue = pFields->GetItem("CurrentFund"); //取CurrentFund字段的值
float curFund = pValue->Value;
curFund -= drawing;
float temp = curFund;
pCurFund = &temp; //返回值
//--更新纪录-------------
char buffer[30];
_gcvt( curFund, 30, buffer );
_variant_t vColumn,vValue;
vColumn.SetString("CurrentFund");
vValue.SetString(buffer);
pRecordset->Update(vColumn,vValue);
}
CoUninitialize();
}
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -