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

📄 adoconnect.cpp

📁 个人理财系统(管理股票信息)
💻 CPP
字号:
// AdoConnect.cpp: implementation of the CAdoConnect class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AdoConnect.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAdoConnect::CAdoConnect()
{
	m_connect = NULL;	
}

CAdoConnect::~CAdoConnect()
{
	if(  m_connect )
		m_connect->Close();
}

BOOL CAdoConnect::InitInstance()
{
	try
	{
		m_connect.CreateInstance(__uuidof(Connection));
	}
	catch ( _com_error e )
	{
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
		TRACE(errormessage);
		return FALSE;
	}
	return TRUE;
}

BOOL CAdoConnect::ConnectDb(CString &connectstr)
{
	m_connect.CreateInstance(__uuidof(Connection));
	try
	{
		m_connect->Open((_bstr_t)connectstr, "", "", adModeUnknown);
	}
	catch(_com_error e)
	{
		CString strError;
		strError.Format("警告:打开连接发生异常.错误信息%s",e.ErrorMessage());
		AfxMessageBox(strError);
		return FALSE;
	}
	return TRUE;
}

BOOL CAdoConnect::GetRecordSet(CString &SqlCommand, CAdoRecordSet& rec)
{
	_RecordsetPtr ptr;
	//ptr.CreateInstance(__uuidof(Recordset));
	try
	{
		ptr = m_connect->Execute((_bstr_t)SqlCommand, NULL, adCmdText);
 		rec.SetRecord(ptr);
 		//如果指针指向最后一个,说明没数据
 		if (ptr->adoEOF) {
 			return 2;
 		}
	}
	catch ( _com_error e )
	{
		CString errormessage;
		errormessage.Format("AdoConect->GetRecordSet!\r\n错误信息:%s",e.ErrorMessage());
		TRACE(errormessage);
		return FALSE;
	}
	return TRUE;
}

BOOL CAdoConnect::ExecuteCmd(CString &SqlCommand)
{
	try
	{	
		_CommandPtr pCmd(__uuidof(Command));
		pCmd->put_ActiveConnection(_variant_t((IDispatch*)m_connect));
		pCmd->CommandText=SqlCommand.AllocSysString();
		pCmd->Execute(NULL,NULL,adCmdText);
	}
	catch (_com_error e)
	{
		CString errormessage;
		errormessage.Format("SQL语句失败!\r\n错误信息:%s",e.ErrorMessage());
		AfxMessageBox(errormessage);
		return FALSE;
	}
	return TRUE;
}
//修改用户名
BOOL CAdoConnect::AmendUserName(CString &oldname, CString &newname)
{	
	CString sqlcommand;
	sqlcommand.Format("update tb_account set name='%s' where name='%s'",\
		newname, oldname);
	return ExecuteCmd(sqlcommand);
}
//判断密码是否正确
BOOL CAdoConnect::IsThePwd(CString &username, CString &inputpwd)
{
	CAdoRecordSet				record;
	CString						command;
	command.Format("select * from tb_account where name='%s' and password='%s'",username,inputpwd);
	GetRecordSet(command, record);
	if (record.IsEof())
	{
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}
//修改密码
BOOL CAdoConnect::AmendPwd(CString &user, CString &newpwd)
{
	CString command;
	command.Format("update tb_account set password='%s' where name='%s'",\
		newpwd, user);
	return ExecuteCmd(command);
}

//判断是否是相同的用户名
BOOL CAdoConnect::IsTheSameName(CString &newname)
{
	_RecordsetPtr ptr;
	CAdoRecordSet rec;
	CString command;
	command.Format("select password from tb_account where name='%s'",newname);
	try
	{
		ptr = m_connect->Execute((_bstr_t)command, NULL, adCmdText);
		rec.SetRecord(ptr);
		if (!ptr->adoEOF)
		{
			return 2;
		}
	}
	catch ( _com_error e )
	{
		CString errormessage;
		errormessage.Format("执行判断用户名是否相同SQL语句失败!\r\n错误信息:%s",e.ErrorMessage());
		TRACE(errormessage);
		return FALSE;
	}
	return TRUE;
}

//新建用户
BOOL CAdoConnect::InsertNewAccount(CString &user, CString &pwd, CString &dealtpye, double &balance, CString &remark)
{
	CString				command;
	CTime				time;
	CString				accountid;
	time = CTime::GetCurrentTime();
	accountid = time.Format("%Y%M%D%H%M%S");
	command.Format("insert into tb_account values('%s','%s','%s','%s','%f','%s')",\
					accountid, user, pwd, dealtpye , balance, remark);
	return ExecuteCmd(command);
}

//删除账户
BOOL CAdoConnect::DlelteAccount(CString &accountname)
{
	CString			command;
	command.Format("delete tb_account where name ='%s'",accountname);
	return ExecuteCmd(command);
}
//增加参数
BOOL CAdoConnect::AddParameter(CString &mart_type, CString &stock_spec_code ,CString &stock_for_short, double &poundage, double &least_poundage, double &stamp_tax, double &transferrate, double &least_transferred, double &commission)
{
	CString			command;
	command.Format("insert into tb_stock_base_info values('%s','%s','%s','%.2f','%.2f','%.2f','%.2f','%.2f','%.2f')",\
		mart_type, stock_spec_code, stock_for_short, poundage, least_poundage, stamp_tax,transferrate, least_transferred, commission);
	return ExecuteCmd(command);
}

//设置当前用户的账户金额,主要用于追加资金或者划出资金
BOOL CAdoConnect::AmendAccountMoney(CString &user, double &money)
{
	CString			command;
	command.Format("update tb_account set balance='%.2f' where name='%s'",money, user);
	return ExecuteCmd(command);
}
//把股票的信息写入数据库TB_SUMMARYLIST
BOOL CAdoConnect::AddStockInfoToTB_SUMMARYLIST(CString &account_id, CString &stock_code, CString &stock_name, 
								  double &new_price, double &cose_price, int &stock_amount, double &mart_value, double &profit_lost)
{
	CTime				time;
	CString				sum_id;
	time = CTime::GetCurrentTime();
	sum_id = time.Format("%Y%M%D%H%M%S");
	CString			command;
	command.Format("insert into tb_summarylist values('%s','%s','%s','%s','%.2f','%.2f','%d','%.2f','%.2f')",sum_id,account_id,stock_code,stock_name,new_price,cose_price,stock_amount,mart_value,profit_lost);
	return ExecuteCmd(command);	
	
	return true;
}
//把交易信息写入数据库TB_DEALDETAIL
BOOL CAdoConnect::AddStockInfoToTB_DEALDETAIL(CString &deal_type,CString &deal_date, double &unit_type,
		int &amount, double &deal_money, double &fare, double &profit_loss, double &stock_amount)
{

	return true;
}

⌨️ 快捷键说明

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