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

📄 kufangdbsdlg.cpp

📁 用Delphi设计学院库房管理数据库系统。系统由三部分组成:物资供应管理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// kufangDBSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "kufangDBS.h"
#include "kufangDBSDlg.h"
#include "ShebeiDlg.h"
#include "XunDlg.h"
#include "SjxxDlg.h"
#include "XunsjxxDlg.h"
#include "TjshebeiDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CKufangDBSDlg dialog

CKufangDBSDlg::CKufangDBSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKufangDBSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKufangDBSDlg)
	m_strDBSource = _T("");
	m_strDBUser = _T("");
	m_strDBPassword = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKufangDBSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKufangDBSDlg)
	DDX_Control(pDX, IDC_LIST_XIANSHI_SJXX, m_listSjxx);
	DDX_Control(pDX, IDC_LIST_XIANSHI_SHEBEI, m_listShebei);
	DDX_Text(pDX, IDC_EDIT_DBSOURCE, m_strDBSource);
	DDX_Text(pDX, IDC_EDIT_DBUSER, m_strDBUser);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strDBPassword);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKufangDBSDlg, CDialog)
	//{{AFX_MSG_MAP(CKufangDBSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_DBCONNECT, OnBtnDbconnect)
	ON_BN_CLICKED(IDC_SYS_EXIT, OnSysExit)
	ON_BN_CLICKED(IDC_BTN_ADD_SHEBEI, OnBtnAddShebei)
	ON_BN_CLICKED(IDC_BTN_XIUGAI_SHEBEI, OnBtnXiugaiShebei)
	ON_BN_CLICKED(IDC_BTN_DEL_SHEBEI, OnBtnDelShebei)
	ON_BN_CLICKED(IDC_BTN_BAOBIAO_SHEBEI, OnBtnBaobiaoShebei)
	ON_BN_CLICKED(IDC_BTN_SJ_XINXI, OnBtnSjXinxi)
	ON_BN_CLICKED(IDC_BTN_XUN_SHEBEI, OnBtnXunShebei)
	ON_BN_CLICKED(IDC_BTN_ADD_SJXX, OnBtnAddSjxx)
	ON_BN_CLICKED(IDC_BTN_XIUGAI_SJXX, OnBtnXiugaiSjxx)
	ON_BN_CLICKED(IDC_BTN_DEL_SJXX, OnBtnDelSjxx)
	ON_BN_CLICKED(IDC_BTN_XUN_SJXX, OnBtnXunSjxx)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKufangDBSDlg message handlers

BOOL CKufangDBSDlg::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
		CKufangDBSDlg::InitControl();
	return TRUE;  // return TRUE  unless you set the focus to a control

}


void CKufangDBSDlg::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 CKufangDBSDlg::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 CKufangDBSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
CDatabase m_db;
void CKufangDBSDlg::OnBtnDbconnect() 
{
	// TODO: Add your control notification handler code here
	// 从界面控件中获取信息更新到控件变量中
if(! UpdateData())
return;
//检查数据库是否已经连接,如果连接,则返回
if(m_db.IsOpen()){
   AfxMessageBox("数据库已经连接");
return;
}
//检查数据库配置参数
if(m_strDBSource.IsEmpty() || m_strDBUser.IsEmpty() || m_strDBPassword.IsEmpty()){
AfxMessageBox("数据库配置参数不能够为空");
return;
}
//创建连接字符串
CString strConnect;
strConnect.Format("DSN=%s;UID=%s;PWD=%s",m_strDBSource,m_strDBUser,m_strDBPassword);
//打开数据库的连接,并捕获异常
TRY{
   m_db.OpenEx(strConnect,CDatabase::noOdbcDialog);
}
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
InitCtrlData();  //和InitControl不一样

	
}

void CKufangDBSDlg::OnSysExit() 
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CKufangDBSDlg::InitControl()
{
//设置列表空间风格
DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE | LVS_EX_UNDERLINEHOT;
m_listShebei.SetExtendedStyle(dwExStyle);
// m_list  .SetExtendedstyle(dwExstyle);

//初始化设备列表控件
m_listShebei.InsertColumn(0,"仪器编号",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(1,"分类号",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(2,"仪器名称",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(3,"型号",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(4,"规格",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(5,"单价",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(6,"厂家",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(7,"国别",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(8,"出厂号",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(9,"出厂日期",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(10,"供应商",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(11,"购置日期",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(12,"验收人",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(13,"现状",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(14,"领用单位",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(15,"领用人",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(16,"使用方向",LVCFMT_CENTER,60) ;
m_listShebei.InsertColumn(17,"经费科目",LVCFMT_CENTER,45) ;
m_listShebei.InsertColumn(18,"经手人",LVCFMT_CENTER,60) ;
//初始化商家信息列表控件
m_listSjxx.SetExtendedStyle(dwExStyle);
m_listSjxx.InsertColumn(0,"商家编号",LVCFMT_CENTER,60) ;
m_listSjxx.InsertColumn(1,"商家名称",LVCFMT_CENTER,60) ;
m_listSjxx.InsertColumn(2,"商家电话",LVCFMT_CENTER,60) ;
m_listSjxx.InsertColumn(3,"传真号码",LVCFMT_CENTER,60) ;
m_listSjxx.InsertColumn(4,"联系人",LVCFMT_CENTER,60) ;
m_listSjxx.InsertColumn(5,"联系人电话",LVCFMT_CENTER,60) ;
//

}

void CKufangDBSDlg::InsertShebeiItem(int yqbh, CString flh, CString yqmc, CString xh, CString gg,CString dj,CString  cj ,CString  gb , CString  cch ,CString ccrq ,CString gys , CString gzrq ,CString ysr ,CString xz ,CString lydw ,CString lyr ,CString syfx ,CString jfkm ,CString jsr)
{
//获取当前的显示条数
int nIndex = m_listShebei.GetItemCount();
LV_ITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = nIndex;
lvItem.iSubItem = 0;
CString temp;
temp.Format("%d", yqbh);
lvItem.pszText = (char*)(LPCTSTR) temp;
//在nIndex一行插入数据
m_listShebei . InsertItem(&lvItem);
m_listShebei . SetItemText(nIndex,1, flh);
m_listShebei . SetItemText(nIndex,2, yqmc);
m_listShebei . SetItemText(nIndex,3, xh);
m_listShebei . SetItemText(nIndex,4, gg); 
m_listShebei . SetItemText(nIndex,5, dj);
m_listShebei . SetItemText(nIndex,6, cj);
m_listShebei . SetItemText(nIndex,7, gb);
m_listShebei . SetItemText(nIndex,8, cch);
m_listShebei . SetItemText(nIndex,9, ccrq);
m_listShebei . SetItemText(nIndex,10, gys);
m_listShebei . SetItemText(nIndex,11, gzrq);
m_listShebei . SetItemText(nIndex,12, ysr);
m_listShebei . SetItemText(nIndex,13, xz);
m_listShebei . SetItemText(nIndex,14, lydw); 
m_listShebei . SetItemText(nIndex,15, lyr);
m_listShebei . SetItemText(nIndex,16, syfx);
m_listShebei . SetItemText(nIndex,17, jfkm);
m_listShebei . SetItemText(nIndex,18, jsr);

}

void CKufangDBSDlg::InitCtrlData()
{
TRY{
  CRecordset rs;
  rs.m_pDatabase = & m_db;
  //向成员列表中添加成员记录信息
  CString sql = "Select * from sb";
  //获取所有的设备记录集
  rs.Open(CRecordset::dynaset, sql);
   //设备部分变量
  int yqbh;
  CString xh,dj, cj , gb , ccrq , gys , gzrq , ysr , xz , lydw , lyr , syfx , jfkm , jsr, flh, yqmc, gg, cch;
  while (!rs.IsEOF()){
    CDBVariant  var;
  //获取成员字段值
  rs.GetFieldValue((short)0,var,SQL_C_SLONG);
    if (var.m_dwType !=DBVT_NULL)
        yqbh = var.m_iVal;
   var.Clear();
  rs.GetFieldValue(1, flh);
  rs.GetFieldValue(2, yqmc);
  rs.GetFieldValue(3, xh);
  rs.GetFieldValue(4,gg);
  rs.GetFieldValue(5,dj);
  //rs.GetFieldValue(5,var,SQL_C_SLONG);
   /* if (var.m_dwType !=DBVT_NULL)
        dj = var.m_iVal;
   var.Clear();*/


  rs.GetFieldValue(6,cj);
  rs.GetFieldValue(7, gb);
  rs.GetFieldValue(8, cch);
  rs.GetFieldValue(9, ccrq);
  rs.GetFieldValue(10, gys);
  rs.GetFieldValue(11, gzrq);
  rs.GetFieldValue(12, ysr);
  rs.GetFieldValue(13, xz);
  rs.GetFieldValue(14, lydw);
  rs.GetFieldValue(15, lyr);
  rs.GetFieldValue(16, syfx);
  rs.GetFieldValue(17, jfkm);
  rs.GetFieldValue(18, jsr);
  InsertShebeiItem(yqbh, flh, yqmc,xh,  gg, dj,cj , gb , cch ,ccrq ,gys , gzrq ,ysr , xz ,lydw ,lyr ,syfx ,jfkm ,jsr);
  rs.MoveNext();
  }
rs.Close();
  //////////////////////////////////////
  sql= "Select * from sj";
  rs.Open(CRecordset::dynaset, sql);
  //商家部分变量
  int sjbh;
  CString sjmc,sjdh,czhm,lxr,lxrdh;
  while (!rs.IsEOF()){
    CDBVariant  var;
  //商家部分获取成员字段值
  rs.GetFieldValue((short)0,var,SQL_C_SLONG);
    if (var.m_dwType !=DBVT_NULL)
        sjbh = var.m_iVal;
   var.Clear();
  rs.GetFieldValue(1, sjmc);
  rs.GetFieldValue(2, sjdh);
  rs.GetFieldValue(3, czhm);
  rs.GetFieldValue(4,lxr);
  rs.GetFieldValue(5,lxrdh);
  InsertSjxxItem(sjbh,sjmc,sjdh,czhm,lxr,lxrdh);
  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 CKufangDBSDlg::OnBtnAddShebei() 
{
	// TODO: Add your control notification handler code here
//创建一个成员对话框实例
if(!m_db.IsOpen()){
   AfxMessageBox("数据库未连接");
return;
}
	CShebeiDlg dlg;
//打开对话框
if(dlg.DoModal() == IDOK){
 //从对话框中获取姓名和生日参数
	/*仪器编号一行由程序自动添加*/
CString  strFlh = dlg.m_strFlh;
CString  strYqmc = dlg.m_strYqmc;
CString  strXh = dlg.m_strXh;
CString  strGg = dlg.m_strGg;
CString  strDj = dlg.m_strDj;
CString  strCj = dlg.m_strCj;
CString  strGb = dlg.m_strGb;
CString  strCch = dlg.m_strCch;
CString  strCcrq = dlg.m_dtCcrq.Format("%Y-%m-%d");
CString  strGys = dlg.m_strGys;
CString  strGzrq = dlg.m_dtGzrq.Format("%Y-%m-%d");
CString  strYsr = dlg.m_strYsr;
CString  strXz = dlg.m_strXz;
CString  strLydw = dlg.m_strLydw;
CString  strLyr = dlg.m_strLyr;
CString  strSyfx = dlg.m_strSyfx;
CString  strJfkm = dlg.m_strJfkm;
CString  strJsr = dlg.m_strJsr;
TRY{
//打开记录集,获取最大的成员ID值
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,"select max(yqbh) from sb");
//设置新添加记录的成员ID值
int newShebeiID = 1;
//如果数据库里面已经有记录了,则新的成员ID 是成员ID最大值+1
if(!rs.IsEOF()){
    CDBVariant var;
    rs.GetFieldValue((short)0, var, SQL_C_SLONG);
    if(var.m_dwType != DBVT_NULL)
        newShebeiID = var.m_iVal + 1 ;
}
//创建插入新记录的字符
CString sql ;
sql.Format("Insert into sb(yqbh,"
"flh,yqmc,xh,gg,dj,cj,gb,cch,ccrq,gys,gzrq,ysr,xz,lydw,lyr,syfx,jfkm,jsr)"
"VALUES("

⌨️ 快捷键说明

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