Delphi下枚举局域网内SQL服务器以及数据库(版权所有!!)

类别:Delphi 点击:0 评论:0 推荐:

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, dxCntner, dxEditor, dxExEdtr, dxEdLib, StdCtrls,
  ComCtrls,ComObj, Buttons;

type
  TfrmMain = class(TForm)
    Panel1: TPanel;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Bevel4: TBevel;
    Label1: TLabel;
    HostName: TdxPickEdit;
    Label2: TLabel;
    DataBaseName: TdxPickEdit;
    GroupBox1: TGroupBox;
    Label3: TLabel;
    dxEdit1: TdxEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label4: TLabel;
    Label5: TLabel;
    UserID: TdxEdit;
    UserPass: TdxEdit;
    Label6: TLabel;
    PassLogin: TRadioButton;
    NtLogin: TRadioButton;
    procedure HostNameDropDown(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure DataBaseNameDropDown(Sender: TObject);
    procedure NtLoginClick(Sender: TObject);
    procedure PassLoginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
     end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.HostNameDropDown(Sender: TObject);
var
     SQLServer:Variant;
     ServerList:Variant;
     i,nServers:integer;

begin
    if HostName.Items.Count<1 then
   begin
        HostName.Items.Clear;
        SQLServer:=CreateOleObject('SQLDMO.Application');
        ServerList:=SQLServer.ListAvailableSQLServers;
        nServers:=ServerList.Count;
        for i:=0 to nservers-1 do     
           HostName.Items.Add(ServerList.Item(i));
        SQLServer:=NULL;
        serverList:=NULL;
        
   end;
  end;
procedure TfrmMain.BitBtn2Click(Sender: TObject);
begin
        Close;
end;

procedure TfrmMain.DataBaseNameDropDown(Sender: TObject);
var
        SQLServer:Variant;
        Server:Variant;
        db:Variant;
        i,j:integer;
begin
        SQLServer:=CreateOleObject('SQLDMO.Application');
        Server:=CreateOleObject('SQLDMO.SQLServer');
        if NtLogin.Checked then
        begin
                Server.LoginSecure:=True;
                //设置为WINDOWS登录模式
                Server.Connect(HostName.Text);
                Server.IntegratedSecurity.SecurityMode:=1;
                Server.DisConnect;
                Server.Stop;
                while Server.Status<>3 do
                begin
                end;
                Server.Start(True,HostName.Text);
        end
        else
        begin
               Server.LoginSecure:=True;
                //设置为混合登录模式
                Server.Connect(HostName.Text);
                Server.IntegratedSecurity.SecurityMode:=2;
                Server.DisConnect;
                Server.Stop;
                while Server.Status<>3 do
                begin
                end;
                Server.Start(True,HostName.Text);
        end;

        db:=Server.DataBases;
        i:=db.Count;
        DataBaseName.Items.Clear;
        for j:=1 to i do
                DataBaseName.Items.Add(db.item(j).name);

        Server:=NULL;
        SQLServer:=NULL;
        db:=NULL;
end;

procedure TfrmMain.NtLoginClick(Sender: TObject);
begin
        if Ntlogin.Checked=True then
        begin
                UserID.Enabled:=False;
                UserPass.Enabled:=False;
        end;

end;

procedure TfrmMain.PassLoginClick(Sender: TObject);
begin
        if PassLogin.Checked=True then
        begin
                UserID.Enabled:=True;
                UserPass.Enabled:=True;
        end;
end;

end.
程序中使用的控件为Developer Express Inc的扩展控件。经过测试可通过两种登录方式

本文地址:http://com.8s8s.com/it/it4758.htm