10

If I was using C# I could use the .net framework's SqlDataSourceEnumerator to discover and show a user a list of SQL Server instances on the network.

How can I do that in Delphi?

3
  • 7
  • I actually used Whiler's linked code. If you convert that comment to an answer and expand it a tiny bit, I'll accept it.
    – Warren P
    May 18, 2012 at 22:41
  • Done... but like I said... everything is already explained on his page and I don't want to steal his content :/
    – Whiler
    May 18, 2012 at 23:49

3 Answers 3

11

You can use the NetServerEnum function , filtering by the SV_TYPE_SQLSERVER value in the servertype param, another option is use the SQLOLEDB Enumerator ADO object.

6

To enumerate all available Microsoft SQL Servers, you can follow this excellent tutorial:

Enumerating available SQL Servers. Retrieving databases on a SQL Server

Included in Zarko's tutorial, there's a link to download the full source code (direct download) which can be useful to quickly test it and check if it fits your needs.

Edit Zarko Gajic's main routine is:

procedure ListAvailableSQLServers(Names : TStrings);
var
  RSCon: ADORecordsetConstruction;
  Rowset: IRowset;
  SourcesRowset: ISourcesRowset;
  SourcesRecordset: _Recordset;
  SourcesName, SourcesType: TField;

    function PtCreateADOObject(const ClassID: TGUID): IUnknown;
    var
      Status: HResult;
      FPUControlWord: Word;
    begin
      asm
        FNSTCW FPUControlWord
      end;
      Status := CoCreateInstance(
                  CLASS_Recordset,
                  nil,
                  CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER,
                  IUnknown,
                  Result);
      asm
        FNCLEX
        FLDCW FPUControlWord
      end;
      OleCheck(Status);
    end;
begin
  SourcesRecordset := PtCreateADOObject(CLASS_Recordset) as _Recordset;
  RSCon := SourcesRecordset as ADORecordsetConstruction;
  SourcesRowset := CreateComObject(ProgIDToClassID('SQLOLEDB Enumerator')) as ISourcesRowset;
  OleCheck(SourcesRowset.GetSourcesRowset(nil, IRowset, 0, nil, IUnknown(Rowset)));
  RSCon.Rowset := RowSet;
  with TADODataSet.Create(nil) do
  try
    Recordset := SourcesRecordset;
    SourcesName := FieldByName('SOURCES_NAME'); { do not localize }
    SourcesType := FieldByName('SOURCES_TYPE'); { do not localize }
    Names.BeginUpdate;
    try
      while not EOF do
      begin
        if (SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE) and (SourcesName.AsString <> '') then
          Names.Add(SourcesName.AsString);
        Next;
      end;
    finally
      Names.EndUpdate;
    end;
  finally
    Free;
  end;
end;

I don't know what I can add without lamering what Zarko's explained.

1
  • 2
    I took the liberty of posting a core routine (a snippet from the real code) because I think that's acceptable practice here and generally on the internet. No intent here to steal from Zarko, praise be to the Zarkmeister and Uber-About-Delphi-Guy. :-)
    – Warren P
    May 20, 2012 at 13:10
5

I use this code:

uses ActiveX,
     ComObj,
     OleDB,
     DB,
     ADOInt,
     ADODB;

procedure ListAvailableSQLServers(Names: TStringList);
var
  RSCon: ADORecordsetConstruction;
  Rowset: IRowset;
  SourcesRowset: ISourcesRowset;
  SourcesRecordset: _Recordset;
  SourcesName, SourcesType: TField;

function PtCreateADOObject(const ClassID: TGUID): IUnknown;
var
  Status: HResult;
  FPUControlWord: Word;
begin
  asm
    FNSTCW FPUControlWord
  end;
  Status := CoCreateInstance(
              CLASS_Recordset,
              nil,
              CLSCTX_INPROC_SERVER or
              CLSCTX_LOCAL_SERVER,
              IUnknown,
              Result);
  asm
    FNCLEX
    FLDCW FPUControlWord
  end;
  OleCheck(Status);
end;

begin
  SourcesRecordset :=
       PtCreateADOObject(CLASS_Recordset)
       as _Recordset;
  RSCon :=
       SourcesRecordset
       as ADORecordsetConstruction;
   SourcesRowset :=
       CreateComObject(ProgIDToClassID('SQLOLEDB Enumerator'))
       as ISourcesRowset;
   OleCheck(SourcesRowset.GetSourcesRowset(
            nil,
            IRowset, 0,
            nil,
            IUnknown(Rowset)));
   RSCon.Rowset := RowSet;
   with TADODataSet.Create(nil) do
   try
     Recordset := SourcesRecordset;
     SourcesName := FieldByName('SOURCES_NAME');
     SourcesType := FieldByName('SOURCES_TYPE');
     Names.BeginUpdate;
     Names.Clear;
     try
        while not EOF do
        begin
          if (SourcesType.AsInteger = DBSOURCETYPE_DATASOURCE) and
             (SourcesName.AsString <> '') then
            Names.Add(SourcesName.AsString);
          Next;
        end;
     finally
        Names.EndUpdate;
     end;
  finally
     Free;
  end;
end;




procedure GetServer();
var
  oItems: TStringList;
begin
  oItems:= TStringList.Create;
  try
    ListAvailableSQLServers(oItems);
    // To something with oItems
    ShowMessage(oItems.Text);
  finally
    oItems.Free;
  end;
end;
2
  • Don't forget to set ADODB in the uses
    – Ravaut123
    May 16, 2012 at 7:48
  • And a few others. (Made a valid uses clause.)
    – Warren P
    May 16, 2012 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.