More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

Delphi: How to check whether a database alias exists and create or change it Options · View
hong
Posted: Wednesday, November 10, 2010 4:28:45 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 335
Points: 711
Location: Australia
The following code checks whether a database alias exists in BDE. If exists, it updates the alias parameters. Otherwise it creates alias. The Paradox database driver is used in this example.
Code:
var
  AliasList: TStringList;
  lDatabase: TDatabase;
  lSession :TSession;
begin
  lSession := TSession.Create(Application);
         With lSession do
         begin
            AutoSessionName := true;
            Active := true;
         end;

  lDatabase := TDatabase.Create(nil);
  AliasList := TStringList.Create;

  //If alias doesn't exist, add alias
  if (lSession.IsAlias('myDb') = false) then
  begin
      try
         with AliasList do
         begin
            Add(format('PATH=%s', [dbPath]));
         end;
         lSession.AddAlias('myDb', 'PARADOX', AliasList);
      finally
         AliasList.Free;
      end;
  end
  else
        //If alias exists, change the alias with a different path
        //lSession.GetAliasParams('myDb', AliasList);
        AliasList.Clear;
        AliasList.Add(format('PATH=%s', [dbPath]));
        lSession.ModifyAlias('myDb', AliasList);
        Session.SaveConfigFile;

      with lDatabase do
      begin
         //Configure database
         LoginPrompt := false;
         SessionName := lSession.SessionName;
         AliasName := 'myDb';
         DatabaseName := 'myDatabase';
         Connected := true;
      end;
Sponsor
Posted: Wednesday, November 10, 2010 4:28:45 PM
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.054 seconds.