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

📄 ufrmserver.pas

📁 数据库的附加
💻 PAS
字号:
unit UfrmServer;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, DB, ADODB, Buttons,Share;

type
  TFrm_Server = class(TForm)
    Panel1: TPanel;
    Image1: TImage;
    Notebook1: TNotebook;
    Panel2: TPanel;
    ConSetup: TADOConnection;
    BitBtn2: TBitBtn;
    qySetup: TADOQuery;
    BitBtn3: TBitBtn;
    BitBtn1: TBitBtn;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Edit3: TEdit;
    Edit2: TEdit;
    Label2: TLabel;
    RadioGroup1: TRadioGroup;
    Edit1: TEdit;
    Edit4: TEdit;
    Btn_v: TSpeedButton;
    Qy_V: TADOQuery;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure Btn_vClick(Sender: TObject);
  private
    { Private declarations }
  public
  sName:string;
    { Public declarations }
  end;
  const
    RegPath='\Software\NewsumSoft\PowerManager\';
var
  Frm_Server: TFrm_Server;
  myShare:TShare;
  bWriteReg:Boolean;
implementation

{$R *.dfm}

procedure TFrm_Server.BitBtn2Click(Sender: TObject);
var
  ConStr:String;
begin
    if Edit3.Text='' then
      begin
        MessageBox(Application.Handle,'请输入数据库实例名','提示',48);
        Edit3.SetFocus;
        Exit;
      end;
    ConStr:='Provider=SQLOLEDB.1;Persist Security Info=False;'
           +'User ID=sa;Password="'+Edit2.Text+'";'
           +'Initial Catalog=master;Data Source='+edit3.Text;
    ConSetup.Connected:=False;
    ConSetup.ConnectionString:=ConStr;
  try
    ConSetup.Connected:=True;
    application.MessageBox('数据库连接正确','系统提示',48);
    RadioGroup1.Enabled:=true;
    Btn_v.Enabled:=true;
    BitBtn3.Enabled:=True;
    Edit2.ReadOnly:=True;
    edit3.ReadOnly:=true;
    BitBtn2.Enabled:=False;
  except
    application.MessageBox('数据库连接不正确,密码是否正确','系统提示',48);
    exit;
  end;
end;

procedure TFrm_Server.BitBtn3Click(Sender: TObject);
var
  S_Name:string;
begin
  if RadioGroup1.ItemIndex=-1 then
  showmessage('请先选择所安装的 SQL Server 的版本!');
  case RadioGroup1.ItemIndex of
  0:    //SQL Server 7.0
    begin
       if Edit1.Text='' then
         begin
           Application.MessageBox('数据库库名不能为空!',' 系统提示',MB_ICONINFORMATION);
           Edit1.SetFocus;
           exit;
         end;
       //showmessage(ExtractFilePath(Application.Exename)+'DataBase\SQL_7.0\SSMS_7.0_Data.MDF');
      // showmessage(ExtractFilePath(Application.Exename)+'DataBase\SQL_7.0\'+trim(Edit1.Text)+'_log.LDF');
       if not FileExists(ExtractFilePath(Application.Exename)+'DataBase\SQL_7.0\SSMS_7.0_Data.MDF') then
         begin
           Application.MessageBox('数据库文件不存在,请重新安装!',' 系统提示',MB_ICONINFORMATION);
           Exit;
         end;
       if FileExists(ExtractFilePath(Application.Exename)+'DataBase\SQL_7.0\'+trim(Edit1.Text)+'_log.LDF') then
        begin
          S_Name:= '【'+Edit1.text+'】'+' 数据库已被安装!';
          Application.MessageBox(pchar(S_Name),' 系统提示',MB_ICONINFORMATION);
          Exit;
        end;
         qySetup.Close;
         qySetup.SQL.Text:='sp_attach_db @dbname  =N'+''''+Edit1.Text+''''+'  , @filename1 =N'
                   +''''+ExtractFilePath(Application.Exename)+'DataBase\SQL_7.0\SSMS_7.0_Data.MDF'+'''';
        try
          qySetup.ExecSQL;
        except
        end;
        qysetup.Close;
        qysetup.SQL.Text:=' use '+'"'+edit1.Text+'"';
        try
          qysetup.ExecSQL;
          Application.MessageBox('数据库已创建!',' 系统提示',MB_ICONINFORMATION);
          BitBtn3.Enabled:=False;
        except
          Application.MessageBox('数据库新建失败,请退出后重新创建!',' 系统提示',MB_ICONINFORMATION);
          exit;
        end;
        myShare:=TShare.Create;
          if (myShare.WriteRegistryValue(RegPath,'sSource',edit3.Text)) and
             (myShare.WriteRegistryValue(RegPath,'sCataLog',Edit1.Text)) and
             (myShare.WriteRegistryValue(RegPath,'sPass',Edit2.Text))
            then
           bWriteReg:=True
          else
          begin
            Application.MessageBox('注册表写入失败!',' 系统提示',MB_ICONINFORMATION);
            myShare.Free;
            Exit;
          end;
        myShare.Free;
        Close;
    end;
  1:    //SQL Server 2000
    begin
       if Edit4.Text='' then
         begin
           Application.MessageBox('数据库库名不能为空!',' 系统提示',MB_ICONINFORMATION);
           Edit4.SetFocus;
           exit;
         end;
       if not FileExists(ExtractFilePath(Application.Exename)+'DataBase\SQL_2000\SSMS_Data.MDF') then
         begin
           Application.MessageBox('数据库文件不存在,请重新安装!',' 系统提示',MB_ICONINFORMATION);
           Exit;
         end;
       if FileExists(ExtractFilePath(Application.Exename)+'DataBase\SQL_2000\'+trim(Edit4.Text)+'_log.LDF') then
        begin
          S_Name:= '【'+Edit4.text+'】'+'数据库已被安装!';
          Application.MessageBox(Pchar(S_Name),' 系统提示',MB_ICONINFORMATION);
          Exit;
        end;
       qySetup.Close;
       qySetup.SQL.Text:='sp_attach_db @dbname  =N'+''''+Edit4.Text+''''+' , @filename1 =N'
                   +''''+ExtractFilePath(Application.Exename)+'DataBase\SQL_2000\SSMS_Data.MDF'+'''';
        try
          qySetup.ExecSQL;
        except

        end;
        qysetup.Close;
        qysetup.SQL.Text:=' use '+'"'+edit4.Text+'"';
        try
          qysetup.ExecSQL;
          Application.MessageBox('数据库已创建!',' 系统提示',MB_ICONINFORMATION);
          BitBtn3.Enabled:=False;
        except
          Application.MessageBox('数据库新建失败!',' 系统提示',MB_ICONINFORMATION);
          exit;
        end;
        myShare:=TShare.Create;
          if (myShare.WriteRegistryValue(RegPath,'sSource',edit3.Text)) and
             (myShare.WriteRegistryValue(RegPath,'sCataLog',Edit4.Text)) and
             (myShare.WriteRegistryValue(RegPath,'sPass',Edit2.Text))
            then
           bWriteReg:=True
          else
          begin
            Application.MessageBox('注册表写入失败!',' 系统提示',MB_ICONINFORMATION);
            myShare.Free;
            Exit;
          end;
        myShare.Free;
        Close;
    end;
  end;
end;

procedure TFrm_Server.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if (bitBtn3.Enabled) or (not bWriteReg) then
  begin
    if (application.MessageBox('你确认退出服务器注册程序吗,您可以在安装目录来运行注册程序!','询问',36)=ID_YES) then
    else
    begin
      CanClose:=False;
      Exit;
    end;
  end;
  CanClose:=True;
end;

procedure TFrm_Server.BitBtn1Click(Sender: TObject);
begin
  Close;
end;

procedure TFrm_Server.FormCreate(Sender: TObject);
begin
  bWriteReg:=False;
end;

procedure TFrm_Server.RadioGroup1Click(Sender: TObject);
begin
  case RadioGroup1.ItemIndex of
  0:
    begin
      Edit1.Enabled:=true;
      Edit4.Enabled:=false;
      Edit4.Text:='';
      Edit1.SetFocus;
    end;
  1:
    begin
      Edit4.Enabled:=true;
      Edit1.Enabled:=false;
      Edit1.Text:='';
      Edit4.SetFocus;
    end;
  end;
end;

procedure TFrm_Server.Btn_vClick(Sender: TObject);
var
  ConStr:string;
begin
    ConStr:='Provider=SQLOLEDB.1;Persist Security Info=False;'
         +'User ID=sa;Password="'+Edit2.Text+'";'
         +'Initial Catalog=master;Data Source='+edit3.Text;
    ConSetup.Connected:=False;
    ConSetup.ConnectionString:=ConStr;
    ConSetup.Connected:=true;
    Qy_V.Connection:=ConSetup;
    Qy_V.Prepared;
    Qy_V.Open;      
    ShowMessage(Qy_V.fieldbyname('V').AsString);
    if Copy(Qy_V.FieldByName('V').AsString,23,1)='2' then
      begin
        RadioGroup1.ItemIndex:=1;
        Edit4.Text:='SSMS';
        RadioGroup1.Controls[0].Enabled := False;
      end
      else
      begin
        RadioGroup1.ItemIndex:=0;
        Edit1.Text:='SSMS';
        RadioGroup1.Controls[1].Enabled := False;
      end;
end;

end.

⌨️ 快捷键说明

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