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

📄 addcustomer.cpp

📁 自已做的简单的库存管理软件。包含员工信息管理等
💻 CPP
字号:
// AddCustomer.cpp : 实现文件
//

#include "stdafx.h"
#include "AddCustomer.h"
#include "myhead.h"

extern _ConnectionPtr pMyConnect;
// CAddCustomer 对话框

IMPLEMENT_DYNAMIC(CAddCustomer, CDialog)

CAddCustomer::CAddCustomer(CWnd* pParent /*=NULL*/)
	: CDialog(CAddCustomer::IDD, pParent)
{

}

CAddCustomer::~CAddCustomer()
{
}

void CAddCustomer::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAddCustomer, CDialog)
	ON_CBN_SELCHANGE(IDC_COMBO_PROVINCE, &CAddCustomer::OnCbnSelchangeComboProvince)
	ON_CBN_SELCHANGE(IDC_COMBO_STAFFDEPART, &CAddCustomer::OnCbnSelchangeComboStaffdepart)
	ON_BN_CLICKED(IDOK, &CAddCustomer::OnBnClickedOk)
END_MESSAGE_MAP()


// CAddCustomer 消息处理程序

BOOL CAddCustomer::OnInitDialog()
{
	CDialog::OnInitDialog();


	QueryProvince();

	QueryDepart();

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CAddCustomer::QueryProvince()
{
		
	CComboBox* pWnd;
	pWnd = (CComboBox*)GetDlgItem(IDC_COMBO_PROVINCE);
	pWnd->ResetContent();

	_RecordsetPtr m_pRs; 
	CString m_strSql; 

	int number = 0;

	
	m_strSql.Format(_T("select ProvinceID,ProvinceName from S_Province"));

	m_pRs.CreateInstance( __uuidof(Recordset) ); 
	m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);  
			
	try
	{
		if ( m_pRs->GetRecordCount() > 0 )
		{
			
			_variant_t   TheValue;
			_bstr_t tmp;
			
			m_pRs->MoveFirst(); 
			while(m_pRs->adoEOF==VARIANT_FALSE)	
			{
				//Retrieve column's value: 	 
				
				CString str_provinceid;
				CString str_provincename;
				TheValue = m_pRs->Fields->GetItem(_variant_t("ProvinceID"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 
					CString str_tmp1 = tmp;
					int iProvinceID =  _wtoi(str_tmp1); 		
					str_provinceid.Format( _T("%02d"), iProvinceID);
				}
				
				
				
				TheValue = m_pRs->Fields->GetItem(_variant_t("ProvinceName"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 
					CString str_tmp2 = tmp;				
					str_provincename = str_provinceid + str_tmp2;
				}
				
				pWnd->InsertString(number,str_provincename);				
				
				
				number++;
				
				//Do something what you want to do:	......
				m_pRs->MoveNext(); 
			}		
		}
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		
	}
	m_pRs->Close(); 

}


void CAddCustomer::QueryDepart()
{
		
	int number = 0;

	_RecordsetPtr m_pRs; 
	CString m_strSql; 

	CComboBox* pWnd;
	pWnd = (CComboBox*)GetDlgItem(IDC_COMBO_STAFFDEPART);
	pWnd->ResetContent();

		
	m_strSql.Format(_T("select DEPART from DEPART_TABLE where ( SN > 0)") ); 
	m_pRs.CreateInstance( __uuidof(Recordset) ); 
	m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);  
		
	try
	{	
		if ( m_pRs->GetRecordCount() > 0 )
		{
			_variant_t   TheValue;
			_bstr_t tmp;
			
			m_pRs->MoveFirst(); 
			while(m_pRs->adoEOF==VARIANT_FALSE)	
			{
				//Retrieve column's value: 	 									
				
				TheValue = m_pRs->Fields->GetItem(_variant_t("DEPART"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 						
					CString str_tmp1 = tmp;
					pWnd->InsertString(number,str_tmp1);
					
					number++;
				}
	
				//Do something what you want to do:	......
				m_pRs->MoveNext(); 
			}		
		}
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		
	}
	m_pRs->Close();  

}




void CAddCustomer::OnCbnSelchangeComboProvince()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str_province;
	CComboBox* pTemp;
	pTemp = (CComboBox*)GetDlgItem(IDC_COMBO_PROVINCE);
	int iPos=pTemp ->GetCurSel();//当前选中的行。	
	int n = pTemp->GetLBTextLen( iPos );
	pTemp->GetLBText( iPos, str_province.GetBuffer(n) );
	str_province.ReleaseBuffer();
	CString str_provinceid = str_province.Left(PROVINCEID_LENGTH);
	
	int number = 0;
	_RecordsetPtr m_pRs; 
	CString m_strSql; 

	CComboBox* pWnd;
	pWnd = (CComboBox*)GetDlgItem(IDC_COMBO_CITY);
	pWnd->ResetContent();

	
	m_strSql.Format(_T("select CityID,CityName from S_City where ( ProvinceID='") +str_provinceid+  _T("')")); 
	m_pRs.CreateInstance( __uuidof(Recordset) ); 
	m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);  
		
	
	try
	{	
		if ( m_pRs->GetRecordCount() > 0 )
		{
			_variant_t   TheValue;
			_bstr_t tmp;
		
			m_pRs->MoveFirst(); 
			while(m_pRs->adoEOF==VARIANT_FALSE)	
			{
				//Retrieve column's value: 	 									
				CString str_cityid;
				CString str_cityname;

				TheValue = m_pRs->Fields->GetItem(_variant_t("CityID"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 					
					CString str_tmp1 = tmp;
					int iCityID =  _wtoi(str_tmp1);
					str_cityid.Format( _T("%03d"), iCityID);
				}


				TheValue = m_pRs->Fields->GetItem(_variant_t("CityName"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 
					CString str_tmp2 = tmp;
					str_cityname = str_cityid + str_tmp2;
				}

				pWnd->InsertString(number,str_cityname);


				number++;

				//Do something what you want to do:	......
				m_pRs->MoveNext(); 
			}	
		}
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		
	}
	m_pRs->Close(); 
}

void CAddCustomer::OnCbnSelchangeComboStaffdepart()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str_depart;
	CComboBox* pTemp;
	pTemp = (CComboBox*)GetDlgItem(IDC_COMBO_STAFFDEPART);
	int iPos=pTemp ->GetCurSel();//当前选中的行。	
	int n = pTemp->GetLBTextLen( iPos );
	pTemp->GetLBText( iPos, str_depart.GetBuffer(n) );
	str_depart.ReleaseBuffer();

	
	CComboBox* pWnd;
	pWnd = (CComboBox*)GetDlgItem(IDC_COMBO_STAFFNAME);
	pWnd->ResetContent();

	_RecordsetPtr m_pRs; 
	CString m_strSql; 

	int number = 0;

	
	m_strSql.Format(_T("select STAFF_ID,NAME from STAFF_TABLE where ( DEPART='") +str_depart+  _T("')")); 

	m_pRs.CreateInstance( __uuidof(Recordset) ); 
	m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);  
			
	try
	{	
		if ( m_pRs->GetRecordCount() > 0 )
		{
			_variant_t   TheValue;
			_bstr_t tmp;
		
			m_pRs->MoveFirst(); 
			while(m_pRs->adoEOF==VARIANT_FALSE)	
			{
				//Retrieve column's value: 	 									
				CString str_staffid;
				CString str_name;

				TheValue = m_pRs->Fields->GetItem(_variant_t("STAFF_ID"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 
					CString str_tmp1 = tmp;	
					str_staffid = str_tmp1;
				}


				TheValue = m_pRs->Fields->GetItem(_variant_t("NAME"))->Value;
				if( TheValue.vt != VT_NULL )
				{
					tmp=(_bstr_t)TheValue; 
					CString str_tmp2 = tmp;
					str_name = str_tmp2;
				}

				CString str_staffname;
				str_staffname = str_staffid + str_name;
			
				pWnd->InsertString(number,str_staffname);				


				number++;

				//Do something what you want to do:	......
				m_pRs->MoveNext(); 
			}
		}
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);
		
	}
	m_pRs->Close(); 
}

void CAddCustomer::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str_name;
	CString str_province;
	CString str_city;
	CString str_depart;
	CString str_staff;
	CString str_website;
	CString str_caddr;
	CString str_note;
	CString str_temp;


	CWnd* pWnd;
		
	

	pWnd = GetDlgItem(IDC_EDIT_NAME);
	pWnd->GetWindowText(str_name);
	if ( str_name.GetLength() == 0 )
	{
		AfxMessageBox(_T("客户名称不能为空!"));
		return;
	}
	if ( str_name.GetLength() > 50 )
	{
		AfxMessageBox(_T("客户名称长度超出限制!"));
		return;
	}
	
	_RecordsetPtr m_pRs; 
	CString m_strSql; 

	bool bHad = FALSE;
	m_strSql.Format(_T("select SN from CUSTOMER_TABLE where ( NAME='") +str_name+ _T("')") ); 
	m_pRs.CreateInstance( __uuidof(Recordset) ); 
	m_pRs->Open( _bstr_t(m_strSql), _variant_t((IDispatch *)pMyConnect,true), adOpenKeyset, adLockOptimistic, adCmdText);  
		
	try
	{
		if ( m_pRs->GetRecordCount() > 0 )
		{
			_variant_t   TheValue;
			m_pRs->MoveFirst(); 
			while(m_pRs->adoEOF==VARIANT_FALSE)	
			{
				//Retrieve column's value: 	   
			
				TheValue = m_pRs->Fields->GetItem(_variant_t("SN"))->Value;
			
				if(TheValue.vt   != VT_NULL)
				{
					_bstr_t s1=(_bstr_t)TheValue; 
					
					CString sn = s1;
					bHad = TRUE;
				}
			
				//Do something what you want to do:	......
				m_pRs->MoveNext(); 
			}		
		}
	}
	catch (_com_error &e)
	{
		::MessageBox(NULL,e.Description(),(LPCTSTR)(_T("警告")),MB_OK);

	}
	m_pRs->Close(); 
	
	if ( bHad )
	{
		AfxMessageBox(_T("此客户已存在!"));
		return;
	}


		

	pWnd = GetDlgItem(IDC_COMBO_PROVINCE);
	pWnd->GetWindowText(str_province);		
	if ( str_province.GetLength() == 0 )
	{
		AfxMessageBox(_T("请选择省份!"));
		return;
	}


	pWnd = GetDlgItem(IDC_COMBO_CITY);
	pWnd->GetWindowText(str_temp);		
	if ( str_temp.GetLength() == 0 )
	{
		AfxMessageBox(_T("请选择城市!"));
		return;
	}
	str_city = str_temp.Left(CITYID_LENGTH );
	int iCityID =  _wtoi(str_city); 	
	str_city.Format( _T("%d"), iCityID);
	

	pWnd = GetDlgItem(IDC_COMBO_STAFFDEPART);
	pWnd->GetWindowText(str_depart);		
	if ( str_depart.GetLength() == 0 )
	{
		AfxMessageBox(_T("请选择负责部门!"));
		return;
	}

	pWnd = GetDlgItem(IDC_COMBO_STAFFNAME);
	pWnd->GetWindowText(str_temp);		
	if ( str_temp.GetLength() == 0 )
	{
		AfxMessageBox(_T("请选择负责人!"));
		return;
	}
	str_staff = str_temp.Left(STAFFID_LENGTH);
	

	pWnd = GetDlgItem(IDC_EDIT_WEBSITE);
	pWnd->GetWindowText(str_website);
	if ( str_website.GetLength() == 0 )
	{
		AfxMessageBox(_T("客户网址不能为空!"));
		return;
	}
	if ( str_website.GetLength() > 50 )
	{
		AfxMessageBox(_T("客户网址长度超出限制!"));
		return;
	}

	pWnd = GetDlgItem(IDC_EDIT_CADDR);
	pWnd->GetWindowText(str_caddr);
	if ( str_caddr.GetLength() == 0 )
	{
		AfxMessageBox(_T("客户地址不能为空!"));
		return;
	}
	if ( str_caddr.GetLength() > 50 )
	{
		AfxMessageBox(_T("客户地址长度超出限制!"));
		return;
	}

	pWnd = GetDlgItem(IDC_EDIT_NOTE);
	pWnd->GetWindowText(str_note);


	//goto the last page, and then add a item
	//CString m_strSql; 
	//_RecordsetPtr m_pRecordset;
	_variant_t RecordsAffected;

	m_strSql.Format(_T("INSERT INTO CUSTOMER_TABLE(NAME,CITY_ID,STAFF_ID,WEBSITE,ADDR,NOTE) VALUES ('") 
		+str_name+ _T("','") +str_city+ _T("','") +str_staff+ _T("','") +str_website+  _T("','") +str_caddr+  _T("','") +str_note+ _T("')"));
		
	bool bSuccess = pMyConnect->Execute(_bstr_t(m_strSql),&RecordsAffected,adCmdText);
	if( !bSuccess )
	{
		AfxMessageBox(_T("增加客户资料失败,请重启系统!"));
		return;
	}
	AfxMessageBox(_T("增加客户资料成功!"));


	OnOK();
}

⌨️ 快捷键说明

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