如何获取目前网络上(局域网)可用的数据库连接(50分)

  • 主题发起人 主题发起人 阿群
  • 开始时间 开始时间

阿群

Unregistered / Unconfirmed
GUEST, unregistred user!
[?]如何获取目前网络上(局域网)可用的Microsoft SQL Server数据库服务器,例如
SQL Server200中新注册一个数据库连接时会自动搜索可用的数据库服务器。
 
关注,我也有这样的问题
 
我也想知道
 
----用用我的控件吧!

unit ListSQLServers;

interface

uses
Windows, Messages, SysUtils, Classes;

type
TListSQLServers = class(TComponent)
private
{ Private declarations }
FServerNames : TStringList;
FActive : boolean;
procedure SetActive(aActive: boolean);
function GetComputerName: string;
procedure SetComputerName(ComputerName: string);
protected
{ Protected declarations }
public
{ Public declarations }
constructor create(AOwner: Tcomponent); override;
destructor destory;

procedure Refresh;
published
{ Published declarations }
property Active : boolean Read FActive Write SetActive stored false;
property ServerNames : TStringList read FServerNames stored false;
property LocalServerName : string read GetComputerName write SetComputerName stored false;
end;

procedure Register;

implementation

uses Variants,ComObj;

procedure Register;
begin
RegisterComponents('胡文韬', [TListSQLServers]);
end;


constructor TListSQLServers.create(AOwner: Tcomponent);
begin
inherited;
FServerNames := TStringList.Create;
end;

destructor TListSQLServers.destory;
begin
FreeAndNil(FServerNames);
inherited;
end;

procedure TListSQLServers.SetActive(aActive: boolean);
begin
if aActive = FActive then exit;

if aActive then
Refresh
else FServerNames.Clear;

FActive := aActive;

end;

procedure TListSQLServers.Refresh;
var i : integer;
SQLServer:Variant;
ServerList:Variant;
nServers:integer;
begin
FServerNames.Clear;

SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
nServers:=ServerList.Count;
for i := 1 to nservers do
FServerNames.Add(ServerList.Item(i));

SQLServer := NULL;
serverList := NULL;
end;

function TListSQLServers.GetComputerName: string;
var NameBuf: array [0..MAX_COMPUTERNAME_LENGTH + 1] of char;
NameLen: DWORD;
begin
NameLen := sizeof(NameBuf);
if windows.GetComputerName(NameBuf, NameLen) then
result := NameBuf
else raise exception.Create('取本机计算机名称意外失败');
end;

procedure TListSQLServers.SetComputerName(ComputerName: string);
begin
windows.SetComputerName(pchar(ComputerName));
end;
end.
 
老大有这个控件吧,也给小弟发一个呀,感谢不尽,

jxbiao@sohu.com

谢谢
 
var
i : integer;
SQLServer:Variant;
ServerList:Variant;
nServers:integer;
begin
SQLServer := CreateOleObject('SQLDMO.Application');
ServerList:= SQLServer.ListAvailableSQLServers;
nServers:=ServerList.Count;
for i := 1 to nservers do
memo1.Lines.Add(ServerList.Item(i));
end;
 
后退
顶部