www.pudn.com > xinshengruxue.rar > modiuser.pas


unit modiuser; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, Buttons, StdCtrls, ExtCtrls, DB, DBTables, CheckLst; 
 
type 
  Tmodiuser_Form = class(TForm) 
    Query1: TQuery; 
    GroupBox1: TGroupBox; 
    CheckListBox1: TCheckListBox; 
    Panel1: TPanel; 
    yhm_LEdit: TLabeledEdit; 
    mm_LEdit: TLabeledEdit; 
    BitBtn1: TBitBtn; 
    BitBtn2: TBitBtn; 
    procedure FormCreate(Sender: TObject); 
    procedure FormShow(Sender:TObject); 
    procedure FormClose(Sender: TObject;var Action:TCloseAction); 
    procedure BitBtn2Click(Sender: TObject); 
    procedure BitBtn1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  modiuser_Form: Tmodiuser_Form; 
 
implementation 
 
uses usergl, main; 
 
{$R *.dfm} 
 
procedure Tmodiuser_Form.FormCreate(Sender: TObject); 
var yhm,cdbh,cdmc,sqlstr:string; 
 
begin 
       if usergl_Form.modi then 
        begin 
        yhm_LEdit.Text:= usergl_Form.Query1.FieldByName('用户名').AsString; 
        mm_LEdit.Text:=usergl_Form.Query1.FieldByName('密码').AsString; 
       end; 
       Caption:=usergl_Form.xx; 
       Query1.SessionName:=Main_Form.Database1.SessionName; 
       Query1.DatabaseName:=Main_Form.Database1.DatabaseName; 
       with TQuery.Create(nil)do 
         begin 
         try 
           close; 
           SessionName:=Main_Form.Database1.SessionName; 
           databasename:=Main_Form.Database1.DatabaseName; 
           sqlstr:='select * from [cdx] order by  菜单编号'; 
           SQL.Clear; 
           SQL.Add(sqlstr); 
           Open; 
           First; 
           while not Eof do 
             begin 
               cdbh:=FieldByName('菜单编号').AsString; 
               cdmc:=FieldByName('菜单名').AsString; 
               CheckListBox1.Items.Add(cdbh+'&'+cdmc); 
               if usergl_Form.modi then 
               begin 
                yhm:=usergl_Form.Query1.FieldByName('用户名').AsString; 
                sqlstr:='select * from [qxb] where (用户名='''+yhm+''')and(菜单编号='''+cdbh+''')'; 
                Query1.Close; 
                Query1.SQL.Clear; 
                Query1.SQL.Add(sqlstr); 
                Query1.Open; 
                if Query1.RecordCount>0 then 
                CheckListBox1.Checked[CheckListBox1.Count-1]:=true; 
                end; 
               next; 
             end; 
             Finally 
             Free 
           end; 
         end; 
end; 
        
 
 
 
 
 
 
 
 
procedure Tmodiuser_Form.FormShow(Sender:TObject); 
begin 
 
   if usergl_Form.modi then 
     begin 
      yhm_LEdit.Text:=usergl_Form.Query1.FieldByName('用户名').AsString; 
      mm_LEdit.Text:=usergl_Form.Query1.FieldByName('密码').AsString; 
      end; 
 
end; 
 
 
procedure Tmodiuser_Form.FormClose(Sender: TObject;var Action:TCloseAction); 
  begin 
  Main_Form.Open_Form_number:=Main_Form.Open_Form_number-1; 
   if  Main_Form.Open_Form_number=0 then 
   Main_Form.N15.Enabled:=true; 
  Action:=caFree; 
End; 
 
 
 
procedure Tmodiuser_Form.BitBtn2Click(Sender: TObject); 
begin 
close; 
end; 
 
 
///////////////////////////////////////////////////// 
procedure Tmodiuser_Form.BitBtn1Click(Sender: TObject); 
 
var id,sqlstr1,sqlstr,cdbh,yhm,mm:string; 
  i:integer; 
begin 
  if usergl_Form.modi then 
   id:=usergl_Form.Query1.FieldByName('用户名').AsString; 
   yhm:=Trim(yhm_LEdit.Text); 
 
  if(Length(yhm)<3) or (Length(yhm)>20) then 
  begin 
    showmessage('用户名由3~20个字符组成'); 
    exit; 
  end; 
  mm:=Trim(mm_LEdit.Text); 
  if(Length(mm)<3) or (Length(mm)>20) then 
  begin 
    showmessage('密码由3~20个字符组成'); 
    exit; 
  end; 
 
 
 
  sqlstr1:='select * from [users] where (用户名='''+yhm+''')and (用户名!='''+id+''')'; 
  if usergl_Form.modi then 
      sqlstr:='update [users] set 用户名='''+yhm+''',密码='''+mm+''' where(用户名='''+id+''')' 
  else 
      sqlstr:='insert into [users] (用户名,密码) values ('''+yhm+''','''+mm+''')'; 
 
  with  TQuery.Create(nil) do 
  begin 
    try 
        SessionName:=Main_Form.Database1.SessionName; 
        databasename:=Main_Form.Database1.DatabaseName; 
        SQL.Clear; 
        SQL.Add(sqlstr1); 
        Open; 
        if RecordCount>0 then 
        begin 
          ShowMessage('该用户名已存在,请改用其他用户名!'); 
          exit; 
        end; 
        SQL.Clear; 
        SQL.Add(sqlstr); 
        ExecSQL; 
        for i:=0 to CheckListBox1.Count-1 do 
        begin 
          cdbh:=copy(CheckListBox1.Items.Strings[i],0,3); 
          if usergl_form.modi then 
          begin 
            if CHeckLIstBox1.Checked[i] then 
            begin 
               sqlstr:='select *from [qxb] where (用户名='''+yhm+''')and(菜单编号='''+cdbh+''')order by 菜单编号'; 
               SQl.Clear; 
               SQL.Add(sqlstr); 
               Open; 
               if RecordCount<=0 then 
               begin 
                  sqlstr:='insert into [qxb](用户名,菜单编号)values('''+yhm+''','''+cdbh+''')'; 
                  SQL.Clear; 
                  SQL.Add(sqlstr); 
                  ExecSQl; 
               end; 
            end 
            else 
            begin 
              sqlstr:='delete from [qxb] where(用户名='''+yhm+''')and(菜单编号='''+cdbh+''')'; 
              SQL.Clear; 
              SQL.Add(sqlstr); 
              ExecSQl; 
            end; 
         end 
         else 
         begin 
           if CHeckLIstBox1.Checked[i] then 
           begin 
               sqlstr:='insert into [qxb](用户名,菜单编号)values('''+yhm+''','''+cdbh+''')'; 
               SQL.Clear; 
               SQL.Add(sqlstr); 
               ExecSQl; 
           end; 
         end; 
      end; 
    finally 
    Free; 
    end; 
  end; 
  usergl_Form.refresh; 
  close; 
end; 
 
end.