www.pudn.com > odbc_class.rar > demo1Dlg.cpp


// demo1Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "demo1.h" 
#include "demo1Dlg.h" 
 
#include "MyODBC.h" 
#include "odbcset.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo1Dlg dialog 
 
CDemo1Dlg::CDemo1Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CDemo1Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDemo1Dlg) 
	m_strID = _T(""); 
	m_strJob = _T(""); 
	m_strName = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	ImageList.Create(16,16,ILC_COLOR8,0,5); 
	ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); 
} 
 
void CDemo1Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDemo1Dlg) 
	DDX_Control(pDX, IDC_LIST1, m_list); 
	DDX_Text(pDX, IDC_EDITID, m_strID); 
	DDX_Text(pDX, IDC_EDITJOB, m_strJob); 
	DDX_Text(pDX, IDC_EDITNAME, m_strName); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CDemo1Dlg, CDialog) 
	//{{AFX_MSG_MAP(CDemo1Dlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1) 
	ON_BN_CLICKED(IDB_BTNREFRESH, OnBtnrefresh) 
	ON_BN_CLICKED(IDC_BTNADD, OnBtnadd) 
	ON_BN_CLICKED(IDC_BTNDEL, OnBtndel) 
	ON_BN_CLICKED(IDC_BTNMODIFY, OnBtnmodify) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo1Dlg message handlers 
 
BOOL CDemo1Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
 
	DWORD dwStyle=GetWindowLong(m_list.GetSafeHwnd(),GWL_STYLE); 
	dwStyle |= LVS_REPORT; 
	SetWindowLongA(m_list.GetSafeHwnd(),GWL_STYLE,dwStyle); 
	m_list.SetExtendedStyle(LVS_EX_HEADERDRAGDROP|LVS_EX_FULLROWSELECT|LVS_EX_TRACKSELECT); 
	m_list.SetImageList(&ImageList, LVSIL_SMALL); 
 
 
	ShowData(); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CDemo1Dlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CDemo1Dlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CDemo1Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
BOOL CDemo1Dlg::ShowData() 
{ 
	int i = 0, iCount; 
	m_list.DeleteAllItems();//首先清空listview 
 
	iCount = m_list.GetHeaderCtrl()->GetItemCount(); 
	for(i = 0; i < iCount; i++) 
	{ 
		m_list.DeleteColumn(0); 
	} 
	for(i = 0; i < iCount; i++) 
	{ 
		m_list.GetHeaderCtrl()->DeleteItem(0); 
	} 
 
	CString strSql; 
	strSql = "select * from emp"; 
	CMyODBC db; 
	db.ConnectDB("daliu","", ""); 
	CODBCSet set; 
	db.PrepareSql(strSql, set); 
	for(i = 0; i < set.GetCols(); i++) 
	{ 
		m_list.InsertColumn(i, set.m_coldatafmt[i].name,LVCFMT_CENTER,80); 
	} 
 
	int iRow = 0; 
	 
	 
 
	while(db.FetchData()) 
	{ 
		for(i = 0; i < set.GetCols(); i++) 
		{ 
			if(i == 0) 
			{ 
				m_list.InsertItem(iRow, set.m_coldata[0].value); 
			} 
			else 
			{ 
				m_list.SetItemText(iRow, i, set.m_coldata[i].value); 
			} 
		 
		} 
		iRow++; 
	} 
 
	set.Empty(); 
	db.DisConnect(); 
		 
	 
return TRUE;	 
 
 
} 
 
void CDemo1Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
	// TODO: Add your control notification handler code here 
	if(pNMListView->uNewState&LVIS_SELECTED) 
	{ 
		char pContent[50] ; 
 
		int iCurrent = pNMListView->iItem; 
		m_list.GetItemText(iCurrent,0,pContent,50); 
		m_strID.Format("%s", pContent); 
		m_list.GetItemText(iCurrent,1,pContent,50); 
		m_strName = CString(pContent); 
		m_list.GetItemText(iCurrent,2,pContent,50); 
		m_strJob = CString(pContent); 
		UpdateData(FALSE); 
	} 
	 
	*pResult = 0; 
} 
 
void CDemo1Dlg::OnBtnrefresh()  
{ 
	// TODO: Add your control notification handler code here 
	this->ShowData(); 
	 
} 
 
void CDemo1Dlg::OnBtnadd()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	CString strSql; 
 
	long nID; 
	nID=atol(m_strID); 
	if(nID<=0) 
	{ 
		//职工号不能为0或者负数 
		MessageBox("“职工号”错误,请重新输入“职工号”!", "demol", MB_ICONSTOP); 
		m_strID=""; 
		UpdateData(false); 
		return; 
	} 
	else 
		strSql.Format("insert into emp values(%d,'%s','%s')", nID, m_strName, m_strJob); 
 
	CMyODBC db; 
	db.ConnectDB("daliu","", "");		//连接数据库 
	db.ExeSqlDirect(strSql);			//执行sql语句 
	db.DisConnect();					//断开连接 
	this->ShowData();					//刷新数据 
	 
	 
} 
 
void CDemo1Dlg::OnBtndel()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	CString strSql; 
	strSql.Format("delete from emp where 职员号 = %s", m_strID); 
	CMyODBC db; 
	db.ConnectDB("daliu","", "");		//连接数据库 
	db.ExeSqlDirect(strSql);			//执行sql语句 
	db.DisConnect();					//断开连接 
	this->ShowData();					//刷新数据 
	m_strID = ""; 
	m_strName = ""; 
	m_strJob = ""; 
	UpdateData(FALSE); 
	 
} 
 
void CDemo1Dlg::OnBtnmodify()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	CString strSql; 
	strSql.Format("update  emp set 职工名称 = '%s', 工作 = '%s' where 职员号 = %s", m_strName, m_strJob, m_strID); 
	CMyODBC db; 
	db.ConnectDB("daliu","", "");		//连接数据库 
	db.ExeSqlDirect(strSql);			//执行sql语句 
	db.DisConnect();					//断开连接 
	this->ShowData();					//刷新数据 
	m_strID = ""; 
	m_strName = ""; 
	m_strJob = ""; 
	UpdateData(FALSE); 
	 
}