2011. január 22., szombat

Check if MS SQL Server is reachable


Problem/Question/Abstract:

Can I find programmatically if certain MS SQL Server is reachable on net?

Answer:

Here is function, performing check if MS SQL Server is reachable on net against given server name (usually host on that MS SQL Server is installed), user name and password

function CheckMSSQLServer(fServerName, fUserName, fPsw: string): Bool;
var
  wDb: TDatabase;
begin // Check if MS SQL Server is reachable
  // Important! BDE Must be installed
  Result := False;
  wDb := TDatabase.Create(nil);

  with wDb do
  begin
    DatabaseName := 'wDbDatabaseName'; // arbitrary name, must be unique
    // in current Session
    Params.Values['SERVER Name'] := fServerName;
    Params.Values['USER Name'] := fUserName;
    Params.Values['PASSWORD'] := fPsw;
    LoginPrompt := False;
  end;

  try
    wDb.DriverName := 'MSSQL';
    try
      wDb.Connected := True;
      wDb.Connected := False;
    except
      ShowMessage('Server is not reachable');
    end;
    Result := True;
  finally
    wDb.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése