设计mfc图书管理系统功能设计的代码
时间: 2023-09-10 16:09:12 浏览: 121
好的,以下是MFC图书管理系统的功能模块和对应的代码实现:
1. 登录界面功能模块
- 用户名和密码验证功能:
```
BOOL CLoginDlg::CheckUser(LPCTSTR strUser, LPCTSTR strPwd)
{
// 连接数据库
CDatabase db;
db.OpenEx(_T("DSN=book_management_sys;"), CDatabase::readOnly);
// 查询用户信息
CString strSql;
strSql.Format(_T("SELECT * FROM tb_user WHERE username='%s' AND password='%s'"), strUser, strPwd);
CRecordset rs(&db);
rs.Open(CRecordset::snapshot, strSql);
// 判断用户是否存在并且密码正确
BOOL bResult = FALSE;
if (!rs.IsEOF() && !rs.IsBOF())
{
bResult = TRUE;
}
rs.Close();
db.Close();
return bResult;
}
```
2. 图书管理界面功能模块
- 添加图书信息功能:
```
void CBookManageDlg::OnBtnAdd()
{
// 获取图书信息
CString strBookName, strAuthor, strPublisher, strCategory;
m_edtBookName.GetWindowText(strBookName);
m_edtAuthor.GetWindowText(strAuthor);
m_edtPublisher.GetWindowText(strPublisher);
m_edtCategory.GetWindowText(strCategory);
// 插入图书信息
CDatabase db;
db.OpenEx(_T("DSN=book_management_sys;"), CDatabase::useDefaultType);
CString strSql;
strSql.Format(_T("INSERT INTO tb_book(name, author, publisher, category) VALUES('%s', '%s', '%s', '%s')"),
strBookName, strAuthor, strPublisher, strCategory);
db.ExecuteSQL(strSql);
db.Close();
// 刷新列表
RefreshList();
}
```
3. 读者管理界面功能模块
- 添加读者信息功能:
```
void CReaderManageDlg::OnBtnAdd()
{
// 获取读者信息
CString strReaderName, strReaderSex, strReaderAge, strReaderTel;
m_edtReaderName.GetWindowText(strReaderName);
m_edtReaderSex.GetWindowText(strReaderSex);
m_edtReaderAge.GetWindowText(strReaderAge);
m_edtReaderTel.GetWindowText(strReaderTel);
// 插入读者信息
CDatabase db;
db.OpenEx(_T("DSN=book_management_sys;"), CDatabase::useDefaultType);
CString strSql;
strSql.Format(_T("INSERT INTO tb_reader(name, sex, age, tel) VALUES('%s', '%s', '%s', '%s')"),
strReaderName, strReaderSex, strReaderAge, strReaderTel);
db.ExecuteSQL(strSql);
db.Close();
// 刷新列表
RefreshList();
}
```
4. 借阅管理界面功能模块
- 借阅图书功能:
```
void CBorrowManageDlg::OnBtnBorrow()
{
// 获取读者信息和图书信息
CString strReaderId, strBookId;
m_edtReaderId.GetWindowText(strReaderId);
m_edtBookId.GetWindowText(strBookId);
// 判断读者是否存在
if (!CheckReader(strReaderId))
{
MessageBox(_T("读者不存在!"), _T("提示"), MB_OK | MB_ICONWARNING);
return;
}
// 判断图书是否存在
if (!CheckBook(strBookId))
{
MessageBox(_T("图书不存在!"), _T("提示"), MB_OK | MB_ICONWARNING);
return;
}
// 判断图书是否已借出
if (CheckBorrowed(strBookId))
{
MessageBox(_T("图书已借出!"), _T("提示"), MB_OK | MB_ICONWARNING);
return;
}
// 借阅图书
CTime tNow = CTime::GetCurrentTime();
CString strTime = tNow.Format(_T("%Y-%m-%d %H:%M:%S"));
CDatabase db;
db.OpenEx(_T("DSN=book_management_sys;"), CDatabase::useDefaultType);
CString strSql;
strSql.Format(_T("INSERT INTO tb_borrow(reader_id, book_id, borrow_time) VALUES('%s', '%s', '%s')"),
strReaderId, strBookId, strTime);
db.ExecuteSQL(strSql);
db.Close();
// 刷新列表
RefreshList();
}
```
5. 统计查询界面功能模块
- 查询借阅信息功能:
```
void CStatisticQueryDlg::OnBtnQuery()
{
// 获取查询条件
CString strReaderId, strBookId;
m_edtReaderId.GetWindowText(strReaderId);
m_edtBookId.GetWindowText(strBookId);
// 查询借阅信息
CDatabase db;
db.OpenEx(_T("DSN=book_management_sys;"), CDatabase::readOnly);
CString strSql;
if (strReaderId.IsEmpty() && strBookId.IsEmpty())
{
strSql = _T("SELECT * FROM tb_borrow");
}
else if (!strReaderId.IsEmpty() && strBookId.IsEmpty())
{
strSql.Format(_T("SELECT * FROM tb_borrow WHERE reader_id='%s'"), strReaderId);
}
else if (strReaderId.IsEmpty() && !strBookId.IsEmpty())
{
strSql.Format(_T("SELECT * FROM tb_borrow WHERE book_id='%s'"), strBookId);
}
else
{
strSql.Format(_T("SELECT * FROM tb_borrow WHERE reader_id='%s' AND book_id='%s'"), strReaderId, strBookId);
}
CRecordset rs(&db);
rs.Open(CRecordset::snapshot, strSql);
// 显示借阅信息
m_lstBorrow.DeleteAllItems();
int nIndex = 0;
while (!rs.IsEOF())
{
CString strReaderId, strBookId, strBorrowTime;
rs.GetFieldValue(_T("reader_id"), strReaderId);
rs.GetFieldValue(_T("book_id"), strBookId);
rs.GetFieldValue(_T("borrow_time"), strBorrowTime);
m_lstBorrow.InsertItem(nIndex, strReaderId);
m_lstBorrow.SetItemText(nIndex, 1, strBookId);
m_lstBorrow.SetItemText(nIndex, 2, strBorrowTime);
rs.MoveNext();
nIndex++;
}
rs.Close();
db.Close();
}
```
希望以上代码可以帮助您完成MFC图书管理系统的功能设计。
阅读全文