📄 productinfo.cpp
字号:
// ProductInfo.cpp : implementation file
//
#include "stdafx.h"
#include "Store.h"
#include "ProductInfo.h"
#include "Affirm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CProductInfo dialog
CProductInfo::CProductInfo(CWnd* pParent /*=NULL*/)
: CDialog(CProductInfo::IDD, pParent)
{
//{{AFX_DATA_INIT(CProductInfo)
m_productID = _T("");
m_productName = _T("");
m_standard = _T("");
m_model = _T("");
m_price = 0.0f;
m_qualityGuarantee = 0;
m_maintance = 0;
m_unit = _T("");
m_colorType = _T("");
m_qualityLevel = _T("");
m_finishRate = 0;
m_taxRate = 0.0f;
m_storeHouse = _T("");
m_remark = _T("");
m_numMax = 0;
m_numMin = 0;
//}}AFX_DATA_INIT
}
void CProductInfo::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProductInfo)
DDX_Control(pDX, IDC_storeHouse, m_storeHouseCombo);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_productID, m_productID);
DDX_Text(pDX, IDC_productName, m_productName);
DDX_Text(pDX, IDC_standard, m_standard);
DDX_Text(pDX, IDC_model, m_model);
DDX_Text(pDX, IDC_price, m_price);
DDX_Text(pDX, IDC_qualityGuarantee, m_qualityGuarantee);
DDX_Text(pDX, IDC_maintance, m_maintance);
DDX_CBString(pDX, IDC_unit, m_unit);
DDX_CBString(pDX, IDC_colorType, m_colorType);
DDX_CBString(pDX, IDC_qualityLevel, m_qualityLevel);
DDX_Text(pDX, IDC_finishRate, m_finishRate);
DDX_Text(pDX, IDC_taxRate, m_taxRate);
DDX_CBString(pDX, IDC_storeHouse, m_storeHouse);
DDX_Text(pDX, IDC_remark, m_remark);
DDX_Text(pDX, IDC_numMax, m_numMax);
DDX_Text(pDX, IDC_numMin, m_numMin);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProductInfo, CDialog)
//{{AFX_MSG_MAP(CProductInfo)
ON_BN_CLICKED(IDC_exit, OnExit)
ON_BN_CLICKED(IDC_add, OnAdd)
ON_BN_CLICKED(IDC_delete, OnDelete)
ON_BN_CLICKED(IDC_modify, OnModify)
ON_BN_CLICKED(IDC_save, OnSave)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProductInfo message handlers
void CProductInfo::OnExit()
{
this->OnCancel();
}
void CProductInfo::OnAdd()
{
UpdateData(true);//对话框数据更新到变量
//获取新增加的产品的编号
int productID=((CStoreApp*)AfxGetApp())->m_pIDRecordset->GetCollect("productID").intVal;
if(productID<10) //为产品编号设定格式
m_productID.Format("product0000%d",productID);
else if(productID<100&&productID>9)
m_productID.Format("product000%d",productID);
else if(productID<1000&&productID>99)
m_productID.Format("product00%d",productID);
CString str;
str.Format("%d",productID+1); //更新下一个可用的产品编号
((CStoreApp*)AfxGetApp())->m_pIDRecordset->PutCollect("productID",_variant_t(str));
((CStoreApp*)AfxGetApp())->m_pIDRecordset->Update();
//为对话框控件的变量设定默认值
m_productName = _T("");
m_standard = _T("");
m_model = _T("");
m_price = 0.0f;
m_qualityGuarantee = 0;
m_maintance = 0;
m_unit = _T("");
m_colorType = _T("");
m_qualityLevel = _T("");
m_finishRate = 0;
m_taxRate = 0.0f;
m_storeHouse = _T("");
m_remark = _T("");
m_numMin=0;
m_numMax=0;
flag=1; //添加
//设定除产品编号以外的其他编辑控件均可用
tx_productID->EnableWindow(false);
tx_productName->EnableWindow(true);
tx_standard->EnableWindow(true);
tx_model->EnableWindow(true);
tx_price->EnableWindow(true);
tx_qualityGuarantee->EnableWindow(true);
tx_maintance->EnableWindow(true);
tx_unit->EnableWindow(true);
tx_colorType->EnableWindow(true);
tx_qualityLevel->EnableWindow(true);
tx_finishRate->EnableWindow(true);
tx_taxRate->EnableWindow(true);
tx_storeHouse->EnableWindow(true);
tx_remark->EnableWindow(true);
tx_numMax->EnableWindow(true);
tx_numMin->EnableWindow(true);
//设定按钮的可用性
bt_save->EnableWindow(true); //保存按钮可用
bt_add->EnableWindow(false); //添加按钮不可用
bt_modify->EnableWindow(false); //修改按钮不可用
bt_delete->EnableWindow(false); //删除按钮不可用
UpdateData(false);//将数据更新到对话框
}
void CProductInfo::OnDelete()
{
CAffirm dlg;
if(dlg.DoModal()!=IDOK)//显示确认删除对话框
{
return;
}
_variant_t RecordsAffected;
CString strSQL;
//构造删除记录的sql语句
strSQL="delete from product where productID='";
strSQL=strSQL+m_productID+"'";
//执行sql语句,删除记录
(((CStoreApp*)AfxGetApp())->m_pConn)->Execute((_bstr_t)strSQL,&RecordsAffected,adCmdText);
POSITION pos = m_list.GetFirstSelectedItemPosition();//获取删除的记录的位置
if(pos)
{
int nFirstSelItem = m_list.GetNextSelectedItem(pos);//获取删除的记录对应的条目
m_list.DeleteItem(nFirstSelItem);//将该条目删除
}
bt_delete->EnableWindow(false); //删除按钮不可用
//为控件关联变量赋默认值
m_productID = _T("");
m_productName = _T("");
m_standard = _T("");
m_model = _T("");
m_price = 0.0f;
m_qualityGuarantee = 0;
m_maintance = 0;
m_unit = _T("");
m_colorType = _T("");
m_qualityLevel = _T("");
m_finishRate = 0;
m_taxRate = 0.0f;
m_storeHouse = _T("");
m_remark = _T("");
m_numMax=0;
m_numMin=0;
Refresh();
UpdateData(false); //更新对话框数据
}
void CProductInfo::OnModify()
{
//设定除产品编号以外的其他编辑控件均可用
tx_productID->EnableWindow(false);
tx_productName->EnableWindow(true);
tx_standard->EnableWindow(true);
tx_model->EnableWindow(true);
tx_price->EnableWindow(true);
tx_qualityGuarantee->EnableWindow(true);
tx_maintance->EnableWindow(true);
tx_unit->EnableWindow(true);
tx_colorType->EnableWindow(true);
tx_qualityLevel->EnableWindow(true);
tx_finishRate->EnableWindow(true);
tx_taxRate->EnableWindow(true);
tx_storeHouse->EnableWindow(true);
tx_remark->EnableWindow(true);
tx_numMax->EnableWindow(true);
tx_numMin->EnableWindow(true);
flag=2;//修改
bt_save->EnableWindow(true);//保存按钮可用
}
void CProductInfo::OnSave()
{
UpdateData(true);//将控件中的值更新到变量
_RecordsetPtr m_pRecordset;
CString strSQL;
if(flag==1) //添加
{
strSQL="SELECT * FROM product" ;//构造sql语句
try
{
HRESULT hTRes;
hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
if (SUCCEEDED(hTRes))
{
hTRes = m_pRecordset->Open((LPTSTR)strSQL.GetBuffer(130),
((CStoreApp*)AfxGetApp())->m_pConn.GetInterfacePtr(),
adOpenDynamic,adLockPessimistic,adCmdText);//打开查询结果记录集
if(SUCCEEDED(hTRes))
{
TRACE(_T("连接成功!\n"));
CString str;
m_pRecordset->AddNew();//添加新的记录
m_pRecordset->PutCollect("productID",_variant_t(m_productID));//产品编号
m_pRecordset->PutCollect("productName",_variant_t(m_productName));//产品名称
m_pRecordset->PutCollect("standard",_variant_t(m_standard)); //规格
m_pRecordset->PutCollect("model",_variant_t(m_model)); //型号
str.Format("%f",m_price); //单价
m_pRecordset->PutCollect("price",_variant_t(str));
str.Format("%d",m_qualityGuarantee);//保质期
m_pRecordset->PutCollect("qualityGuarantee",_variant_t(str));
str.Format("%d",m_maintance);//维护期
m_pRecordset->PutCollect("maintance",_variant_t(str));
m_pRecordset->PutCollect("unit",_variant_t(m_unit));//计量单位
m_pRecordset->PutCollect("colorType",_variant_t(m_colorType));//颜色类别
m_pRecordset->PutCollect("qualityLevel",_variant_t(m_qualityLevel));//质量等级
str.Format("%f",m_finishRate); //完成率
m_pRecordset->PutCollect("finishRate",_variant_t(str));
m_pRecordset->PutCollect("storeHouse",_variant_t(m_storeHouse));//存放仓库
str.Format("%f",m_taxRate);//税率
m_pRecordset->PutCollect("taxRate",_variant_t(str));
str.Format("%d",m_numMax);//库存上限
m_pRecordset->PutCollect("numMax",_variant_t(str));
str.Format("%d",m_numMin);//库存下限
m_pRecordset->PutCollect("numMin",_variant_t(str));
m_pRecordset->PutCollect("remark",_variant_t(m_remark)); //备注信息
m_pRecordset->Update(); //更新数据库
int j;
j=m_list.GetItemCount(); //获取要插入的条目的位置
m_list.InsertItem(j,m_productID); //产品编号
m_list.SetItemText(j,1,m_productName); //产品名称
m_list.SetItemText(j,2,m_standard);//规格
m_list.SetItemText(j,3,m_model); //型号
str.Format("%f",m_price); //单价
m_list.SetItemText(j,4,str);
str.Format("%d",m_qualityGuarantee);//保质期
m_list.SetItemText(j,5,str);
str.Format("%d",m_maintance);//维护期
m_list.SetItemText(j,6,str);
m_list.SetItemText(j,7,m_unit);//计量单位
m_list.SetItemText(j,8,m_colorType);//颜色类别
m_list.SetItemText(j,9,m_qualityLevel);//质量等级
str.Format("%d",m_finishRate); //成品率
m_list.SetItemText(j,10,str);
m_list.SetItemText(j,11,m_storeHouse);//存放仓库
str.Format("%d",m_taxRate);//税率
m_list.SetItemText(j,12,str);
str.Format("%d",m_numMax);//库存上限
m_list.SetItemText(j,13,str);
str.Format("%d",m_numMin);//库存下限
m_list.SetItemText(j,14,str);
m_list.SetItemText(j,15,m_remark); //备注
AfxMessageBox("插入成功!"); //提示信息
}
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
MessageBox("创建记录集失败!","错误");
}
}
else if(flag==2) //修改
{
strSQL="SELECT * FROM product where productID='" ;
strSQL=strSQL+m_productID+"'";//构造查询指定产品编号的产品的信息的sql语句
try
{
HRESULT hTRes;
hTRes = m_pRecordset.CreateInstance(_T("ADODB.Recordset"));
if (SUCCEEDED(hTRes))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -