Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (7)


Enumerate MS-SQL Servers via SQL-DMO into TStringsFormat this article printer-friendly!Bookmark function is only available for registered users!
EnumSqlServers()
Product:
Delphi 5.x (or higher)
Category:
OLE
Skill Level:
Scoring:
Last Update:
08/02/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet SQL-DMO ENUM SERVERS
Times Scored:
11
Visits:
6280
Uploader: Mike Heydon
Company: EOH
Reference: N/A
 
Question/Problem/Abstract:
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. Article #2385 "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.

// Declaration
function EnumSqlServers(AStrings : TStrings) : boolean;

// Eg.
EnumSqlServers(Memo1.Lines);

Answer:



uses ComObj, Variants;     {Variants is for Delphi 7)

// ====================================================
// Load SQL Servers on a Network into a string list
// ====================================================

function EnumSqlServers(AStrings : TStrings) : boolean;
var oDmo,oApp,oServers : OleVariant;
    bResult : boolean;
    i : integer;
begin
  AStrings.Clear;

  try
    oDMO := CreateOleObject('SQLDMO.SQLServer');
    oApp := oDMO.Application;
    oServers := oApp.ListAvailableSQLServers;

    try
      AStrings.BeginUpdate;
      for i := 1 to oServers.Count do
        AStrings.Add(oServers.Item(i));
    finally
      AStrings.EndUpdate;
    end;

    bResult := true;
  except
    bResult := false;
  end;

  oServers := Unassigned;
  oApp := Unassigned;
  oDMO := Unassigned;

  Result := bResult;
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Retrieving Server Name of a ODBC DSN
    MohammadReza ZarrinPour (Apr 24 2006 1:14PM)

Hi Heydon
Thanks for your help.
I have another Question.How can i retrieve Server Name or Host Name
for a given ODBC Date Source.I've searched the MSDN and found out ,we have an API called "SqlBrowseConnect". and it gives us information about ODBC connection BUT i cant use it bacause i 'm not familiar with API call..Any way what's your idea .?

Thanks in advance.
Regards.
Respond

RE: Retrieving Server Name of a ODBC DSN
Mike Heydon (Apr 24 2006 3:26PM)

Once again that information is available from the Registry in Key HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI  

For example on my machine in this key I have Helpdesk2 and PG01 which are MSSQL and ORACLE ODBC connections, Within each of these keys ther are further values ie.

Server=pgbbxbfs1              (this is for the MSSQL)
ServerName=pg01.world     (for Oracle)

Use RegEdit in windows to have a look at the values in these keys and you will see the info you are looking for. I have a class that can read and set these values, I will send to you if you send me your E-Mail Address.
Respond

RE: RE: Retrieving Server Name of a ODBC DSN
MohammadReza ZarrinPour (Apr 24 2006 10:18PM)

Thanks for your attention.
My Email is ShellProgrammer@yahoo.com

By the way ,Reading Registry for achieving this info is a good way??
I think microsoft may change this Reg_Key in future.what's your idea.?
Any way..i look forward for your Class.:-)

Regards
Respond

List of ODBC for a given Server Name
    MohammadReza ZarrinPour (Apr 23 2006 4:39PM)

Hi
I want to know is it possible to retrieve the list of all ODBC DataSources for a given Server Name(HOST) in a LAN .in the other word in my office i have a small LAN and i want to achieve the ODBC aliases of other clients programmatically.Is there any function or method for this purpose.?


Thanks in advance.
Regards.
Respond

RE: List of ODBC for a given Server Name
Mike Heydon (Apr 24 2006 11:38AM)

Read the Registry of the remote server using Delphi's class TRegistry. The method TRegistry.RegistryConnect(UNCNamer) will allow you to connect to the registry of a remote server (if adequate permissions).

UNCName is the name of the remote computer, and must take the following form: '\\computername'

Then use the normal OpenKey and ReadKey methods of TRegistry to
read the installed ODBC information.

The keys you are looking for are in
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI
and
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI

Respond

How to call this?
    Matt Minnis (Jul 30 2004 5:47PM)

What is the method to call this?
It seems to want the name of a SQL server before it can enumerate servers.  The whole point of what I am trying to do is to find the available SQL Servers.
I am trying to use the Windows authentication version.
Here is how I am calling it:
//BStrings is a TStringList.
//There is no server named test on the network.
EnumSQLServers(BStrings, 'test');

Am I missing something here?
Respond

RE: How to call this?
Mike Heydon (Aug 2 2004 10:12AM)

Whoooops,
You are correct, you do not need to connect to a server to enumerate the servers on a network. The code is an offshoot from my TDmoClass object that scripts objects etc. and requires a connection to a server. (Victim of Copy & Paste). I have rectified the function so that all that is required is a string list. Thanx.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)