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

📄 customeradd.cpp

📁 本人工作中的一个软件开发实例。里面包含了数据库
💻 CPP
字号:
// CustomerAdd.cpp : implementation file
//

#include "stdafx.h"
#include "OIL.h"
#include "CustomerAdd.h"

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


extern  COILApp				theApp;

/////////////////////////////////////////////////////////////////////////////
// CCustomerAdd dialog


CCustomerAdd::CCustomerAdd(CWnd* pParent /*=NULL*/)
	: CDialog(CCustomerAdd::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCustomerAdd)
	m_strAddress = _T("");
	m_strCustomerID = _T("");
	m_strCustomerName = _T("");
	m_strCustomerShort = _T("");
	m_strCustomerShortEnglish = _T("");
	m_strHandler = _T("");
	m_strNotes = _T("");
	m_strTelephone = _T("");
	//}}AFX_DATA_INIT
}


void CCustomerAdd::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCustomerAdd)
	DDX_Text(pDX, IDC_ADDRESS, m_strAddress);
	DDX_Text(pDX, IDC_CUSTOMERID, m_strCustomerID);
	DDX_Text(pDX, IDC_CUSTOMERNAME, m_strCustomerName);
	DDX_Text(pDX, IDC_CUSTOMERSHORT, m_strCustomerShort);
	DDX_Text(pDX, IDC_CUSTOMERSHORTENGLISH, m_strCustomerShortEnglish);
	DDX_Text(pDX, IDC_HANDLE, m_strHandler);
	DDX_Text(pDX, IDC_NOTES, m_strNotes);
	DDX_Text(pDX, IDC_TELEPHONE, m_strTelephone);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCustomerAdd, CDialog)
	//{{AFX_MSG_MAP(CCustomerAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCustomerAdd message handlers

void CCustomerAdd::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();

	//检查数据完整性
	if(m_strCustomerName.IsEmpty())
	{
		AfxMessageBox("客户名称不能为空!");
		GetDlgItem(IDC_CUSTOMERNAME)->SetFocus();
		return;
	}
    if( m_strCustomerShort.IsEmpty())
	{
		AfxMessageBox("客户名简称不能为空!");
        GetDlgItem(IDC_CUSTOMERSHORT)->SetFocus();
		return;
	}

	//根据实际情况插入或修改数据
	CString		sql;
	if(m_nDoWhat==ADD)    
	{
      	//生成SQL语句
		sql="insert into \
		     customer(CustomerID,CustomerShort,CustomerShortEnglish,CustomerName,Address,Telephone,Handler,Notes) \
			 values  ('"+m_strCustomerID+"','"+m_strCustomerShort+"','"+m_strCustomerShortEnglish+"','"+m_strCustomerName+"','"+m_strAddress+"','"+m_strTelephone+"','"+m_strHandler+"','"+m_strNotes+"')";

		//执行语句
		_variant_t RecordsAffected;
		theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
	}

    //修改客户资料
	if(m_nDoWhat==2)    
	{    
		//生成动态sql语句 Address,Telephone,Handler,Notes
		sql="update Customer set CustomerName='"+m_strCustomerName+
			"',CustomerShortEnglish='"+m_strCustomerShortEnglish+
			"',CustomerShort='"+m_strCustomerShort+
			"',Address='"+m_strAddress+
			"',Telephone='"+m_strTelephone+
			"',Handler='"+m_strHandler+
			"',Notes='"+m_strNotes+
			"' where CustomerID='"+m_strCustomerID+"' ";

		//执行语句
		_variant_t RecordsAffected;
		theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
	}

	//刷新list列表的内容
	if(m_pParent!=NULL)
	{
		CString sql="SELECT * FROM customer";  
		ReadtoListCtrl((CListCtrl *)m_pParent->GetDlgItem(IDC_LIST_CUSTOMER),sql);
	}

	CDialog::OnOK();	
}

BOOL CCustomerAdd::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//禁示修改客户ID
	//GetDlgItem(IDC_CUSTOMERID)->EnableWindow(false);

	HWND Hwnd=::GetDlgItem(this->GetSafeHwnd(),IDC_CUSTOMERID);
	::EnableWindow(Hwnd,false);

	//设置相应的标题
	switch(m_nDoWhat)
	{
	case 1:
		{
		SetWindowText("增加客户资料");
		SetDlgItemText(IDOK,"增加");
		CString sql="SELECT max(customerID)+1 FROM customer";
        SetDlgItemText(IDC_CUSTOMERID,GetAutoID(sql,4));
		break;
		}
	case 2:
		SetWindowText("修改客户资料");
		SetDlgItemText(IDOK,"修改");
		break;
	case 3:
        break;
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCustomerAdd::OnCancel() 
{
	// TODO: Add extra cleanup here	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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