// xitong.cpp : implementation file
//
#include "stdafx.h"
#include "Project2.h"
#include "xitong.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <afxdb.h>
#include <stdio.h>
#include <string.h>
extern CDatabase m_db;
/
// xitong dialog
xitong::xitong(CWnd* pParent /*=NULL*/)
: CDialog(xitong::IDD, pParent)
{
//{{AFX_DATA_INIT(xitong)
m_beizhu = _T("");
m_chengyang = _T("");
m_kongci = _T("");
m_zhuohou = _T("");
//}}AFX_DATA_INIT
}
void xitong::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(xitong)
DDX_Control(pDX, IDC_xianshi, m_xianshi);
DDX_Text(pDX, IDC_beizhu, m_beizhu);
DDX_Text(pDX, IDC_chengyang, m_chengyang);
DDX_Text(pDX, IDC_kongci, m_kongci);
DDX_Text(pDX, IDC_zhuohou, m_zhuohou);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(xitong, CDialog)
//{{AFX_MSG_MAP(xitong)
ON_BN_CLICKED(quxiao, Onquxiao)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_tianjia, Ontianjia)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// xitong message handlers
void xitong::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
}
BOOL xitong::OnInitDialog()
{
CDialog::OnInitDialog();
InitControl();
InitCtrlData();
//GetDlgItem(IDC_shifouzaiguan)->EnableWindow(TRUE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void xitong::Onquxiao()
{
// TODO: Add your control notification handler code here
OnOK();
}
void xitong::Ontianjia()
{
// TODO: Add your control notification handler code here
//从界面控件中获取信息更新到控件变量中.
if(!UpdateData())
return;
/*if(m_shuming.IsEmpty())
{
AfxMessageBox("书名不能为空!");
return;
}*/
TRY{
m_db.BeginTrans();
CRecordset rs(&m_db);
CString sql;
//获取新的客户ID值.
rs.Open(CRecordset::dynaset,
"Select max(id) from weight");
int newID = 1;
if(!rs.IsEOF())
{
CDBVariant var;
rs.GetFieldValue((short)0, var, SQL_C_SLONG);
if (var.m_dwType != DBVT_NULL)
{
newID = var.m_iVal+1;
var.Clear();
}
}
//插入新的客户信息记录.
int jieguo,chengyang,kongci,zhuohou;
if (m_chengyang.IsEmpty()) {
chengyang = 0;
} else {
chengyang = atoi(m_chengyang);
}
if (m_kongci.IsEmpty()) {
kongci = 0;
} else {
kongci = atoi(m_kongci);
}
if (m_zhuohou.IsEmpty()) {
zhuohou = 0;
} else {
zhuohou = atoi(m_zhuohou);
}
jieguo = chengyang + kongci + zhuohou;
//获取系统时间
SYSTEMTIME st;
GetSystemTime(&st);
CString nianyueri;
strcat(nianyueri,st.wYear);
strcat(nianyueri,st.wMonth);
strcat(nianyueri,st.wDay);
CString s;
s.Format("%d",jieguo);
sql.Format("Insert into weight(id,nianyueri,chengyang,kongci,zhuohou,jieguo,beizhu) VALUES(%d,'%s','%s','%s','%s','%s','%s')",newID,nianyueri,m_chengyang,m_kongci,m_zhuohou,s,m_beizhu);
m_db.ExecuteSQL(sql);
rs.Close();
//向界面中插入新的客户信息.
InsertCRInfoItem(newID,nianyueri,m_chengyang,m_kongci,m_zhuohou,s,m_beizhu);
m_db.CommitTrans();
}
CATCH(CDBException,ex)
{
AfxMessageBox (ex->m_strError);
AfxMessageBox (ex->m_strStateNativeOrigin);
m_db.Rollback();
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox (szError);
m_db.Rollback();
}
END_CATCH
}
void xitong::InitControl()
{
DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES
|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE | LVS_EX_UNDERLINEHOT;
m_xianshi.SetExtendedStyle(dwExStyle);
m_xianshi.InsertColumn(0,"id",LVCFMT_CENTER,50);
m_xianshi.InsertColumn(1,"年月日",LVCFMT_CENTER,100);
m_xianshi.InsertColumn(2,"称样重",LVCFMT_CENTER,100);
m_xianshi.InsertColumn(3,"空瓷重量",LVCFMT_CENTER,100);
m_xianshi.InsertColumn(4,"灼后重量",LVCFMT_CENTER,100);
m_xianshi.InsertColumn(5,"结果",LVCFMT_CENTER,100);
m_xianshi.InsertColumn(6,"备注",LVCFMT_CENTER,100);
}
void xitong::InitCtrlData()
{
if(!m_db.IsOpen()){
MessageBox("数据库未打开");
return;
}
TRY{
CRecordset rs(&m_db);//类对象代表一个从数据源中获取的纪录集
//打开所有的用户信息记录
CString sql;
sql.Format("select * from weight");
rs.Open(CRecordset::dynaset, sql);
while (!rs.IsEOF())
{
int id;
CString nianyueri,beizhu,chengyang,kongci,zhuohou,jieguo;
CDBVariant var;
//获取客户信息ID字段值
rs.GetFieldValue((short)0, var, SQL_C_SLONG);
if (var.m_dwType != DBVT_NULL)
id = var.m_iVal;
var.Clear();
//获取基本信息
rs.GetFieldValue(1,nianyueri);
rs.GetFieldValue(2,chengyang);
rs.GetFieldValue(3,kongci);
rs.GetFieldValue(4,zhuohou);
rs.GetFieldValue(5,jieguo);
rs.GetFieldValue(6,beizhu);
InsertCRInfoItem(id,nianyueri,chengyang,kongci,zhuohou,jieguo,beizhu);
rs.MoveNext();
}
rs.Close();
}
CATCH(CDBException,ex)
{
AfxMessageBox (ex->m_strError);
AfxMessageBox (ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox ("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox (szError);
}
END_CATCH
}
void xitong::InsertCRInfoItem(int id, CString nianyueri,CString chengyang, CString kongci,CString zhuohou,CString jieguo,CString beizhu)
{
int nIndex = m_xianshi.GetItemCount();
LV_ITEM lvItem;
lvItem.mask = LVIF_TEXT ;
lvItem.iItem = nIndex; //行数
lvItem.iSubItem = 0;
CString temp ;
temp.Format("%d",id);
lvItem.pszText = (char*)(LPCTSTR)temp; //第一列
//在最后一行插入记录值.
m_xianshi.InsertItem(&lvItem);
//设置该行的其他列的值.
m_xianshi.SetItemText(nIndex,1,nianyueri);
m_xianshi.SetItemText(nIndex,2,chengyang);
m_xianshi.SetItemText(nIndex,3,kongci);
m_xianshi.SetItemText(nIndex,4,zhuohou);
m_xianshi.SetItemText(nIndex,5,jieguo);
m_xianshi.SetItemText(nIndex,6,beizhu);
}