unit U_SqlServers;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB, ComObj, RzButton, ExtCtrls, RzPanel, Mask,
  RzEdit,IniFiles;

type
  TFrm_ServersList = class(TForm)
    ADOConnection1: TADOConnection;
    RzBitBtn1: TRzBitBtn;
    RzBitBtn2: TRzBitBtn;
    RzGroupBox1: TRzGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    CB_ServerNames: TComboBox;
    CB_DataNames: TComboBox;
    Ed_Login: TEdit;
    Ed_Pwd: TEdit;
    RzGroupBox2: TRzGroupBox;
    Label5: TLabel;
    Edit1: TEdit;
    Label6: TLabel;
    Edit2: TEdit;
    RzGroupBox3: TRzGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    edt_DataIP: TEdit;
    edt_UserName: TEdit;
    edt_PassWord: TEdit;
    edt_ServerName: TEdit;
    edt_Port: TRzNumericEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure CB_DataNamesEnter(Sender: TObject);
    procedure RzBitBtn2Click(Sender: TObject);
    procedure RzBitBtn1Click(Sender: TObject);
  private
    { Private declarations }
    function GetSQLServerList: Boolean;
    function conn(dataname: string): string;
  public
    { Public declarations }
  end;

var
  Frm_ServersList: TFrm_ServersList;

implementation

uses LoadConFig, SkyPublic, FunctionPas;

{$R *.dfm}

{ TFrm_ServersList }

procedure TFrm_ServersList.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := CaFree;
end;

procedure TFrm_ServersList.FormShow(Sender: TObject);
var
  L_IniFile: TIniFile;
begin
  GetSQLServerList;
  CB_ServerNames.Text := ConfigValue.DbServer;
  Ed_Login.Text := ConfigValue.DbUser;
  Ed_Pwd.Text := UncrypKey(ConfigValue.DbPass);
  CB_DataNames.Text := ConfigValue.DbBase;
  Edit1.Text := ConfigValue.HphmQz;
  Edit2.Text := ConfigValue.SYDWXX;
  L_IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\Config.ini');
  try
    edt_DataIP.Text := UncrypKey(L_IniFile.ReadString('OraDatabase', 'DB_IP', ''));
    edt_UserName.Text := UncrypKey(L_IniFile.ReadString('OraDatabase',
      'DB_Username', ''));
    edt_PassWord.Text := UncrypKey(L_IniFile.ReadString('OraDatabase',
      'DB_Password', ''));
    edt_ServerName.Text := UncrypKey(L_IniFile.ReadString('OraDatabase',
      'DB_ServerName',''));
    edt_Port.IntValue := L_IniFile.ReadInteger('OraDatabase', 'DB_Port', 1521);
  finally
    L_IniFile.Free;
  end;
end;

procedure TFrm_ServersList.Button1Click(Sender: TObject);
begin
  Close;
end;

function TFrm_ServersList.GetSQLServerList: Boolean;
var
  i: integer;
  SQLServer: Variant;
  ServerList: Variant;
begin
  try
    SQLServer := CreateOleObject('SQLDMO.Application');
    ServerList := SQLServer.ListAvailableSQLServers;
    for i := 1 to ServerList.Count do
      CB_ServerNames.Items.Add(ServerList.item(i));
    CB_ServerNames.ItemIndex := 0;
    Result := True;
  except
//    ShowMsg('找不到SQL服务器,请检查!', '错误信息', mtError,   [mbOK],
//          ['确定'],$00efebef,$00000000);
    Result := false;
  end;
end;

function TFrm_ServersList.conn(dataname: string): string;
begin
  try
    ADOConnection1.Close;
    ADOConnection1.ConnectionString := 'Provider=SQLOLEDB.1;Password=' + Trim(
      Ed_Pwd.Text) + ';Persist Security Info=False;User ID=' + Trim(
      Ed_Login.Text) + ';Initial Catalog=' + dataname + ';Data Source=' + Trim(
      CB_ServerNames.Text);
    ADOConnection1.LoginPrompt := false;
    ADOConnection1.Connected := True;
  except
    
  end;
end;

procedure TFrm_ServersList.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    perform(WM_NEXTDLGCTL, 0, 0);
end;

procedure TFrm_ServersList.CB_DataNamesEnter(Sender: TObject);
var
  TheAdo :TADOQuery;
begin
  conn('master');
  CB_DataNames.Items.Clear;
  TheAdo := TADOQuery.Create(Self); 
  try
    TheAdo.Connection := ADOConnection1;
    TheAdo.Close;
    TheAdo.SQL.Clear;
    TheAdo.SQL.Add('select name from sysDatabases');
    TheAdo.Open;
    while not TheAdo.Eof do
    begin
      CB_DataNames.Items.Add(TheAdo.FieldValues['name']);
      TheAdo.Next;
    end;
    CB_DataNames.ItemIndex:=0;
  finally
    TheAdo.Close;
    FreeAndNil(TheAdo);
  end;
end;

procedure TFrm_ServersList.RzBitBtn2Click(Sender: TObject);
begin
  close;
end;

procedure TFrm_ServersList.RzBitBtn1Click(Sender: TObject);
var
  L_IniFile: TIniFile;
begin
  ConfigValue.DbServer := CB_ServerNames.Text;
  ConfigValue.DbUser := Ed_Login.Text ;
  ConfigValue.DbPass := EncrypKey(Ed_Pwd.Text);
  ConfigValue.DbBase := CB_DataNames.Text;
  ConfigValue.HphmQz := Edit1.Text ;
  ConfigValue.SYDWXX :=Edit2.Text ;
  L_IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\Config.ini');
  try
    L_IniFile.WriteString('OraDatabase', 'DB_IP', EncrypKey(edt_DataIP.Text));
    L_IniFile.WriteString('OraDatabase', 'DB_Username', EncrypKey(edt_UserName.Text));
    L_IniFile.WriteString('OraDatabase', 'DB_Password', EncrypKey(edt_PassWord.Text));
    L_IniFile.WriteString('OraDatabase', 'DB_ServerName', EncrypKey(edt_ServerName.Text));
    L_IniFile.WriteInteger('OraDatabase', 'DB_Port', edt_Port.IntValue);
    MessageBox(Handle, '配置信息已保存!', '提示', MB_OK + MB_ICONINFORMATION);
  finally
    L_IniFile.Free;
  end;
end;

end.