Solved

Retrieve All SQL Server Name....

Posted on 2002-03-20
4
326 Views
Last Modified: 2010-04-05
        I'm on project concern of programming SQL Server Application utilizing Delphi 5.0 as a complier. What I just want to know is How to retrieve all SQL Server Name and have it shown in Listbox on my form...for their convenience... I dont' want them to ask Administrator or to remember SQL Server name without necessary. Just like when I double click on TADOConnection and select provider to be OLE SQL Server then SQL Server Name Shown on ListBox above.
         Show me some code to do that, I'll reward you my points...
0
Comment
Question by:DOOM_DAEMON
4 Comments
 
LVL 1

Accepted Solution

by:
BartonM earned 50 total points
ID: 6885017
Hi Doom Daemon,

put this code in a unit:

unit uDomainInfo;

interface

uses
 Windows, Classes, Sysutils;

const
   NetApi32 = 'netapi32.dll';
   ERROR_SUCCESS = 0;
   ERROR_MORE_DATA = 234;
   SV_TYPE_SERVER = $2;//   'Server type mask, all types of servers
   SIZE_SI_101 = 24;

type

   PSERVER_INFO_101 = ^SERVER_INFO_101;

   SERVER_INFO_101 = record
     dwPlatformId : integer;
     lpszServerName: LPWSTR;
     dwVersionMajor: integer;
     dwVersionMinor: integer;
     dwType: integer;
     lpszComment: LPWSTR;
  end;


//function NetApiBufferFree(BufPtr : pointer): integer; stdcall;
procedure GetServers(filter : Dword; const domain : string; list : TStrings);
Var
NetAPI: THandle;

_NetServerEnum: function(serverName : PWideChar; level : Integer;
   var BufPtr : Pointer; prefMaxLen : Integer; var entriesRead,
   totalEntries : Integer; servertype : Integer; domain : PWideChar;
   var resume_handle : Integer) : integer; stdcall;

_NetApiBufferFree: function(BufPtr : pointer): integer; stdcall;

_SQLBrowseConnect: function(SQLHDBCConnectionHandle: THandle;
InConnectionString: string; StringLength1: integer;
OutConnectionString: PWideChar;
BufferLength: integer;
StringLength2Ptr: integer) :integer; stdcall;
implementation


procedure GetServers(filter : Dword; const domain : string; list : TStrings);
var
 wServerName : WideString;
 wDomain : WideString;
 pwDomain : PWideChar;
 rv : Dword;
 p : PSERVER_INFO_101;
 buffer : pointer;
 i, entriesRead,
 totalEntries, MAX_PREFERRED_LENGTH,
 NIL_HANDLE : Integer;
 ServerName : string;

begin
 list.Clear;
 ServerName := '';
 wServerName := ServerName;
 MAX_PREFERRED_LENGTH := -1;
 wDomain := domain;
 if domain = '' then
   pwDomain := Nil
 else
   pwDomain := PWideChar (wDomain);
 NetAPI := SafeLoadLibrary(NetApi32);
 if NetAPI <> 0 then
 begin
   @_NetServerEnum := GetProcAddress(NetAPI, 'NetServerEnum');
   @_NetAPIBufferFree := GetProcAddress(NetAPI, 'NetApiBufferFree');
 end
 else
   Exit;
 rv := _NetServerEnum (PWideChar (wServerName), 101, buffer, MAX_PREFERRED_LENGTH,
                     entriesRead,  totalEntries, filter, pwDomain, NIL_HANDLE);
 if rv = ERROR_SUCCESS then
   try
     p := PSERVER_INFO_101 (buffer);
     for i := 0 to entriesRead - 1 do
     begin
       list.AddObject (p^.lpszServerName , TObject ( p^.dwPlatformId));
       Inc(p)
     end
   finally
     _NetAPIBufferFree(buffer);
   end
 else
   raise Exception.CreateRes(rv);
end;

end.

And use this code to fill a combobox with the servernames:

procedure x
var lst : TStringList;
begin

  lst := TStringList.Create;
  GetServers($4, DOMAINNAME', lst);
  ComboBox1.Items := lst;
end;

BartonM
0
 
LVL 15

Expert Comment

by:simonet
ID: 6889563
following...
(nice code!)
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6976072
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20110714.html
http://www.experts-exchange.com/questions/Q.20173874.html
http://www.experts-exchange.com/questions/Q.20170377.html
http://www.experts-exchange.com/questions/Q.20178176.html
http://www.experts-exchange.com/questions/Q.20178177.html
http://www.experts-exchange.com/questions/Q.20229473.html
http://www.experts-exchange.com/questions/Q.20279642.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20220086.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 
LVL 1

Author Comment

by:DOOM_DAEMON
ID: 7004527
    Sorry for took it so long,... I've been aboard and so busy....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now