• Delphi取局域网上所有的SQL服务器名称


    unit GetSQLServerUnt;
    
    interface
    
    uses ADODB, OleDB, ActiveX,  ComObj, ADOInt, ADOConst, Classes, Windows, SysUtils,
      DB;
    
    
    const CLSID_SQLOLEDB_ENUMERATOR: TGUID = '{DFA22B8E-E68D-11d0-97E4-00C04FC2AD98}';
    function CreateADOObject(const ClassID: TGUID): IUnknown;
    //取得局域上所有的SQL服务器名
    procedure getSQLServerNames(Names: TStrings);
    
    
    implementation
    
    function CreateADOObject(const ClassID: TGUID): IUnknown;
    var
      Status: HResult;
      FPUControlWord: Word;
    begin
      asm
        FNSTCW  FPUControlWord
      end;
      Status := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or
        CLSCTX_LOCAL_SERVER, IUnknown, Result);
      asm
        FNCLEX
        FLDCW FPUControlWord
      end;
      if (Status = REGDB_E_CLASSNOTREG) then
        raise Exception.CreateRes(@SADOCreateError) else
        OleCheck(Status);
    end;
    
    procedure getSQLServerNames(Names: TStrings);
    var
      RSCon: ADORecordsetConstruction;
      Rowset: IRowset;
      SourcesRowset: ISourcesRowset;
      SourcesRecordset: _Recordset;
      SourcesName, SourcesType: TField;
    begin
      SourcesRecordset := CreateADOObject(CLASS_Recordset) as _Recordset;
      RSCon := SourcesRecordset as ADORecordsetConstruction;
      SourcesRowset := CreateComObject(CLSID_SQLOLEDB_ENUMERATOR) as ISourcesRowset;
      OleCheck(SourcesRowset.GetSourcesRowset(nil, IRowset, 0, nil, IUnknown(Rowset)));
      RSCon.Rowset := RowSet;
      with TADODataSet.Create(nil) do
      try
        Recordset := SourcesRecordset;
        First;
        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 then
              Names.Add(SourcesName.AsString);
            Next;
          end;
        finally
          Names.EndUpdate;
        end;
      finally
        Free;
      end;
    end;
    
    
    
    
    end.
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    c# 如何获取当前方法的调用堆栈(转载)
    被忽视的问题:测试环境配置管理
    性能测试如何创造业务价值
    高级性能测试工程师面试必问十大问题
    Vue随机验证码组件
    面试突击61:说一下MySQL事务隔离级别?
    面试突击60:什么情况会导致 MySQL 索引失效?
    抽奖动画 鲤鱼跳龙门
    VScode 安装PHPdebug新版Xdebug3.0
    Java 项目安全、如何进行安全审计
  • 原文地址:https://www.cnblogs.com/jijm123/p/14260335.html
Copyright © 2020-2023  润新知