如何用delphi做信息管理系统登陆窗口

    最近初学delphi,用它做了一个图书馆信息管理系统,现在把登陆窗口代码发上来,希望对初学者能少许帮助.
    先把界面发上来,如下图所示:
   
   步骤:
    1.新建一个窗体,在窗体中放置上图所示组建.
    2.连接数据库.选中ADOQuery1组建,点击左侧ConnectionString属性右侧的省略号,在弹出对话框中点击Build,再在弹出框中选择Microsoft OLE DB Provider for SQL Sever,点击"下一步",在新弹出的窗口中选择"使用Windows NT 继承安全设置"(当然,如果你的SQL安装时有用户名和密码则选择下面那个),然后在服务器上数据库下拉框中选择你要连接的数据库,最后一路确定即可.其他选项暂时可不用管它.
   3.写代码.双击"登陆",写如下代码
   procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  try
    with adoquery1 do
    begin
      close;
      sql.clear;
      sql.add('select * from user_master where 用户名=:a and 密码=:b and 权限=:c');
      parameters.ParamByName('a').Value:=trim(combobox1.Text);
      parameters.ParamByName('b').Value:=trim(edit1.Text);
      if combobox1.Text ='' then
        begin
          application.MessageBox('请输入用户名','提示信息',64);
          combobox1.SetFocus;
          exit;
        end;
      if edit1.Text ='' then
        begin
          application.MessageBox('请输入密码','提示信息',64);
          edit1.SetFocus;
          exit;
        end;
      if radiobutton1.Checked=true then
        begin
          Quanxian:='1';
        end;
      if radiobutton2.Checked=true then
        begin
          Quanxian:='0';
        end;
      parameters.ParamByName('c').Value:=trim(quanxian);
      open;
      end;
      if adoquery1.RecordCount<>0 then
        begin
          Username:=combobox1.Text;
          Password:=edit1.Text;
          application.MessageBox('登陆成功','提示信息',64);
          form2.show;
          self.Hide;
        end
      else
        application.MessageBox('输入的用户名或密码错误','提示信息',64);
  except
    application.MessageBox('登陆失败','提示信息',64);
  end;
end;
 
    4.为了让用户在第一次使用管理系统时数据库文件可自动附加到SQL服务器中,可双击窗体空白部分,加入一下代码:
    procedure TForm1.FormCreate(Sender: TObject);
var
   ADOCommand:TADOCommand;
   s,DataPath : string;
begin
   adoConnection1:=TADOConnection.Create(nil);
   adoConnection1.ConnectionString:='Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=library';
   adoConnection1.LoginPrompt:=false;
   try
     adoConnection1.Connected:=true;
   except
     ADOCommand:=TADOCommand.Create(nil);
     ADOCommand.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False';
     DataPath:=ExtractFilePath(Application.ExeName) ;
     s:='EXEC sp_attach_db @dbname = N'+char(39)+'library'+char(39)+','+
        '@filename1 = N'+char(39)+DataPath+'library_Data.MDF'+char(39)+
          ','+'@filename2 = N'+char(39)+DataPath+'library_Log.LDF'+char(39);
     ADOCommand.CommandText := s;
     ADOCommand.Execute();
   end;
end;
   

你可能感兴趣的:(职场,Delphi,休闲,信息管理系统登陆窗口)