📄 addcategdlg.cpp
字号:
// AddCategDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyMFCODBCSmpl.h"
#include "AddCategDlg.h"
#include "MainFrm.h"
#include "CategorySet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAddCategDlg dialog
CAddCategDlg::CAddCategDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAddCategDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAddCategDlg)
m_strName = _T("");
m_strNote = _T("");
//}}AFX_DATA_INIT
}
void CAddCategDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAddCategDlg)
DDX_Text(pDX, IDC_CATEG_NAME, m_strName);
DDX_Text(pDX, IDC_CATEG_NOTE, m_strNote);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAddCategDlg, CDialog)
//{{AFX_MSG_MAP(CAddCategDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddCategDlg message handlers
void CAddCategDlg::OnOK()
{
UpdateData();
int nCategID = WriteData();
if( nCategID < 0 ) return;
//插入一个结点到左面视图树上
CMainFrame * pMain = (CMainFrame *)AfxGetMainWnd();
CTreeView * pTree = (CTreeView *)pMain->m_wndSplitter.GetPane(0,0);
CTreeCtrl & tc = pTree->GetTreeCtrl();
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent = NULL;
tvinsert.hInsertAfter = TVI_LAST;
tvinsert.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE| TVIF_TEXT;
tvinsert.item.hItem = NULL;
tvinsert.item.state = 0;
tvinsert.item.stateMask = 0;
tvinsert.item.cchTextMax = 6;
tvinsert.item.iSelectedImage = 0;
tvinsert.item.cChildren = 0;
tvinsert.item.lParam = 0;
tvinsert.item.pszText = m_strName.GetBuffer(m_strName.GetLength());
tvinsert.item.iImage = 0;
tvinsert.item.iSelectedImage = 0;
tvinsert.hParent = NULL;
HTREEITEM hTop = tc.InsertItem(&tvinsert);
//设置ItemData,产品分类为*10+0
tc.SetItemData(hTop,nCategID*10+0);
UpdateWindow();
CDialog::OnOK();
}
int CAddCategDlg::WriteData()
{
int nRet = -1;
if( m_strName.IsEmpty() )
{
AfxMessageBox("产品分类名称不能为空!");
return nRet;
}
CDatabase db;
//打开数据库
if( !db.Open(_T("MyNWind")) )
{
AfxMessageBox("数据源连接失败!");
return nRet;
}
//测试该驱动程序是否支持事务处理
if(!db.CanTransact())
return nRet;
//开始处理事务
if(!db.BeginTrans())
return nRet;
TRY
{
CString strSql = "Insert into Categories(CategoryName,Description) values(";
CString strTemp;
strTemp = m_strName;
strSql += "'";
strSql += strTemp;
strSql += "'";
strSql += ",";
strSql += "'";
strTemp = m_strNote;
strSql += strTemp;
strSql += "'";
strSql += ")";
db.ExecuteSQL(strSql);
db.CommitTrans();
}
CATCH (CDBException,e)
{
//若异常则回滚事务
db.Rollback();
AfxMessageBox(e->m_strError);
e->Delete();
return nRet;
}
END_CATCH;
//得到刚才新加入记录的CategoryID值
CCategorySet * pSet = new CCategorySet(&db);
if( !pSet->IsOpen() )pSet->Open();
pSet->m_strSort = "ASC CategoryID";
pSet->MoveLast();
nRet = pSet->m_CategoryID;
pSet->Close();
delete pSet;
db.Close();
return nRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -