W FAQ mamy prosty sposób jak odczytać nazwę komputera, a jak odczytać nazwę instacji MSSQL Server?
Aby połączyć się z MSSQL muszę podać: nazwa_komputera_z_mssql\instancja jak w delphi to odczytać?
Metod jest kilka. Ja znam siedem. Możesz na przykład użyć SQLPing. Poniżej fragment kodu (nie mój - gdzieś znalazłem te ideę coś z 8 lat temu i tylko trochę przerobiłem)
var i, k, c: Integer; P, T, D, Peer, S, REC, Mach, Inst: String;
UDP: TIdUDPClient; recList: TStringList; Port: TIdPort;
begin
try
recList := TStringList.Create;
if (Length(FMachine) = 0) then exit;
try
UDP := TidUDPCLient.Create(nil);
try
UDP.Host := FMachine;
UDP.Port := 1434;
UDP.BroadcastEnabled := True;
UDP.ReceiveTimeout := 6000;
UDP.IPVersion := Id_IPv4;
c := 0;
if Terminated then exit;
while (c < 4) do begin
try
UDP.Send(#2);
except exit; end;
repeat
try
REC := UDP.ReceiveString(Peer, Port);
except exit; end;
if (Length(REC) < 6) then continue;
recList.Add(copy(REC, 4, MaxInt));
until (REC = '');
if (recList.Count > 0) then break else Inc(c);
end;
for i := 0 to (recList.Count - 1) do begin
REC := recList[i];
if ((REC = ';') or (REC = ';;')) then continue;
repeat
k := pos(';;', REC);
if (k < 0) then begin
S := REC;
REC := '';
end else begin
S := copy(REC, 0, k - 1);
REC := copy(REC, k + 2, MaxInt);
end;
while true do begin
if Terminated then exit;
P := AnsiUpperCase(StrToken(S, ';'));
if (P = '') then begin
if (Length(Mach + Inst) > 0) then begin
if (Length(Inst) > 0) then Mach := Mach + '\' + Inst;
Searcher.AddServer(Mach);
end;
break;
end else T := P;
D := StrToken(S, ';');
if (T = 'SERVERNAME') then Mach := D else
if (T = 'INSTANCENAME') then begin
if (AnsiUpperCase(D) <> 'MSSQLSERVER') then Inst := D;
end;
end;
until (REC = '');
end;
finally
try UDP.Free; except end;
end;
finally
recList.Free;
end;
except end;
Pytamy się tu komputera FMachine o serwery SQL i przekazujemy znalezione szukaczowi, czyli Searcher.AddServer(Mach);
Metod jest kilka. Ja znam siedem. Możesz na przykład użyć SQLPing. Poniżej fragment kodu (nie mój - gdzieś znalazłem te ideę coś z 8 lat temu i tylko trochę przerobiłem)
var i, k, c: Integer; P, T, D, Peer, S, REC, Mach, Inst: String;
UDP: TIdUDPClient; recList: TStringList; Port: TIdPort;
begin
try
recList := TStringList.Create;
if (Length(FMachine) = 0) then exit;
try
UDP := TidUDPCLient.Create(nil);
try
UDP.Host := FMachine;
UDP.Port := 1434;
UDP.BroadcastEnabled := True;
UDP.ReceiveTimeout := 6000;
UDP.IPVersion := Id_IPv4;
c := 0;
if Terminated then exit;
while (c < 4) do begin
try
UDP.Send(#2);
except exit; end;
repeat
try
REC := UDP.ReceiveString(Peer, Port);
except exit; end;
if (Length(REC) < 6) then continue;
recList.Add(copy(REC, 4, MaxInt));
until (REC = '');
if (recList.Count > 0) then break else Inc(c);
end;
for i := 0 to (recList.Count - 1) do begin
REC := recList[i];
if ((REC = ';') or (REC = ';;')) then continue;
repeat
k := pos(';;', REC);
if (k < 0) then begin
S := REC;
REC := '';
end else begin
S := copy(REC, 0, k - 1);
REC := copy(REC, k + 2, MaxInt);
end;
while true do begin
P := AnsiUpperCase(StrToken(S, ';'));
if (P = '') then begin
if (Length(Mach + Inst) > 0) then begin
if (Length(Inst) > 0) then Mach := Mach + '\' + Inst;
Searcher.AddServer(Mach);
end;
break;
end else T := P;
D := StrToken(S, ';');
if (T = 'SERVERNAME') then Mach := D else
if (T = 'INSTANCENAME') then begin
if (AnsiUpperCase(D) <> 'MSSQLSERVER') then Inst := D;
end;
end;
until (REC = '');
end;
finally
try UDP.Free; except end;
end;
finally
recList.Free;
end;
except end;
Pytamy się tu komputera FMachine o serwery SQL i przekazujemy znalezione szukaczowi, czyli Searcher.AddServer(Mach);
Jaki moduł jest potrzebny dla zmiennych: TIdUDPClient oraz TIdPort?
FMachine?
TIdUDPClient jest w IdUDPClient.pas. TIdPort jest w IdGlobal.pas. Przynajmniej w tym pakiecie INDY, który mam zainstalowany u siebie. FMachine: String -> zawiera nazwę maszyny, na której szukasz serwerów M$ SQL. Wstaw sobie tę nazwę, którą znalazłeś w jakimś FAQ (nie wiem w jakim, ale skoro napisałeś, że znalazłeś, to przecież wiesz, w jakim).