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

📄 bookborrow.cpp

📁 C++ Builder数据库开发经典案例解析 示例程序都是在C++ Builder 6.0(Enterprise)和SQL Server 2000个人版下调试通过的
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "BookBorrow.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfmBookBorrow *fmBookBorrow;
//---------------------------------------------------------------------------
__fastcall TfmBookBorrow::TfmBookBorrow(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::FormShow(TObject *Sender)
{
    // 输入读者查询条件获取焦点
    edReaderCon->SetFocus();
    // 设置借阅图书明细Grid的标题
    StringGrid1->Cells[0][0] = "状态";
    StringGrid1->Cells[1][0] = "编号";
    StringGrid1->Cells[2][0] = "书名";
    StringGrid1->Cells[3][0] = "借出时间";
    StringGrid1->Cells[4][0] = "应还时间";
    StringGrid1->Cells[5][0] = "出版社";
    StringGrid1->Cells[6][0] = "书价";
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::btOkClick(TObject *Sender)
{
    // 保存 新借 的图书
    AnsiString sql;
    for(int i=1; i<StringGrid1->RowCount; i++)
    {
        if(!(StringGrid1->Cells[0][i] == "新借" && StringGrid1->Cells[1][i].Length()>0))
        {
            continue;
        }
        sql = "insert into 图书借阅(图书编号, 读者编号, 借阅时间, ";
        sql += "应还时间, 续借次数, 操作员, 状态) values ('";
        sql += StringGrid1->Cells[1][i] + "','";
        sql += edReaderCon->Text + "','";
        sql += StringGrid1->Cells[3][i] + "','";
        sql += StringGrid1->Cells[4][i] + "',0,'操作员','新借')";
        Query1->SQL->Clear();
        Query1->SQL->Add(sql);
        Query1->ExecSQL();
    }

    // 调用存储过程, 减少图书库存
    TQuery* pQuery = new TQuery(NULL);
    pQuery->DatabaseName = "db";
    pQuery->SQL->Add("exec sf_图书借阅");
    pQuery->ExecSQL();
    delete pQuery;

    m_nBorrowNum = 0;
    m_nNum = 0;
    // 清空数据
    edReaderCon->Text = "";
    edBookCon->Text = "";
    // 输入读者查询条件获取焦点
    edReaderCon->SetFocus();
    // 清空借阅列表
    for(int i=1; i<StringGrid1->RowCount; i++)
    {
        StringGrid1->Cells[0][i] = "";
        StringGrid1->Cells[1][i] = "";
        StringGrid1->Cells[2][i] = "";
        StringGrid1->Cells[3][i] = "";
        StringGrid1->Cells[4][i] = "";
        StringGrid1->Cells[5][i] = "";
        StringGrid1->Cells[6][i] = "";
    }
    Panel2->Caption = "";
}
//---------------------------------------------------------------------------

void __fastcall TfmBookBorrow::btCancelClick(TObject *Sender)
{
    m_nBorrowNum = 0;
    m_nNum = 0;
    // 清空数据
    edReaderCon->Text = "";
    edBookCon->Text = "";
    // 输入读者查询条件获取焦点
    edReaderCon->SetFocus();
    // 清空借阅列表
    for(int i=1; i<StringGrid1->RowCount; i++)
    {
        StringGrid1->Cells[0][i] = "";
        StringGrid1->Cells[1][i] = "";
        StringGrid1->Cells[2][i] = "";
        StringGrid1->Cells[3][i] = "";
        StringGrid1->Cells[4][i] = "";
        StringGrid1->Cells[5][i] = "";
        StringGrid1->Cells[6][i] = "";
    }
    Panel2->Caption = "";
}
//---------------------------------------------------------------------------

void __fastcall TfmBookBorrow::btExitClick(TObject *Sender)
{
    this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::FormClose(TObject *Sender,
      TCloseAction &Action)
{
    // 删除窗体并回收空间
    Action = caFree;
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::edReaderConKeyPress(TObject *Sender, char &Key)
{
    //  回车选择读者
    if(Key == 13)
    {
        // 检查是否存在未保存的记录
        for(int i=1; i<StringGrid1->RowCount; i++)
        {
            if(StringGrid1->Cells[0][i] == "新借")
            {
                Application->MessageBox("存在未保存的记录","提示",MB_OK);
                return;
            }
        }
        AnsiString szCon, sql, szReadCode;
        // 按照编号查询
        if(rbReaderCode->Checked)
            szCon = "编号";
        else
            szCon = "条形码";
        szCon += "='" + edReaderCon->Text + "'";
        sql = "select 姓名,类型,编号 from 读者信息 where " + szCon;
        Query1->SQL->Clear();
        Query1->SQL->Add(sql);
        Query1->Open();
        // 选择读者
        edName->Text = Query1->FieldByName("姓名")->AsString;
        edType->Text = Query1->FieldByName("类型")->AsString;
        szReadCode = Query1->FieldByName("编号")->AsString;
        // 如果查询条件不正确,返回重新查询
        if(edName->Text == "")
        {
            return;
        }
        // 选择该类型读者可以借阅的册书
        Query1->SQL->Clear();
        sql = "select 图书册书 from 读者类型 where 类型='";
        sql += edType->Text + "'";
        Query1->SQL->Add(sql);
        Query1->Open();
        edNum->Text = Query1->FieldByName("图书册书")->AsString;
        // 将焦点跳到选择图书
        edBookCon->SetFocus();
        // -------------------
        // 在借书列表中显示已经借出的图书
        Query1->SQL->Clear();
        sql = "select 图书编号,借阅时间,应还时间 from 图书借阅 ";
        sql += "where 状态='未还' and 读者编号='";
        sql += szReadCode + "'";
        Query1->SQL->Add(sql);
        Query1->Open();
        Query1->First();
        m_nBorrowNum = 0;
        // 清空借阅列表
        for(int i=1; i<StringGrid1->RowCount; i++)
        {
            StringGrid1->Cells[0][i] = "";
            StringGrid1->Cells[1][i] = "";
            StringGrid1->Cells[2][i] = "";
            StringGrid1->Cells[3][i] = "";
            StringGrid1->Cells[4][i] = "";
            StringGrid1->Cells[5][i] = "";
            StringGrid1->Cells[6][i] = "";
        }

        // 序号写入已借图书列表
        while(!Query1->Eof)
        {
            // 借出册书加一
            m_nBorrowNum ++;
            AnsiString szBookCode = Query1->FieldByName("图书编号")->AsString;
            Table1->Filtered = false;
            Table1->Filter = "编号='" + szBookCode + "'";
            Table1->Filtered = true;
            StringGrid1->Cells[0][m_nBorrowNum] = "未还";
            StringGrid1->Cells[1][m_nBorrowNum] =
                Table1->FieldByName("编号")->AsString;
            StringGrid1->Cells[2][m_nBorrowNum] =
                Table1->FieldByName("书名")->AsString;
            StringGrid1->Cells[3][m_nBorrowNum] =
                Query1->FieldByName("借阅时间")->AsString;
            StringGrid1->Cells[4][m_nBorrowNum] =
                Query1->FieldByName("应还时间")->AsString;
            StringGrid1->Cells[5][m_nBorrowNum] =
                Table1->FieldByName("出版社")->AsString;
            StringGrid1->Cells[6][m_nBorrowNum] =
                Table1->FieldByName("价格")->AsString;
            // 下一记录
            Query1->Next();
        }
    }
    // 设置状态条册书
    Panel2->Caption = "共 " + IntToStr(m_nBorrowNum) + " 册    ";
    Panel2->Caption = Panel2->Caption + "本次 0 册";
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::edBookConKeyPress(TObject *Sender,
      char &Key)
{
    //  回车选择图书
    if(Key == 13)
    {
        // 检查数量是否达到限制了
        if(m_nBorrowNum >= StrToInt(edNum->Text))
        {
            Application->MessageBox("借书数量达到上限","提示",MB_OK);
            return;
        }

        AnsiString szCon, sql, szBookType;
        // 按照编号查询
        if(rbBookCode->Checked)
            szCon = "编号";
        else
            szCon = "条形码";
        szCon += "='" + edBookCon->Text + "'";
        Table1->Filtered = false;
        Table1->Filter = szCon;
        Table1->Filtered = true;

        // 先检查选择的图书在列表中是否存在
        for(int i=1; i<StringGrid1->RowCount; i++)
        {
            if(StringGrid1->Cells[1][i] == Table1->FieldByName("编号")->AsString)
            {
                Application->MessageBox("图书已借给该读者","提示",MB_OK);
                return;
            }
        }

        szBookType = Table1->FieldByName("类型")->AsString;
        // 获得该类别图书的借期
        sql = "select 可借天数 from 图书类型 where 类型名称='";
        sql += szBookType + "'";
        // 选择该类型读者可以借阅的册书
        Query1->SQL->Clear();
        Query1->SQL->Add(sql);
        Query1->Open();
        // 借出册书加1
        m_nBorrowNum ++;
        m_nNum ++;
        TDateTime dt;
        dt = dt.CurrentDate();   // 借阅时间取当前时间
        StringGrid1->Cells[0][m_nBorrowNum] = "新借";
        StringGrid1->Cells[1][m_nBorrowNum] =
                Table1->FieldByName("编号")->AsString;
        StringGrid1->Cells[2][m_nBorrowNum] =
                Table1->FieldByName("书名")->AsString;
        // 借阅时间取当前时间
        StringGrid1->Cells[3][m_nBorrowNum] = dt.DateString();
        // 应还时间当前时间+可借天数
        dt = dt + Query1->FieldByName("可借天数")->AsInteger;
        StringGrid1->Cells[4][m_nBorrowNum] = dt.DateString();
        StringGrid1->Cells[5][m_nBorrowNum] =
                Table1->FieldByName("出版社")->AsString;
        StringGrid1->Cells[6][m_nBorrowNum] =
                Table1->FieldByName("价格")->AsString;
        // -------------------------
        // 设置状态条册书
        Panel2->Caption = "共 " + IntToStr(m_nBorrowNum) + " 册    ";
        Panel2->Caption = Panel2->Caption + "本次 ";
        Panel2->Caption = Panel2->Caption + IntToStr(m_nNum) + " 册";
        // -------------------------
        // 清空图书查询条件
        edBookCon->Text = "";
    }
}
//---------------------------------------------------------------------------
void __fastcall TfmBookBorrow::FormCloseQuery(TObject *Sender,
      bool &CanClose)
{
    // 关闭检查是否存在未保存的记录
    for(int i=1; i<StringGrid1->RowCount; i++)
    {
        if(StringGrid1->Cells[0][i] == "新借")
        {
            if(Application->MessageBox("未保存数据,是否关闭?",
                    "提示",MB_YESNO) == IDYES)
            {
                CanClose = true;
            }
            else
                CanClose = false;
            break;
        }
    }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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