📄 unit7.pas
字号:
unit Unit7;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls;
type
TForm7 = class(TForm)
Label1: TLabel;
Updatestuff: TButton;
Button2: TButton;
code: TEdit;
address: TEdit;
mobile: TEdit;
stuffname: TEdit;
age: TEdit;
telephone: TEdit;
mail: TEdit;
man: TRadioButton;
woman: TRadioButton;
partment: TComboBox;
starttime: TDateTimePicker;
endtime: TDateTimePicker;
remark: TMemo;
job: TComboBox;
jobstate: TComboBox;
Checkaddress: TCheckBox;
Checkdepart: TCheckBox;
Checkstate: TCheckBox;
Checkend: TCheckBox;
Checkremark: TCheckBox;
Checkage: TCheckBox;
Checkmobile: TCheckBox;
Checktel: TCheckBox;
Checkname: TCheckBox;
Checkjob: TCheckBox;
Checksex: TCheckBox;
Checkmail: TCheckBox;
Button1: TButton;
Checkstart: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure oninit();
procedure UpdatestuffClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form7: TForm7;
implementation
{$R *.dfm}
uses Unit2;
procedure TForm7.oninit;
var temp:string;
begin
man.Checked:=true; //初始选择男
//选择query_depart用来读取部门资料表内容,
//选择Query组件可以是任意的,只要不影响其他操作
with mydata.query_depart do
begin
close;//执行操作时先关闭查询组件
with sql do //设置SQL属性内容
begin
clear; //先清除SQL内容
add('select 部门名称 from 部门资料表');
end;
open;//打开数据集
first; //移到第一条记录
partment.Clear; //部门组合框内容清空
while (eof=false) do //启启遍历所有数据,eof表示Query组件表示的数据集结束
begin
//调用fieldbyname函数读取数集记录中某字段内容
temp:=fieldbyname('部门名称').AsString;
//住部门组合框中添加新的数据项
partment.Items.Append(temp);
next; //移动到下一条记示
end;
end;
with mydata.query_job do
begin
close;
with sql do
begin
clear;
add('select * from 职务类别表');
end;
open;
first;
job.Clear; //职务组合框清空
while (eof=false) do
begin
//读取职务记录
temp:=fieldbyname('职务').AsString;
job.Items.Append(temp);
next;
end;
end;
end;
procedure TForm7.Button1Click(Sender: TObject);
var sign:boolean;
begin
sign:=false;
with mydata.Query1 do
begin
close;
with sql do
begin
clear;
add('select * from 员工资料表 where 员工号=:code');
ParamByName('code').AsString:=trim(code.Text);
end;
open;
if findlast() then //判该工号是否存在于数据库中
begin
//如果存在,把员工资料表中的内容读出并显示在窗体各组件中
//利用FieldByName函数读取Query组件中保存的内容
stuffname.Text:=FieldByName('员工名').AsString;
if (FieldbyName('性别').AsInteger=1) then
begin
man.Checked:=true;
end else
begin
man.Checked:=false;
end;
age.Text:=FieldByName('年龄').AsString;
partment.Text:=FieldByName('部门').AsString;
job.Text:=FieldByName('职务').AsString;
starttime.DateTime:=FieldByName('合同开始时间').AsDateTime;
endtime.DateTime:=FieldByName('合同结束时间').AsDateTime;
address.Text:=FieldByName('住址').AsString;
telephone.Text:=FieldByName('联系电话').AsString;
mobile.Text:=FieldByName('手机').AsString;
mail.Text:=FieldByName('邮箱').AsString;
jobstate.Text:=FieldByname('工作状态').AsString;
remark.Text:=FieldByName('备注').AsString;
end else
begin
//如果工号输入不正确,则给出提示信息,让用户续继输入员工号
showmessage('员工号不正确');
end;
end;
end;
procedure TForm7.UpdatestuffClick(Sender: TObject);
var sign:boolean;
begin
sign:=false;//开始无更新选项
with mydata.Query1 do//使用Query1
begin
close;
with sql do//设置SQL属性
begin
clear;
add('update 员工资料表 set');//开始添加更新资料的SQL语句
if checkname.Checked then//更新员工名
begin
add(' 员工名=:name');
//添加更新员工名的SQL语句
ParamByName('name').AsString:=trim(stuffname.Text);
sign:=true;
end;
//更新性别资料
if checksex.Checked then
begin
if sign then
begin
//如果前面已有其他字段更新,则需要在赋值语句前加上“,”号
add(' ,');
end;
sign:=true;
add(' 性别=:sex');
if man.Checked then
begin
ParamByName('sex').AsInteger:=1;
end else
begin
ParamByName('sex').AsInteger:=0;
end;
end;
//更新年龄资料
if checkage.Checked then
begin
if sign then
begin
add(' ,');
end;
add(' 年龄=:age');
sign:=true;
ParamByName('age').AsString:=trim(age.Text);
end;
//更新部门资料
if checkdepart.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 部门=:depart');
ParamByName('depart').AsString:=trim(partment.Text);
end;
//更新职务资料
if checkjob.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 职务=:job');
ParamByName('job').AsString:=trim(job.Text);
end;
//更新合同开始时间资料
if checkstart.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 合同开始时间=:startdate');
ParamByName('startdate').AsString:=datetostr(starttime.Date);
end;
//更新合同结束时间资料
if checkend.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 合同结束时间=:endtime');
ParamByName('endtime').AsString:=datetostr(endtime.Date);
end;
//更新工作状态资料
if checkstate.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 工作状态=:state');
ParamByName('state').AsString:=trim(jobstate.Text);
end;
//更新地址资料
if checkaddress.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 住址=:address');
ParamByName('address').AsString:=trim(address.Text);
end;
//更新电话资料
if checktel.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 联系电话=:telephone');
ParamByName('telephone').AsString:=trim(telephone.Text);
end;
//更新手机号码资料
if checkmobile.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 手机=:mobile');
ParamByName('mobile').AsString:=trim(mobile.Text);
end;
//更新电子邮箱资料
if checkmail.Checked then
begin
if sign then
begin
add(' ,');
end;
sign:=true;
add(' 邮箱=:mail');
ParamByName('mail').AsString:=trim(mail.Text);
end;
//更新备注资料
if checkremark.Checked then
begin
if sign then
begin
add(' ,');
end;
add(' 备注=:remark');
ParamByName('remark').AsString:=trim(remark.Text);
end;
add(' where 员工号=:code');
ParamByName('code').AsString:=trim(code.Text);
end;
if sign then
begin
ExecSQL;//如果有更新选项,则执行更新语句
end else
begin
//如果没有更新选项,则不做任何工作
showmessage('没有更新选项');
end;
end;
with mydata.query_stuff do
begin
close;
open;
end;
end;
procedure TForm7.Button2Click(Sender: TObject);
begin
close;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -