📄 user_m.pas
字号:
if isSaved = true then
begin
panel2.Enabled:=true; //Panel3.Enabled:=false;
BitBtn2.Enabled:=True; BitBtn3.Enabled:=true;
BitBtn4.Enabled:=true; BitBtn7.Enabled:=true;
BitBtn5.Enabled:=false; BitBtn6.Enabled:=false;
edName2.Enabled:=false; edPwd1.Enabled:=false;
edPwd2.Enabled:=false; edAdress.Enabled:=false;
edit8.Enabled:=false; edit9.Enabled:=false;
edit10.Enabled:=false;
cbPost.Enabled:=false; cbSex2.Enabled:=false;
dateDTP.Enabled:=false;
//********************************************************
end
else
begin
panel2.Enabled:=false; //Panel3.Enabled:=true;
BitBtn2.Enabled:=false; BitBtn3.Enabled:=false;
BitBtn4.Enabled:=false; BitBtn7.Enabled:=false;
BitBtn5.Enabled:=true; BitBtn6.Enabled:=true;
edName2.Enabled:=true; edPwd1.Enabled:=true;
edPwd2.Enabled:=true; edAdress.Enabled:=true;
edit8.Enabled:=true; edit9.Enabled:=true;
edit10.Enabled:=true;
cbPost.Enabled:=true; cbSex2.Enabled:=true;
dateDTP.Enabled:=true;
//********************************************************
end;
end;
procedure TUserForm.BitBtn2Click(Sender: TObject);
var
i:integer;
begin
unenable(self);
for i := 0 to fcTreeView.Items.Count - 1 do
fcTreeView.Items[i].Checked := False;
ControlTheEnablePro(false);
clearText(self);
cbPost.ItemIndex := 0; cbSex2.ItemIndex := 0;
edName2.SetFocus;
flag:=1;
end;
procedure TUserForm.BitBtn4Click(Sender: TObject);
begin
ControlTheEnablePro(false);
unenable(self);
flag := 2;
end;
procedure TUserForm.BitBtn5Click(Sender: TObject);
begin
ControlTheEnablePro(true);
enable(self);
edNO.Enabled:=true;
edName1.Enabled:=true;
flag := 0;
end;
procedure TUserForm.BitBtn6Click(Sender: TObject);
var
Uno: string;
sqlStr: string;
begin
//*******************************************
if edName2.Text='' then
begin
if application.MessageBox('姓名不能为空','提示消息', mb_iconInformation+mb_ok)=idok then
edName2.SetFocus;
end
else
// if isName(edit3.Text)=false then
// begin
// application.MessageBox('姓名含有非法字符', '提示消息', MB_ICONINFORMATION);
// edit3.SetFocus;
// end
// else
// if isNameExist(edit3.Text) then
// begin
// Application.MessageBox('该姓名已存在', '提示消息', MB_ICONINFORMATION);
// edit3.SetFocus;
// end
// else
// if combobox2.ItemIndex < 1 then
// begin
// application.MessageBox('职位不能为空','提示消息', mb_iconInformation+mb_ok);
// combobox2.SetFocus;
// end
// else
// if Length(edit5.Text) < 6 then
// begin
// application.MessageBox('密码长度必须位六位以上','提示消息', mb_iconInformation+mb_ok);
// edit5.SetFocus;
// end
// else
if edPwd1.Text='' then
begin
application.MessageBox('密码不能为空','提示消息', mb_iconInformation+mb_ok);
edPwd1.SetFocus;
end
else
if edPwd2.Text = '' then
begin
application.MessageBox('重输密码不能为空','提示消息', mb_iconInformation+mb_ok);
edPwd2.SetFocus;
end
else
if edPwd2.Text <> edPwd1.Text then
begin
application.MessageBox('两次密码输入不相同','提示消息', mb_iconInformation+mb_ok);
edPwd2.SetFocus;
end
else
if(dateDTP.Date<1905-1-1) or (dateDTP.Date>Now) then
begin
application.MessageBox('出生日期不合法','提示消息', mb_iconInformation+mb_ok);
dateDTP.SetFocus;
end
else
// if(edit9.Text<>'') and (isFixedTel(edit9.Text)=false) then
// begin
// application.MessageBox('电话号码不合法','提示消息', mb_iconInformation+mb_ok);
// edit9.SetFocus;
//end
// else
if(edit10.Text <> '') and (isEmail(edit10.Text)=false) then
begin
application.MessageBox('电子邮件地址不合法','提示消息', mb_iconInformation+mb_ok);
edit10.SetFocus;
end
else
if(length(edit10.Text) > 30) then
begin
application.MessageBox('电子邮件地址长度不能超过30','提示消息', mb_iconInformation+mb_ok);
edit10.SetFocus;
end
else
if isTvChecked(fcTreeView)=false then
begin
application.MessageBox('必须分配角色','提示消息', mb_iconInformation+mb_ok);
fcTreeView.SetFocus;
end
else
begin
if flag=1 then // flag=1表示此时是增加状态
begin
if isUnameExist(edName2.Text) then
begin
if Application.MessageBox('用户名已存在,是否继续添加!', '提示消息',MB_ICONINFORMATION + MB_YESNO)= id_yes then
begin
adoqUser.Close;
Uno := GetUno; // 自动获取用户编号
sqlStr := 'INSERT TUser(Uno, UName, USex, UpassWord, UBirth,'
+ 'UPost, UPhone, UEmail, UAddress, UMemo)'
+ 'Values('+QuotedStr(Uno)+','+ QuotedStr(edName2.Text)+','
+ QuotedStr(GetDIdFromCombox(cbSex2,DM.ADOConnection1))+','
+ QuotedStr(edPwd1.Text)+','
+ QuotedStr(DateToStr(dateDTP.Date))+','
+ QuotedStr(GetDIdFromCombox(cbPost, DM.ADOConnection1)) + ','
+ QuotedStr(edit9.Text) + ',' + QuotedStr(edit10.Text)+','
+ QuotedStr(edAdress.Text) + ',' + QuotedStr(edit8.Text)
+')';
ToExecSQL(sqlStr, DM.ADOConnection1); //插入记录到用户表
SaveRnoFromTv(Uno); //保存树的记录到用户角色表
showmessage('添加成功!');
end
else
edName1.SetFocus;
end
else
begin
adoqUser.Close;
Uno := GetUno; // 自动获取用户编号
sqlStr := 'INSERT TUser(Uno, UName, USex, UpassWord, UBirth,'
+ 'UPost, UPhone, UEmail, UAddress, UMemo)'
+ 'Values('+QuotedStr(Uno)+','+ QuotedStr(edName2.Text)+','
+ QuotedStr(GetDIdFromCombox(cbSex2,DM.ADOConnection1))+','
+ QuotedStr(edPwd1.Text)+','
+ QuotedStr(DateToStr(dateDTP.Date))+','
+ QuotedStr(GetDIdFromCombox(cbPost, DM.ADOConnection1)) + ','
+ QuotedStr(edit9.Text) + ',' + QuotedStr(edit10.Text)+','
+ QuotedStr(edAdress.Text) + ',' + QuotedStr(edit8.Text)
+')';
ToExecSQL(sqlStr, DM.ADOConnection1); //插入记录到用户表
SaveRnoFromTv(Uno); //保存树的记录到用户角色表
showmessage('添加成功!');
end;
end;
if flag=2 then // flag=2表示此时是修改状态
begin
//更新TUser表
sqlStr := 'UPDATE TUser SET UName=' + QuotedStr(edName2.Text) +','
+ 'USex=' + QuotedStr(GetDIdFromCombox(cbSex2,DM.ADOConnection1)) +','
+ 'UpassWord=' + QuotedStr(edPwd1.Text) +','
+ 'UBirth=' + QuotedStr(DateToStr(dateDTP.date)) + ','
+ 'UPost=' + QuotedStr(GetDIdFromCombox(cbPost, DM.ADOConnection1)) +','
+ 'UPhone=' + QuotedStr(edit9.Text) + ','
+ 'UEmail=' + QuotedStr(edit10.Text)+','
+ 'UAddress=' + QuotedStr(edAdress.Text) + ','
+ 'UMemo=' + QuotedStr(edit8.Text)
+' WHERE UNo=' + QuotedStr(adoqUser.fieldByName('Uno').AsString);
ToExecSQL(sqlStr, DM.ADOConnection1);
//更新TUserRole表 ,先删记录,再插入
sqlStr := 'DELETE TUserRole WHERE UNo=' + QuotedStr(adoqUser.fieldByName('Uno').AsString);
ToExecSQL(sqlStr,dm.ADOConnection1);
SaveRnoFromTv(adoqUser.fieldByName('Uno').AsString);
showmessage('修改成功!');
end;
adoqUser.Close;
adoqUser.Open;
flag := 0;
ControlTheEnablePro(true);
enable(self);
edNO.Enabled:=true;
edName1.Enabled:=true;
end;
end;
procedure TUserForm.BitBtn7Click(Sender: TObject);
begin
close;
end;
procedure TUserForm.adoqUserAfterScroll(DataSet: TDataSet);
var
roleQry: TAdoquery;
i: integer;
begin
roleQry := TadoQuery.Create(nil);
roleQry.Connection := dm.ADOConnection1;
for i := 0 to fcTreeView.Items.Count - 1 do
begin
fcTreeView.Items[i].Checked := false;
end;
with adoqUser do
begin
edName2.Text := fieldByname('Uname').AsString;
edPwd1.Text := fieldByname('upassWord').AsString;
edPwd2.Text := fieldByname('uPassWord').AsString;
dateDTP.Date := StrToDate(fieldByName('UBirth').AsString);
edAdress.Text := fieldByname('UAddress').AsString;
edit8.Text := fieldByname('Umemo').AsString;
edit10.Text := fieldByname('UEmail').AsString;
edit9.Text := fieldByname('UPhone').AsString;
cbPost.ItemIndex := cbPost.Items.IndexOf(fieldbyname('post').AsString);
cbSex2.ItemIndex := cbSex2.Items.IndexOf(fieldByname('Sex').AsString);
end;
CheckTvByField(adoqUser, DM.ADOConnection1, fcTreeView);
roleQry.Free;
end;
procedure TUserForm.BitBtn3Click(Sender: TObject);
var
RoleQry: TadoQuery;
Sqlstr: String;
begin
if messagebox(handle,'您确定要修改吗?','提示',MB_YESNO + MB_ICONQUESTION)=ID_yes then
begin
RoleQry := TAdoQuery.Create(nil);
with RoleQry do
begin
Connection := DM.ADOConnection1;
sql.Text := 'SELECT c.Rno FROM TUser a '
+ 'JOIN TUserRole b On a.Uno=b.Uno '
+ 'JOIN TRole c On b.Rno=c.Rno '
+ 'WHERE a.Uno=' + QuotedStr(adoqUser.fieldByname('Uno').AsString);
Open;
end;
if(RoleQry.FieldByName('Rno').AsString = '004') then
application.MessageBox('系统管理员不能被删除!','警告', mb_IconError)
else
if(adoqUser.fieldByname('Uno').AsString)=DM.uno then
begin
application.MessageBox('不能删除自己!', '警告', mb_IconError);
end
else
begin
if application.MessageBox('确认删除', '提示信息', mb_IconInformation
+ mb_YesNo)=id_yes then
begin
//删除TUserRole里面的记录
Sqlstr := 'DELETE TUserRole WHERE UNo='
+ QuotedStr(adoqUser.fieldByname('Uno').AsString);
ToExecSQL(Sqlstr, DM.ADOConnection1);
//删除TUser里面的记录
Sqlstr := 'DELETE TUser WHERE UNo='
+ QuotedStr(adoqUser.fieldByname('Uno').AsString);
ToExecSQL(SqlStr, dm.ADOConnection1);
adoqUser.Close;
adoqUser.Open;
end;
end;
ControlTheEnablePro(true);
enable(self);
RoleQry.Free;
end;
end;
function TUserForm.GetUno: string;
var
UnoQry: TADOQuery;
No: string;
t: integer;
begin
UnoQry := TADOQuery.Create(self);
with Unoqry do
begin
Connection := dm.ADOConnection1;
sql.Text := 'SELECT MAX(Uno) as Muno FROM TUser';
Open;
No := fieldbyName('Muno').AsString;
end;
t := StrToInt(No);
t := t + 1000 + 1;
No := Copy(IntToStr(t), 2, 3);
Result := No;
UnoQry.Free;
end;
procedure TUserForm.SaveRnoFromTv(Uno: String);
var
i: integer;
sqlStr: string;
Node: TfcTreeNode;
begin
for i := 0 to fcTreeView.Items.Count - 1 do
begin
Node := fcTreeView.Items[i];
if Node.Checked then
begin
sqlStr := 'INSERT TUserRole(Uno, Rno) VALUES('
+ QuotedStr(Uno) + ',' + QuotedStr(Node.StringData)
+')';
ToExecSQL(sqlStr, DM.ADOConnection1);
end;
end;
end;
function TUserForm.isUnameExist(Uname: string): Boolean;
var
SqlStr: string;
tempQry: TADOQuery;
begin
sqlStr := ' SELECT * FROM TUser WHERE Uname=' + QuotedStr(edName2.Text);
tempQry := ToGetDataSQL(SqlStr, DM.adoconnection1);
if tempQry.RecordCount > 0 then
result := true
else
result := false;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -