Author: Mike Heydon
Function to load a StringList with MS-SQL Servers on a Network via SQL-DMO. MS-SQL
DMO is a COM/OLE object that can do many things, in this article we just enumerate
the SQL Servers on a Network. "List SQL servers on the network" by Tommy Andersen
deals with this issue by using WinSock and a comment supplies a solution using
CoApplication.Create. This article solves the issue by using
CreateOleObject('SQLDMO.SQLServer'). The function returns true if successful.
/
1 / Declaration
2 3 function EnumSqlServers(AStrings: TStrings): boolean;
4 5 // Eg.6 EnumSqlServers(Memo1.Lines);
7 8 Answer:
9 10 uses ComObj, Variants; {Variants is for Delphi 7}11 12 // ====================================================13 // Load SQL Servers on a Network into a string list14 // ====================================================15 16 function EnumSqlServers(AStrings: TStrings): boolean;
17 var18 oDmo, oApp, oServers: OleVariant;
19 bResult: boolean;
20 i: integer;
21 begin22 AStrings.Clear;
23 24 try25 oDMO := CreateOleObject('SQLDMO.SQLServer');
26 oApp := oDMO.Application;
27 oServers := oApp.ListAvailableSQLServers;
28 29 try30 AStrings.BeginUpdate;
31 for i := 1 to oServers.Count do32 AStrings.Add(oServers.Item(i));
33 finally34 AStrings.EndUpdate;
35 end;
36 37 bResult := true;
38 except39 bResult := false;
40 end;
41 42 oServers := Unassigned;
43 oApp := Unassigned;
44 oDMO := Unassigned;
45 46 Result := bResult;
47 end;