unit bookinf;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB, Mask, DBCtrls, ExtCtrls;

type
  Tadmin_bookinf = class(TForm)
    DBGrid1: TDBGrid;
    btnDelete: TButton;
    btnExit: TButton;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    btnSeek: TButton;
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    GroupBox2: TGroupBox;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    btnAppend: TButton;
    btnModify: TButton;
    procedure btnExitClick(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnSeekClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnModifyClick(Sender: TObject);
    procedure btnAppendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  admin_bookinf: Tadmin_bookinf;
  arrInf:TStringList;  //字符串列表变量，用于存放字段名
implementation
//引用单元
uses login,admin, book_record;

{$R *.dfm}

procedure Tadmin_bookinf.btnExitClick(Sender: TObject);
begin
  close;       //关闭窗体
end;

procedure Tadmin_bookinf.ComboBox2Change(Sender: TObject);
begin
if combobox2.itemindex=0 then  begin
    combobox1.Visible:=false;     //隐藏查询表达式其他部分
    edit1.Visible:=false;
  end
else  begin
    combobox1.Visible:=true;    //显示查询表达式其他部分
    edit1.Visible:=true;
  end;
end;

procedure Tadmin_bookinf.btnSeekClick(Sender: TObject);
var
  strSql:string;   //临时存放SQL命令串
begin
  if combobox2.ItemIndex>0 then  begin
     try
       case  combobox2.ItemIndex-1 of
        0,1,2,3,4,10,11:    //查询字段为串符串类型
         strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+quotedstr(edit1.Text);
        5:                  //查询字段为日期类型
         strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+quotedstr(edit1.Text);
        6,7,8,9:            //查询字段为数值类型
         strSql:='select * from booksinf where '+arrInf[combobox2.ItemIndex-1]+combobox1.Text+edit1.Text;
       end;
       with  ADOQuery1 do  begin
          close;
          sql.Clear;
          sql.Add(strSql);
          Open;    //执行查询
       end;
     except
        showmessage('SQL语句错误：'+strSql);
        with  ADOQuery1 do  begin
          close;
          sql.Clear;
          sql.Add('select * from booksinf');
          Open;
        end;
     end
  end
  else begin
      with  ADOQuery1 do  begin
        close;
        sql.Clear;
        sql.Add('select * from booksinf');
        Open;    
      end;
  end;
end;

procedure Tadmin_bookinf.FormShow(Sender: TObject);
begin
  with  ADOQuery1 do begin
   close;
   sql.Clear;
   sql.Add('select * from booksinf');
   Open;
  end;
  //设置字段名列表
  arrInf:=TStringList.Create;
  arrInf.commatext:=('book_id,book_name,book_type,author,publisher,pub_date,borrow_num,booking_num,book_num,price,locality,mem');
end;

procedure Tadmin_bookinf.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  frmadmin.Visible :=true;
  ADOQuery1.close;
  arrInf.Free;  //释放arrInf
end;

procedure Tadmin_bookinf.btnAppendClick(Sender: TObject);
begin
    ADOQuery1.Append;
    frm_book_record.visible:=true;  //打开记录编辑窗体
    self.Enabled:=false;            //禁止对当前窗体操作
end;

procedure Tadmin_bookinf.btnDeleteClick(Sender: TObject);
var
  intYesNo:integer;
begin
  intYesNo:=MessageDlg('确定要删除当前记录？',mtWarning,[mbYes,mbNo],0);
  if intYesNo=IDYES then
    ADOQuery1.Delete;
end;

procedure Tadmin_bookinf.btnModifyClick(Sender: TObject);
begin
    frm_book_record.visible:=true;  //打开记录编辑窗体修改记录
    self.Enabled:=false;            //禁止对当前窗体操作
end;
end.
