Enumerate Microsoft SQL database servers on the local network, using delphi

前端 未结 3 1962
-上瘾入骨i
-上瘾入骨i 2020-12-30 09:53

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 t

3条回答
  •  甜味超标
    2020-12-30 09:56

    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;
    

提交回复
热议问题