Enumerating Database Names on an SQL Server in Delphi

Here's the code:

procedure TOpenSQLServerForm.DatabasesOnServer(Databases : TStrings);
var
rs : _RecordSet;
begin
Databases.Clear;
with TAdoConnection.Create(nil) do
try
//simple ConnectionString without the DB name
ConnectionString := ConnStr;
LoginPrompt := False;
try
Open;
rs := ConnectionObject.OpenSchema(
adSchemaCatalogs,
EmptyParam,
EmptyParam);
with rs do
begin
try
Databases.BeginUpdate;
while not Eof do
begin
Databases.Add(
VarToStr(Fields['CATALOG_NAME'].Value));
MoveNext;
end;
finally
Databases.EndUpdate;
end;
end;
Close;
except
on e:exception do
MessageDlg(e.Message,mtError, [mbOK],0);
end;
finally
Free;
end;
end;

Komentar