?? 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 + -