1. Computing
Send to a Friend via Email

Manipulating BDE Aliases from Code

By

In order to access a data source, a Delphi application can use Data Access components to establish a connection through the BDE. The BDE uses an Alias to access a particular database. The terms alias and database are synonymous when talking about the BDE (some experienced users may say that I'm wrong here).

By using an Alias you can change the physical location to the data, say from a local drive to a network drive, without having to recompile the application. The alias is set up in the BDE Configuration Utility and specifies driver parameters and database locations, such as Driver Type, Server Name, User Name and others.

Simply put, an alias is just a name for a given path, for example, the alias name MYDATA, may be the alias name for the directory "C:\MyApplication\Data."

The advantage to using aliases is that you don't have to hard code (and recompile) directory paths in your applications if a table is moved to a different directory or a different disk. The application only needs to know the Alias for the database and it will have access to all data in that database.

Working with Aliases from Code

Each time you start a database application, the BDE sets up a global TSession object called Session. If you do not use any of the database components, you will have to manually add the DBTables unit to have access to the Session variable. Alternatively, you could add your own TSession variable to your project - TSession is a component in the Data Access tab of the component palette.

Listing Aliases
While developing database applications we will often need to know what BDE aliases exist on a given computer. This is handy when we want to enable our users to choose an alias from a list.

 //Place one TButton and one TComboBox on a form.
 uses DBTables;
 //Handles Button1's OnClick event
 procedure TForm1.Button1Click(Sender: TObject) ;
 var
   CurrentAliases: TStringList;
 begin
   CurrentAliases := TStringList.Create;
   try
     Session.GetAliasNames(CurrentAliases) ;
     ComboBox1.Items:= CurrentAliases;
   finally
     CurrentAliases.Free;
   end;
 end; 

Is this an Alias? What Directory?[/br] Many times you will just need to know if a given string represents an alias name. The following method (IsAlias) of a TSession object will test if a given string value corresponds to an existing BDE database alias known to the session.

 uses DBTables;
 ...
 var sMaybeAlias : string;
 ...
 Session.IsAlias(sMaybeAlias) 
When you know that sMaybeAlias represents an alias here's how to see the database location:
 uses dbtables;
 
 procedure TForm1.Button1Click(Sender: TObject) ;
 var
   sMaybeAlias : string;
   DBPath : string;
   ParamList : TStringList;
 begin
   sMaybeAlias := 'DBDEMOS';
 
   if Session.isAlias(sMaybeAlias) then
   begin
     ParamList := TStringList.Create;
     try
       Session.GetAliasParams(sMaybeAlias, ParamList) ;
       DBPath := ParamList.Values['PATH'];
     finally
      ParamList.Free;
     end;
     ShowMessage('Path to ' + sMaybeAlias + ' is ' + DBPath) ;
   end
   else
     ShowMessage(sMaybeAlias + ' is not an alias!') ;
 end; 

Creating an Alias
There are several ways to create an alias:

  • Through the BDE Administrator utility (located in Control Panel)
  • Through the Database Desktop program,
  • Through the SQL Explorer,
  • Through code at runtime.
In this article, we'll stick to the create-from-code technique.

First will look at another method of a TSession object: AddStandardAlias. Call to AddStandardAlias at runtime will create a session-specific standard (for Paradox, dBASE or ASCII tables) BDE alias.

The following code will add a standard alias for Paradox tables:

 sNewAlias := 'MyNewParadoxAlias';
 sDataDir := 'c:\MyApplication\NewDataDirectory';
 Session.AddStandardAlias(sNewAlias, sDataDir, 'PARADOX') ;
 
Note: "Aliases created within a session are not automatically saved to the BDE configuration file. To save an alias to the configuration file, call the SaveConfigFile method."

If you prefer working with InterBase than the AddAlias method will be used. This time we will save the alias permanently (Session.SaveConfigFile).

 uses dbtables;
 procedure TForm1.Button1Click(Sender: TObject) ;
 var AliasParams : TStringList;
      AliasDirectory : string;
      AliasName : string;
 begin
   AliasDirectory:='c:\MyApp\Data\IBData.GDB';
   AliasName:='IBNewData';
 
   AliasParams := TStringList.create;
   try
     AliasParams.Values['SERVER NAME'] := DataFile;
     AliasParams.Values['USER NAME'] := 'SYSDBA';
     Session.AddAlias(AliasName, 'INTERBASE', AliasParams) ;
     Session.SaveConfigFile;
   finally
     AliasParams.Free;
   end; 

More BDE related articles can be located here: BDE Delphi programming

©2014 About.com. All rights reserved.