⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginserver.pas

📁 物流管理系统是一个典型的数据库应用程序
💻 PAS
字号:

unit loginserver;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frame, StdCtrls,ExtCtrls, Buttons,inifiles;

type
  Tf_Loginserver = class(Tf_frame)
    Label1: TLabel;
    Servername: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Login: TBitBtn;
    Cancel: TBitBtn;
    Bevel1: TBevel;
    Image1: TImage;
    Dialog1: TOpenDialog;
    Label5: TLabel;
    DatabaseName: TEdit;
    Username: TEdit;
    Password: TEdit;
    procedure CancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ServernameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure LoginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  f_Loginserver: Tf_Loginserver;
Const
  server = '服务器';
  database = '数据库';
  user = '用户名';
  pass = '口令';
implementation
  uses ComObj,data;
{$R *.dfm}

procedure Tf_Loginserver.CancelClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure Tf_Loginserver.FormCreate(Sender: TObject);
var
  i: Integer;
  server,sql: Variant;
begin
  inherited;
   Try  //列举局域网中的SQL服务器
    Server := CreateOleObject('SQLDMO.Application');
    sql := server.ListAvailableSQLServers;
    For i := 1 to sql.Count-1 do
      if Trim(sql.Item(i))<>'' then
        ServerName.Items.Add(Trim(sql.Item(i)));
    ServerName.ItemIndex := 0;
  Finally
    server := Null;
    sql := null;
  End;
end;

procedure Tf_Loginserver.ServernameKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  if Key = VK_Return then
    FindNext(True);
end;
//处理登录按钮的单击事件
procedure Tf_Loginserver.LoginClick(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := Nil;
  inherited;
  Try
    Try
      t_Data.Connection1.Close;
      t_data.Connection1.ConnectionString := 'Provider=SQLOLEDB.1;Password='+Trim(Password.Text)+';Persist Security Info=True;User ID='+Trim(Username.Text)+';Initial Catalog= Master'+';Data Source='+Trim(Servername.Text);
      t_data.Connection1.Open;
      With t_Data.Query1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select * from sysdatabases where name = :name');
        Parameters.ParamByName('Name').Value := DatabaseName.Text;
        Open;
      end;
      if t_Data.Query1.RecordCount<1 then
      begin
        if Application.MessageBox(Pchar(Servername.Text+'服务器中没有安装程序所需的数据库,是否在该服务器中安装数据库?'),'提示',mb_yesno)= ID_Yes then
        begin
          //判断数据库文件是否在指定的路径下
          if FileExists(ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Data.MDF')and
             FileExists(ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Log.LDF')then
          begin
            Try
              With t_Data.Query1 do
              begin
                Close;
                SQL.Clear;
                SQL.Add(' Exec sp_attach_db  @dbname = :a , @filename1 = :b ,@filename2 = :c' );
                Parameters.ParamByName('a').Value := DatabaseName.Text;
                Parameters.ParamByName('b').Value := ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Data.MDF';
                Parameters.ParamByName('c').Value := ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Log.LDF';
                ExecSQL;
              end;
              Application.MessageBox('数据库安装成功.','提示',64);
            Except
              Application.MessageBox('数据库安装失败.','提示',64);
              Exit;
            end;
          end
          else
          if Dialog1.Execute then
            if Dialog1.Files.Count>1 then
            begin
              Try
                With t_Data.Query1 do
                begin
                  Close;
                  SQL.Clear;
                  SQL.Add(' Exec sp_attach_db  @dbname = :a , @filename1 = :b ,@filename2 = :c' );
                  Parameters.ParamByName('a').Value := DatabaseName.Text;
                  Parameters.ParamByName('b').Value := Dialog1.Files[0];
                  Parameters.ParamByName('c').Value := Dialog1.Files[1];
                  ExecSQL;
                end;
                Application.MessageBox('数据库安装成功.','提示',64);
              Except
                Application.MessageBox('数据库安装失败.','提示',64);
                Exit;
              end;
            end
            else
            begin
              Application.MessageBox('请至少选择2个文件.','提示',64);
              Exit;
            end;
        end;
      end;
      t_Data.Connection1.Close;
      t_data.Connection1.ConnectionString := 'Provider=SQLOLEDB.1;Password='+Trim(Password.Text)+';Persist Security Info=True;User ID='+Trim(Username.Text)+';Initial Catalog='+Trim(Databasename.Text)+';Data Source='+Trim(Servername.Text);
      t_data.Connection1.Open;
      //验证数据库是否合法
      with t_data.Query1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('select * from sysobjects where xtype = ''u'' and name = ''tb_storeserviceID'' ');
        open;
      end;
      if t_data.Query1.RecordCount <1 then //数据库同名,但不是本系统使用的数据库
      begin
        Application.MessageBox('该数据库已经存在,请重新设置数据库名称.','提示',64);
        DatabaseName.SelectAll;
        Databasename.SetFocus;
        Exit;
      end;
    Except
      Application.MessageBox('数据库连接失败.','提示',64);
      Exit;
    end;
    //利用Ini文件记录数据库配置信息
    Ini := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Login.ini');
    Ini.WriteString('配置信息',server,Trim(Servername.Text));
    Ini.WriteString('配置信息',database,Trim(Databasename.Text));
    Ini.WriteString('配置信息',user,Trim(Username.Text));
    Ini.WriteString('配置信息',pass,Trim(Password.Text));
    Application.MessageBox('数据库连接成功.','提示',64);
    Close;
  Finally
    Ini.Free;
  End;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -