2007. április 21., szombat

How to assign a new path to a TTable at runtime


Problem/Question/Abstract:

How to assign a new path to a TTable at runtime

Answer:

Use a TDataBase with a custom, application-specific alias. Set the Alias property to an empty string, select a DriverName, and insert the string 'PATH=C:\MYPATH' into the Params property.

Now any TTable etc. of your project can see an alias of the name you choose for the DataBaseName property of TDataBase. At runtime you can assign a new path at a single place. You have to re-open the tables, however. Like this:

procedure AssignDBDir(ADataBase: TDataBase; const ADir: string);
begin
  with ADataBase do
    if (Params.Count = 0) or (Params[0] <> 'PATH=' + ExtractFilePath(AFileName)) then
    begin
      if Connected then
        Close; {closes all tables as well}
      DriverName := 'STANDARD'; {clears any alias as well}
      Params.Clear;
      Params.Add('PATH=' + ADir);
      Open; {reopen tables here}
    end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése