📄 product.cpp
字号:
// Product.cpp: implementation of the CProduct class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Stock.h"
#include "Product.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CProduct::CProduct()
{
Pid = 0;
Pname = "";
TypeId = 0;
Pstyle = "";
Punit = "";
Pprice = 0;
Plow = 0;
Phigh = 0;
Valid = 0;
AlarmDays = 0;
}
CProduct::~CProduct()
{
}
//设置和读取成员变量
int CProduct::GetPid()
{
return Pid;
}
void CProduct::SetPid(int iPid)
{
Pid = iPid;
}
CString CProduct::GetPname()
{
return Pname;
}
void CProduct::SetPname(CString cPname)
{
Pname = cPname;
}
int CProduct::GetTypeId()
{
return TypeId;
}
void CProduct::SetTypeId(int iTypeId)
{
TypeId = iTypeId;
}
CString CProduct::GetPstyle()
{
return Pstyle;
}
void CProduct::SetPstyle(CString cPstyle)
{
Pstyle = cPstyle;
}
CString CProduct::GetPunit()
{
return Punit;
}
void CProduct::SetPunit(CString cPunit)
{
Punit = cPunit;
}
float CProduct::GetPprice()
{
return Pprice;
}
void CProduct::SetPprice(float fPprice)
{
Pprice = fPprice;
}
int CProduct::GetPlow()
{
return Plow;
}
void CProduct::SetPlow(int iPlow)
{
Plow = iPlow;
}
int CProduct::GetPhigh()
{
return Phigh;
}
void CProduct::SetPhigh(int iPhigh)
{
Phigh = iPhigh;
}
int CProduct::GetValid()
{
return Valid;
}
void CProduct::SetValid(int iValid)
{
Valid = iValid;
}
int CProduct::GetAlarmDays()
{
return AlarmDays;
}
void CProduct::SetAlarmDays(int iAlarmDays)
{
AlarmDays = iAlarmDays;
}
//数据库操作
int CProduct::HaveName(CString cPname)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Product WHERE Pname='" + cPname + "'";
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//返回各列的值
if (m_pRecordset->adoEOF)
return -1;
else
return 1;
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
int CProduct::HaveType(CString cTypeId)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Product WHERE TypeId=" + cTypeId;
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//返回各列的值
if (m_pRecordset->adoEOF)
return -1;
else
return 1;
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
void CProduct::sql_insert()
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置INSERT语句
CString strTypeId;
strTypeId.Format("%d", TypeId);
CString strPrice;
strPrice.Format("%f", Pprice);
CString strPlow;
strPlow.Format("%d", Plow);
CString strPhigh;
strPhigh.Format("%d", Phigh);
CString strValid;
strValid.Format("%d", Valid);
CString strAlarm;
strAlarm.Format("%d", AlarmDays);
_bstr_t vSQL;
vSQL = "INSERT INTO Product (Pname, TypeId, Pstyle, Punit, Pprice, Plow, Phigh, Valid, AlarmDays) VALUES('"
+ Pname + "'," + strTypeId + ",'" + Pstyle + "','" + Punit + "'," + strPrice + "," + strPlow + ","
+ strPhigh + "," + strValid + "," + strAlarm + ")";
//执行INSERT语句
m_AdoConn.ExecuteSQL(vSQL);
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
void CProduct::sql_update(CString cPid)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置UPDATE语句
CString strTypeId;
strTypeId.Format("%d", TypeId);
CString strPrice;
strPrice.Format("%f", Pprice);
CString strPlow;
strPlow.Format("%d", Plow);
CString strPhigh;
strPhigh.Format("%d", Phigh);
CString strValid;
strValid.Format("%d", Valid);
CString strAlarm;
strAlarm.Format("%d", AlarmDays);
_bstr_t vSQL;
vSQL = "UPDATE Product SET Pname='" + Pname + "', Pstyle='"
+ Pstyle + "', Punit='" + Punit + "', Pprice="
+ strPrice + ", Plow=" + strPlow + ", Phigh=" + strPhigh
+ ", Valid=" + strValid + ", AlarmDays=" + strAlarm
+ " WHERE Pid=" + cPid;
//执行UPDATE语句
m_AdoConn.ExecuteSQL(vSQL);
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
void CProduct::sql_delete(CString cPid)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置DELETE语句
_bstr_t vSQL;
vSQL = "DELETE FROM Product WHERE Pid=" + cPid;
//执行DELETE语句
m_AdoConn.ExecuteSQL(vSQL);
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
//读取所有字段值
void CProduct::GetData(CString cPid)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Product WHERE Pid=" + cPid;
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//返回各列的值
if (m_pRecordset->adoEOF)
CProduct();
else
{
Pid = atoi(cPid);
Pname = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Pname");
TypeId = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TypeId"));
Pstyle = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Pstyle");
Punit = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Punit");
Pprice = atof((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Pprice"));
Plow = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Plow"));
Phigh = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Phigh"));
Valid = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Valid"));
AlarmDays = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("AlarmDays"));
}
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -