📄 bookinfo.pas
字号:
unit bookinfo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, baseform, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ExtDlgs,ADODB;
type
Tf_bookinfo = class(Tf_baseform)
Label1: TLabel;
Panel1: TPanel;
Label2: TLabel;
Bookname: TEdit;
Panel3: TPanel;
Add: TButton;
Update: TButton;
Delete: TButton;
Cancel: TButton;
Label6: TLabel;
Field: TComboBox;
Label7: TLabel;
Value: TEdit;
Query: TButton;
Label3: TLabel;
Barcode: TEdit;
Label4: TLabel;
Author: TEdit;
Label5: TLabel;
Shortname: TEdit;
Label8: TLabel;
Memo: TEdit;
Label9: TLabel;
Price: TEdit;
Label10: TLabel;
cover: TImage;
Label11: TLabel;
Label12: TLabel;
Concern: TEdit;
Kind: TComboBox;
Grid1: TDBGrid;
Source1: TDataSource;
Brown: TButton;
Open1: TOpenPictureDialog;
procedure BooknameKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure BarcodeKeyPress(Sender: TObject; var Key: Char);
procedure PriceKeyPress(Sender: TObject; var Key: Char);
procedure CancelClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure QueryClick(Sender: TObject);
procedure Grid1CellClick(Column: TColumn);
procedure BrownClick(Sender: TObject);
procedure AddClick(Sender: TObject);
procedure DeleteClick(Sender: TObject);
procedure UpdateClick(Sender: TObject);
private
{ Private declarations }
public
Function InfoIsNull: Boolean;
Function BookIsMore(bar: String): Boolean;//判断书籍是否存在
{ Public declarations }
end;
var
f_bookinfo: Tf_bookinfo;
implementation
uses data;
{$R *.dfm}
procedure Tf_bookinfo.BooknameKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
inherited;
if Key = vk_Return then
FindNext(True);
end;
procedure Tf_bookinfo.BarcodeKeyPress(Sender: TObject; var Key: Char);
begin
inherited;
if not (Key in ['0'..'9',#8]) then
Key := #0;
end;
procedure Tf_bookinfo.PriceKeyPress(Sender: TObject; var Key: Char);
begin
inherited;
if not (Key in ['0'..'9','.',#8]) then
Key := #0
else if (Key = '.')and(Pos(Key,Trim(Price.Text))<>0) then
Key := #0;
end;
procedure Tf_bookinfo.CancelClick(Sender: TObject);
var
i: Integer;
begin
inherited;
For i :=0 to Panel1.ControlCount-1 do
if Panel1.Controls[i] is TEdit then
TEdit(Panel1.Controls[i]).Clear
else if Panel1.Controls[i]is TComboBox then
TComboBox(Panel1.Controls[i]).ItemIndex := 0;
Cover.Picture := Nil;
With t_Data.book do
begin
Close;
SQL.Clear;
SQL.Add('Select * From tb_bookinfo');
Open;
end;
if t_data.book.RecordCount>0 then
Source1.DataSet := t_data.book
else
begin
Source1.DataSet := Nil;
t_data.book.Close;
end;
end;
procedure Tf_bookinfo.FormShow(Sender: TObject);
begin
inherited;
With t_data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add('select kinds from tb_bookkinds');
Open;
end;
if t_data.Query1.RecordCount>0 then
begin
while Not t_data.Query1.Eof do
begin
Kind.Items.Add(Trim(t_data.Query1.Fields[0].Value));
t_data.Query1.Next;
end;
Kind.ItemIndex := 0;
end;
Cancel.Click;
end;
procedure Tf_bookinfo.QueryClick(Sender: TObject);
begin
inherited;
if (Field.Text<>'')and(Trim(Value.Text)<>'')then
begin
With t_data.book do
begin
Close;
SQL.Clear;
SQL.Add('select * From tb_bookinfo where ');
if Field.ItemIndex =0 then
SQL.Add('bookname = :FieldValue')
else
SQL.Add('shortname = :FieldValue');
Parameters.ParamByName('FieldValue').Value := Trim(Value.Text);
Open;
end;
if t_data.book.RecordCount<1 then
begin
Application.MessageBox('没有找到符合条件的记录.','提示',64);
Cancel.Click;
end;
end;
end;
procedure Tf_bookinfo.Grid1CellClick(Column: TColumn);
begin
inherited;
if Source1.DataSet.active then
begin
Bookname.Text := Trim(Source1.DataSet.FieldByName('bookname').AsString);
Shortname.Text := Trim(Source1.DataSet.FieldByName('Shortname').AsString);
Concern.Text := Trim(Source1.DataSet.FieldByName('Bookconcern').AsString);
Author.Text := Trim(Source1.DataSet.FieldByName('author').AsString);
Price.Text := Trim(Source1.DataSet.FieldByName('Price').AsString);
Kind.ItemIndex := Kind.Items.IndexOf(Trim(Source1.DataSet.FieldByName('kind').AsString));
Barcode.Text := Trim(Source1.DataSet.FieldByName('barcode').AsString);
Memo.Text := Trim(Source1.DataSet.FieldByName('Memo').AsString);
if Source1.DataSet.FieldByName('cover').Value <> '' then
Cover.Picture.Assign(Source1.DataSet.FieldByName('cover'))
else
Cover.Picture := Nil;
end;
end;
procedure Tf_bookinfo.BrownClick(Sender: TObject);
begin
inherited;
if Open1.Execute then
Cover.Picture.LoadFromFile(Open1.FileName);
end;
function Tf_bookinfo.InfoIsNull: Boolean;
var
i: Integer;
begin
Result := False;
For i := 0 to Panel1.ControlCount-1 do
if Panel1.Controls[i]is TEdit then
begin
if (Trim(TEdit(Panel1.Controls[i]).Text)='')and(TEdit(Panel1.Controls[i]).Name<>'memo') then
begin
Result := True;
Break;
end;
end
else if Panel1.Controls[i] is TComboBox then
begin
if Trim(TComboBox(Panel1.Controls[i]).Text)='' then
begin
Result := True;
Break;
end;
end;
end;
function Tf_bookinfo.BookIsMore(bar: String): Boolean;
begin
Result := False;
With t_data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add('select * from tb_bookinfo where barcode = :barcode');
Parameters.ParamByName('barcode').Value := Trim(bar);
Open;
end;
if t_data.Query1.RecordCount>0 then
Result := True;
t_data.Query1.Close;
end;
procedure Tf_bookinfo.AddClick(Sender: TObject);
begin
inherited;
if InfoIsNull = False then
begin
if BookIsMore(Barcode.Text)=False then
begin
Try
With t_data.Insert1 do
begin
Close;
SQL.Clear;
SQL.Add('Insert into tb_bookinfo Values (:bookname,:shortname,:barcode,:bookconcern,:author,:price,:kind,:cover,:memo)');
Parameters.ParamByName('bookname').Value := Trim(Bookname.Text);
Parameters.ParamByName('shortname').Value := Trim(Shortname.Text);
Parameters.ParamByName('barcode').Value := Trim(Barcode.Text);
Parameters.ParamByName('bookconcern').Value := Trim(Concern.Text);
Parameters.ParamByName('author').Value := Trim(Author.Text);
Parameters.ParamByName('price').Value := StrToFloat(Price.Text);
Parameters.ParamByName('bookname').Value := Trim(Bookname.Text);
Parameters.ParamByName('kind').Value := Trim(Kind.Text);
Parameters.ParamByName('cover').Assign(Cover.Picture);
Parameters.ParamByName('memo').Value := Trim(Memo.Text);
ExecSQL;
end;
Application.MessageBox('操作成功.','提示',64);
Except
Application.MessageBox('系统出错.','提示',64);
End;
Cancel.Click;
end
else
Application.MessageBox('该图书信息已经存在.','提示',64);
end
else
Application.MessageBox('图书信息不能为空','提示',64);
end;
procedure Tf_bookinfo.DeleteClick(Sender: TObject);
begin
inherited;
if t_data.book.Active then
begin
if Application.MessageBox('确实要删除当前图书信息吗?','提示',mb_yesno)= ID_Yes then
begin
Try
t_data.book.Delete;
Cancel.Click;
Application.MessageBox('删除成功.','提示',64);
Except
Application.MessageBox('操作失败.','提示',64);
End;
end;
end
else
Application.MessageBox('当前没有可删除的信息.','提示',64);
end;
procedure Tf_bookinfo.UpdateClick(Sender: TObject);
var
Connect: TADOConnection;
Query1: TADOQuery;
begin
inherited;
Connect := Nil;
Query1 := NIl;
if t_data.Book.Active then
begin
if InfoIsNull = False then
begin
if Application.MessageBox('确实要修改当前图书信息吗?','提示',mb_yesno)= id_yes then
begin
if Trim(Barcode.Text)<>Trim(t_data.book.FieldByName('barcode').AsString) then
begin
With t_data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add('select * from tb_bookinfo where barcode = :barcode');
Parameters.ParamByName('barcode').Value := Trim(Barcode.Text);
Open;
end;
if t_data.Query1.RecordCount>0 then
begin
Application.MessageBox('该图书已经存在.','提示',64);
Exit;
end;
end;
Try
Connect := TADOConnection.Create(nil);
Connect.LoginPrompt := False;
Connect.ConnectionString := t_data.Connection1.ConnectionString;
Connect.Open;
Query1 := TADOQuery.Create(nil);
Query1.Connection := Connect;
Try
Connect.BeginTrans;
With Query1 do
begin
Close;
SQL.Clear;
SQL.Add('Update tb_bookinfo set bookname = :bookname,shortname = :shortname,barcode = :barcode,bookconcern = :bookconcern,author = :author,price = :price,kind = :kind,memo = :memo');
SQL.Add('where barcode = :bar');
Parameters.ParamByName('bookname').Value := Trim(Bookname.Text);
Parameters.ParamByName('shortname').Value := Trim(Shortname.Text);
Parameters.ParamByName('barcode').Value := Trim(Barcode.Text);
Parameters.ParamByName('bookconcern').Value := Trim(concern.Text);
Parameters.ParamByName('author').Value := Trim(Author.Text);
Parameters.ParamByName('price').Value := StrToFloat(Price.Text);
Parameters.ParamByName('kind').Value := Trim(Kind.Text);
Parameters.ParamByName('Memo').Value := Trim(Memo.Text);
Parameters.ParamByName('bar').Value := Trim(t_data.book.FieldByName('barcode').AsString);
ExecSQL;
end;
if cover.Picture<>nil then
begin
With Query1 do
begin
Close;
SQL.Clear;
SQL.Add(' DECLARE @ptrval binary(16) SELECT @ptrval = TEXTPTR(cover) FROM tb_bookinfo where barcode = :bar ');
SQL.Add('WRITETEXT tb_bookinfo.cover @ptrval :image');
Parameters.ParamByName('bar').Value := Trim(barcode.Text);
Parameters.ParamByName('Image').Assign(Cover.Picture.Graphic);
ExecSQL;
end;
end;
Connect.CommitTrans;
Application.MessageBox('操作成功.','提示',64);
Except
Connect.RollbackTrans;
Application.MessageBox('操作失败.','提示',64);
End;
Finally
Connect.Free;
Query1.Free;
end;
Cancel.Click;
end;
end
else
Application.MessageBox('信息不能为空.','提示',64);
end
else
Application.MessageBox('当前没有可修改的信息.','提示',64);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -