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

📄 图书管理系统dlg.cpp

📁 用ADO连接数据库,然后有相应图书馆管理的功能
💻 CPP
字号:
// 图书管理系统Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "图书管理系统.h"
#include "图书管理系统Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_BN = _T("");
	m_R = _T("");
	m_ID = _T("");
	m_P = _T("");
	m_delete = _T("");
	m_search = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nCurrentSel = -1;
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT1, m_BN);
	DDX_Text(pDX, IDC_EDIT4, m_R);
	DDX_Text(pDX, IDC_EDIT2, m_ID);
	DDX_Text(pDX, IDC_EDIT3, m_P);
	DDX_Text(pDX, IDC_EDIT6, m_delete);
	DDX_Text(pDX, IDC_EDIT7, m_search);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    login.DoModal();
	// Add "About..." menu item to system menu.
    if(login.state==0)
	CDialog::OnOK();
   
	//////////为列表控件添加列//////////
	m_list.InsertColumn(0,"书名",LVCFMT_LEFT,40);
    m_list.InsertColumn(1,"书号",LVCFMT_LEFT,40);
    m_list.InsertColumn(2,"定价",LVCFMT_LEFT,40);
    m_list.InsertColumn(3,"借阅人",LVCFMT_LEFT,50);
    m_list.InsertColumn(4,"借书日期",LVCFMT_LEFT,100);


m_pRecordset.CreateInstance("ADODB.Recordset");




readDB();





	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnOK() 
{
	// TODO: Add extra validation here
	readDB();

}

void CMyDlg::OnButton1() 
{		m_nCurrentSel = m_list.InsertItem(0xffff,"");
	// TODO: Add your control notification handler code here
	UpdateData(1);
	
CString m_D="2004.5.6";	
login.m_pConnection->Execute("INSERT INTO library VALUES ('"+(_bstr_t)m_BN+"',"+
							 (_bstr_t)m_ID+","+(_bstr_t)m_P+",'"+(_bstr_t)m_R+"','"+(_bstr_t)m_D+"')",&RecordsAffected,adCmdText);

m_list.SetItem(m_nCurrentSel,0,LVIF_TEXT,(_bstr_t)m_BN,NULL,0,0,0);
m_list.SetItem(m_nCurrentSel,1,LVIF_TEXT,(_bstr_t)m_ID,NULL,0,0,0);
m_list.SetItem(m_nCurrentSel,2,LVIF_TEXT,(_bstr_t)m_P,NULL,0,0,0);
m_list.SetItem(m_nCurrentSel,3,LVIF_TEXT,(_bstr_t)m_R,NULL,0,0,0);
m_list.SetItem(m_nCurrentSel,4,LVIF_TEXT,(_bstr_t)m_D,NULL,0,0,0);



}

void CMyDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
UpdateData();
try{
login.m_pConnection->Execute("delete from library where BID="+(_bstr_t)m_delete+" ",&RecordsAffected,adCmdText);   
}
catch(_com_error e)
{
AfxMessageBox("请输入整数!");
}
readDB();
}






void CMyDlg::readDB()
{m_list.DeleteAllItems();
try
	{ 
m_pRecordset =login.m_pConnection->Execute("SELECT * FROM library",&RecordsAffected,adCmdText);
 }
 	catch(_com_error e)///捕捉异常
	{
	login.m_pConnection->Execute("CREATE TABLE library(BN varchar(20),BID INTEGER,P float,R varchar(20),D varchar(20))",&RecordsAffected,adCmdText);	
    login.m_pConnection->Execute("INSERT INTO library VALUES ('语文',2,113.6,'小王','2004.2.30')",&RecordsAffected,adCmdText);
	login.m_pConnection->Execute("INSERT INTO library VALUES ('数学',3,223.6,'小王','2004.1.30')",&RecordsAffected,adCmdText);
	login.m_pConnection->Execute("INSERT INTO library VALUES ('物理',4,31.6,'小红','2004.9.30')",&RecordsAffected,adCmdText);
	login.m_pConnection->Execute("INSERT INTO library VALUES ('音乐',5,152.6,'小明','2004.2.30')",&RecordsAffected,adCmdText);
   AfxMessageBox("创建表空间,强制重起!");
	//CDialog::OnOK();
	}
int nItem;
m_pRecordset->MoveFirst();
_variant_t BN,BID,P,R,D;
try
	{ 
	while(!m_pRecordset->adoEOF)
	{ BN=m_pRecordset->GetCollect("BN");
      BID=m_pRecordset->GetCollect("BID");
      P=m_pRecordset->GetCollect("P");
      R=m_pRecordset->GetCollect("R"); 
      D=m_pRecordset->GetCollect("D");
      nItem=m_list.InsertItem(0xffff,(_bstr_t)BN);
      m_list.SetItem(nItem,1,1,(_bstr_t)BID,NULL,0,0,0);
	  m_list.SetItem(nItem,2,1,(_bstr_t)P,NULL,0,0,0);
	  m_list.SetItem(nItem,3,1,(_bstr_t)R,NULL,0,0,0);
	  m_list.SetItem(nItem,4,1,(_bstr_t)D,NULL,0,0,0);
	  m_pRecordset->MoveNext();
	}

  }
catch(_com_error e)
{
AfxMessageBox("读取失败!");
CDialog::OnOK();
}
}

void CMyDlg::OnButton2() 
{   m_list.DeleteAllItems();
    UpdateData();
   _RecordsetPtr m_pRecordset;
	try
	{ 
m_pRecordset =login.m_pConnection->Execute("SELECT * FROM library where BID="+(_bstr_t)m_search+" ",&RecordsAffected,adCmdText); 
 }
 	catch(_com_error e)///捕捉异常
	{ AfxMessageBox("不存在该数据");
	}
	_variant_t BN,BID,P,R,D;
    int nItem;
     try
	{ 
	while(!m_pRecordset->adoEOF)
	{ BN=m_pRecordset->GetCollect("BN");
      BID=m_pRecordset->GetCollect("BID");
      P=m_pRecordset->GetCollect("P");
      R=m_pRecordset->GetCollect("R"); 
      D=m_pRecordset->GetCollect("D");
      nItem=m_list.InsertItem(0xffff,(_bstr_t)BN);
      m_list.SetItem(nItem,1,1,(_bstr_t)BID,NULL,0,0,0);
	  m_list.SetItem(nItem,2,1,(_bstr_t)P,NULL,0,0,0);
	  m_list.SetItem(nItem,3,1,(_bstr_t)R,NULL,0,0,0);
	  m_list.SetItem(nItem,4,1,(_bstr_t)D,NULL,0,0,0);
	  m_pRecordset->MoveNext();
	}
 }
catch(_com_error e)
{
AfxMessageBox("读取失败!");

}


}

void CMyDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	  m_pRecordset->Close();///关闭记录集
      login.m_pConnection->Close();///关闭连接

	CDialog::OnCancel();
}

⌨️ 快捷键说明

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