程序需要求该
结果发现重装了系统
sqlSERVER连接不上了,调试的时候,出现错误!
就是判断语句上,indy组件里面的
请大家给出其他的判断方法,谢谢!

解决方案 »

  1.   

    获取服务状态:uses
      WinSvc;function GetServiceStatus(const ServiceName: String): Cardinal;
    var
      FSS: TServiceStatus;
      schSCManager, schService: SC_HANDLE;
      sclLock: SC_LOCK;
    begin
      Result := 0;
      // Open service control manager database
      schSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
      if schSCManager <> 0 then   // if
      try  // try
        // Requests ownership of the service control manager database lock.
        // Only one process can own the lock at any given time.
        sclLock := LockServiceDatabase(schSCManager);
        if Assigned(sclLock) then   // if
        try  // try
          // Opens an existing service
          schService := OpenService(schSCManager, PChar(ServiceName), SERVICE_QUERY_STATUS);
          if schService <> 0 then  // if
            try  // try
              // Load Library and Get Proc Address
              if QueryServiceStatus(schService, FSS) then  // if
                Result := FSS.dwCurrentState;
            finally  // wrap up finally
              CloseServiceHandle(schService);
            end;  // end try finally
        finally  // wrap up finally
          UnlockServiceDatabase(sclLock);
        end;  // end try finally
      finally  // wrap up finally
        CloseServiceHandle(schSCManager);
      end;  // end try finally
    end;
      

  2.   

    判断SQL Server是否已经运行:方法1:通过判断进程
    function g_ServerOnRun(serverName: string): boolean;
    var
      ProcessList : Thandle;
      pe : TPROCESSENTRY32;
      ProcList : TStringList;
      i : integer;
    begin
      result := false;
      ProcList := TStringList.Create;
      try
        ProcessList := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        pe.dwSize := sizeof(TPROCESSENTRY32);
        if process32first(ProcessList,pe) then
        begin
          ProcList.Add(pe.szexefile);
          while process32next(ProcessList,pe) do
            ProcList.Add(pe.szexefile);
        end;
        for i := 0 to ProcList.Count -1 do
        begin
          if LowerCase(ProcList[i]) = LowerCase(serverName) then
          begin
            result := true;
            break;
          end;
        end;
      finally
        ProcList.Free;
      end;
    end;
    方法1需要补充:在服务端需要写一个socket server,处理客户端数据库联接侦测以及检测本地数据库是否已启动。方法2:设置ADOConnection.Connected := true进行判断
    这个方法判断存在缺陷,如果是网络或者其他原因导致连接异常,并不能说明服务器数据库未启动。...
      

  3.   

    1楼的1433端口方法也凑着能用。procedure TForm1.Button1Click(Sender: TObject);
    begin
      ClientSocket1.Address := '127.0.0.1';
      ClientSocket1.Port := 1433;
      ClientSocket1.Active := True;
    end;procedure TForm1.ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      ShowMessage('server is open');
    end;procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      if ErrorCode=10061 then
      begin
        ShowMessage('Server does not open ');
        ErrorCode := 0;
      end;
    end;