Getting aliases and table names
by Kent Reisdorph
Every VCL database application has a global variable called Session. This variable is an instance of the TSession class. (Although there is a TSession component on the component palette, it is rarely necessarily to use this component in your applications.) Among other things, the TSession class can be used to get a list of alias names, tables within a particular database, and even to get a list of stored procedures.
To get a list of aliases that are currently registered with the BDE Administrator, call the GetAliasNames() method:
TStringList* aliases = new TStringList; Session->GetAliasNames(aliases);
The single parameter passed to GetAliasNames() is an instance of TStrings. This means that you can easily add all alias names to any component that uses a TStrings instance (a list box, for example). This code does that:
Session->GetAliasNames(ListBox1->Items);
Once you have an alias you can easily get a list of tables contained in the database represented by that alias. This is done with the GetTableNames() method. For example:
TStringList* tables = new TStringList; Session->GetTableNames( "BCDEMOS", "", true, false, tables);
The first parameter is the name of the BDE alias for which you want a list of tables. The second parameter is used to specify a search pattern. You can use wildcard characters to limit the list of tables returned. To retrieve all tables, simply pass an empty string for this parameter. The third parameter is used to specify whether the returned table names include the file extension. This parameter only applies to Paradox and dBase aliases. It is ignore for other database types. The fourth parameter is used to specify whether you want system tables returned as well as user tables. Usually you will pass false for this parameter, since you are not usually interested in system tables. The final parameter is used to specify the TStrings instance (a TStringList, for example) that will contain the table names when the call to GetTableNames() returns.
The GetStoredProcNames() method works similar to GetTableNames(). As its name implies, it is used to get a list of stored procedures contained on a database server.
TSession has a number of other useful utility functions. See the VCL help for TSession for more information.