📄 unit_author.pas
字号:
unit Unit_Author;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids, Buttons, Spin, DB, ADODB;
type
TfmAuthor = class(TForm)
Panel5: TPanel;
GroupBox1: TGroupBox;
Notebook1: TNotebook;
Edt_ProQry: TEdit;
ComboBox2: TComboBox;
ComboBox1: TComboBox;
RadioGroup1: TRadioGroup;
SpinEdit1: TSpinEdit;
Panel2: TPanel;
BitBtn_new: TBitBtn;
BitBtn_edit: TBitBtn;
BitBtn_del: TBitBtn;
BitBtn_ok: TBitBtn;
BitBtn_cancel: TBitBtn;
Panel1: TPanel;
Panel3: TPanel;
StringGrid1: TStringGrid;
Panel4: TPanel;
Label1: TLabel;
Label4: TLabel;
Edt_Name: TEdit;
BitBtn_qry: TBitBtn;
Label7: TLabel;
Label3: TLabel;
Com_Sex: TComboBox;
Mmo_Des: TMemo;
procedure FormCreate(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
procedure BitBtn_newClick(Sender: TObject);
procedure BitBtn_okClick(Sender: TObject);
procedure BitBtn_delClick(Sender: TObject);
procedure BitBtn_qryClick(Sender: TObject);
procedure BitBtn_editClick(Sender: TObject);
private
{ Private declarations }
procedure IniStringGrid;
procedure SetEnable(zt: integer);
procedure NewRecord;
procedure SaveRecord;
procedure DeleteRecordFromDB(row: integer);
procedure DeleteRecordFromSG(row: integer);
public
{ Public declarations }
end;
var
fmAuthor: TfmAuthor;
implementation
{$R *.dfm}
var
EditZT, ModifyRow: integer;
procedure TfmAuthor.IniStringGrid;
var
i: integer;
begin
StringGrid1.ColWidths[4] := 0;
StringGrid1.ColWidths[5] := 0;
StringGrid1.Cells[0,0] := '姓名';
StringGrid1.Cells[1,0] := '性别';
StringGrid1.Cells[2,0] := '备注';
StringGrid1.Cells[3,0] := '编号';
for i := 1 to StringGrid1.RowCount -1 do
begin
StringGrid1.Cells[0,i] := '';
StringGrid1.Cells[1,i] := '';
StringGrid1.Cells[2,i] := '';
StringGrid1.Cells[3,i] := '';
StringGrid1.Cells[4,i] := '';
StringGrid1.Cells[5,i] := '';
end;
StringGrid1.RowCount := 2;
end;
procedure TfmAuthor.FormCreate(Sender: TObject);
begin
IniStringGrid;
EditZT := 0; //初始状态为查询
SetEnable(EditZT);
end;
procedure TfmAuthor.StringGrid1Click(Sender: TObject);
var
RowIndex: integer;
begin
if EditZT <> 0 then exit; //如果当前状态不是查询
RowIndex := StringGrid1.Row;
if StringGrid1.Cells[3,RowIndex] = '' then
begin
Edt_Name.Text := '';
Com_Sex.ItemIndex := -1;
Mmo_Des.Text := '';
Exit;
end;
Edt_Name.Text := StringGrid1.Cells[0,RowIndex];
Com_Sex.ItemIndex := Com_Sex.Items.IndexOf(StringGrid1.Cells[1,RowIndex]);
Mmo_Des.Text := StringGrid1.Cells[2,RowIndex];
end;
procedure TfmAuthor.SetEnable(zt: integer);
begin
if zt = 0 then //查询状态
begin
Panel5.Enabled := true;
BitBtn_new.Enabled := true;
BitBtn_edit.Enabled := true;
BitBtn_del.Enabled := true;
BitBtn_ok.Enabled := false;
BitBtn_cancel.Enabled := false;
end
else
if (zt = 1) or (zt = 2) then
begin
Panel5.Enabled := false;
BitBtn_new.Enabled := false;
BitBtn_edit.Enabled := false;
BitBtn_del.Enabled := false;
BitBtn_ok.Enabled := true;
BitBtn_cancel.Enabled := true;
end;
end;
procedure TfmAuthor.BitBtn_newClick(Sender: TObject);
begin
EditZT := 1; //代表新增
SetEnable(EditZT);
Edt_Name.Text := '';
Com_Sex.Enabled := true;
Com_Sex.ItemIndex := -1;
Mmo_Des.lines.Clear;
Edt_Name.SetFocus;
end;
procedure TfmAuthor.BitBtn_okClick(Sender: TObject);
begin
if (Trim(Edt_Name.Text)='') then
begin
Application.MessageBox('作者姓名不能为空!','提示',MB_OK+MB_IconInformation);
Exit;
end;
if Com_Sex.Text = '' then
begin
Application.MessageBox('作者性别不能为空!','提示',MB_OK+MB_IconInformation);
Exit;
end;
if EditZT = 1 then //新增
begin
NewRecord;
Edt_ProQry.Text:='';
BitBtn_qry.OnClick(self);
end
else
if EditZT = 2 then //修改
begin
DeleteRecordFromDB(ModifyRow);
NewRecord;
StringGrid1.Cells[0,ModifyRow] := Edt_Name.Text;
StringGrid1.Cells[1,ModifyRow] := Com_Sex.Text;
StringGrid1.Cells[2,ModifyRow] := Mmo_Des.Text;
end;
EditZT := 0; //代表查询
SetEnable(EditZT);
end;
procedure TfmAuthor.NewRecord;
var
connQuery,connQuery1: TADOQuery;
connAdo: TADOConnection;
connString: string;
sFilePath: string;
i,maxrecord: integer;
ssql:string;
begin
sFilePath := ExtractFilePath(Application.ExeName);
connAdo := TADOConnection.Create(nil);
connQuery := TADOQuery.Create(nil);
connQuery.CommandTimeout := 60;
connQuery.Connection := connAdo;
connQuery1 := TADOQuery.Create(nil);
connQuery1.CommandTimeout := 60;
connQuery1.Connection := connAdo;
connString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sFilePath+'database.mdb';
try
connAdo.ConnectionString:=connString;
connAdo.Provider := 'Microsoft.Jet.OLEDB.4.0';
connAdo.LoginPrompt:=False;
connAdo.Connected:=True;
except
end;
ssql:='insert into 作者表 (AuthorName,Sex,Remark)values('''+Edt_Name.Text+
''','''+Com_Sex.Text+''','''+Mmo_Des.Text+''')';
connQuery.SQL.Clear;
connQuery.SQL.Add(ssql);
connQuery.ExecSQL;
end;
procedure TfmAuthor.SaveRecord;
var
connQuery,connQuery1: TADOQuery;
connAdo: TADOConnection;
connString: string;
sFilePath: string;
i: integer;
ssql,ID:string;
begin
sFilePath := ExtractFilePath(Application.ExeName);
connAdo := TADOConnection.Create(nil);
connQuery := TADOQuery.Create(nil);
connQuery.CommandTimeout := 60;
connQuery.Connection := connAdo;
connQuery1 := TADOQuery.Create(nil);
connQuery1.CommandTimeout := 60;
connQuery1.Connection := connAdo;
connString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sFilePath+'database.mdb';
try
connAdo.ConnectionString:=connString;
connAdo.Provider := 'Microsoft.Jet.OLEDB.4.0';
connAdo.LoginPrompt:=False;
connAdo.Connected:=True;
except
end;
ID:=StringGrid1.Cells[3,StringGrid1.Row];
ssql:='update 作者表 set AuthorName='''+Edt_Name.Text+''',Sex='''+Com_Sex.Text+
''',Remark='''+Mmo_Des.Text+''' where ID='+ID;
connQuery.SQL.Clear;
connQuery.SQL.Add(ssql);
connQuery.ExecSQL;
end;
procedure TfmAuthor.BitBtn_delClick(Sender: TObject);
begin
if Application.MessageBox('是否确定删除此记录?','提示',MB_OKCANCEL+MB_IconQuestion)= ID_OK then
begin
DeleteRecordFromDB(ModifyRow);
DeleteRecordFromSG(StringGrid1.Row);
StringGrid1.OnClick(StringGrid1);
end;
end;
procedure TfmAuthor.DeleteRecordFromDB(row: integer);
var
connQuery: TADOQuery;
connAdo: TADOConnection;
connString: string;
sFilePath: string;
ssql: string;
i: integer;
begin
sFilePath := ExtractFilePath(Application.ExeName);
connAdo := TADOConnection.Create(nil);
connQuery := TADOQuery.Create(nil);
connQuery.CommandTimeout := 60;
connQuery.Connection := connAdo;
connString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sFilePath+'database.mdb';
try
connAdo.ConnectionString:=connString;
connAdo.Provider := 'Microsoft.Jet.OLEDB.4.0';
connAdo.LoginPrompt:=False;
connAdo.Connected:=True;
except
end;
ssql:='delete * from 作者表 where ID='+StringGrid1.Cells[3,StringGrid1.row];
connQuery.SQL.Clear;
connQuery.SQL.Add(ssql);
connQuery.ExecSQL;
end;
procedure TfmAuthor.DeleteRecordFromSG(row: integer);
var
i, j: integer;
begin
for i := row to StringGrid1.RowCount - 2 do
begin
for j := 0 to StringGrid1.ColCount - 1 do
begin
StringGrid1.Cells[j, i] := StringGrid1.Cells[j, i+1];
end;
end;
for j := 0 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[j, StringGrid1.RowCount - 1]:='';
if StringGrid1.RowCount > 2 then
StringGrid1.RowCount := StringGrid1.RowCount - 1;
end;
procedure TfmAuthor.BitBtn_qryClick(Sender: TObject);
var
connQuery: TADOQuery;
connAdo: TADOConnection;
connString: string;
sFilePath,CreateSqlStr: string;
i: integer;
begin
sFilePath := ExtractFilePath(Application.ExeName);
connAdo := TADOConnection.Create(nil);
connQuery := TADOQuery.Create(nil);
connQuery.CommandTimeout := 60;
connQuery.Connection := connAdo;
connString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sFilePath+'database.mdb';
try
connAdo.ConnectionString:=connString;
connAdo.Provider := 'Microsoft.Jet.OLEDB.4.0';
connAdo.LoginPrompt:=False;
connAdo.Connected:=True;
except
end;
IniStringGrid;
// showmessage(CreateSqlStr);
CreateSqlStr:='select AuthorName, Sex, Remark,ID from 作者表 where AuthorName like '+'"%'+Edt_ProQry.Text+'%"';
with connQuery do
begin
close;
sql.Clear;
sql.Add(CreateSqlStr);
open;
if recordcount = 0 then
begin
showmessage('没有记录');
exit;
end;
first;
i := 1;
while not eof do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[0,i] := Fields[0].AsString;
StringGrid1.Cells[1,i] := Fields[1].AsString;
StringGrid1.Cells[2,i] := Fields[2].AsString;
StringGrid1.Cells[3,i] := Fields[3].AsString;
Inc(i);
next;
end;
StringGrid1.RowCount := StringGrid1.RowCount - 1;
close;
end;
StringGrid1.Row := 1;
StringGrid1.OnClick(StringGrid1);
end;
procedure TfmAuthor.BitBtn_editClick(Sender: TObject);
begin
EditZT := 2; //代表修改
SetEnable(EditZT);
ModifyRow := StringGrid1.Row;
Edt_Name.SetFocus;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -