15.2.3 tsession Parts Application Examples
Example 15.1: We create an application to get the name of the database that the current application can connect to and get the names of all the database tables in any of the databases by invoking the TSession method.
Get information about the database through the tsession part
The form mainly uses two list boxes, where the list box Databaselistbox is used to display the name of the database, and the list box Tablelistbox is used to display the name of the table in the database. After the program is run, the name of the database appears in the Databaselistbox list box, and when the user clicks the name of the database in the Databaselistbox list box, the name of the database table for the database is displayed in the Tablelistbox list box. The relevant program code is as follows:
Program Listing 15.1
Unit unit31;
Interface
Uses
Sysutils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Stdctrls, DB, Dbtables, Buttons, Comctrls, TABNOTBK;
Type
Tqueryform = Class (Tform)
BITBTN1:TBITBTN;
Datasource1:tdatasource;
table1:ttable;
Groupbox1:tgroupbox;
Checkbox1:tcheckbox;
Checkbox2:tcheckbox;
Pagecontrol1:tpagecontrol;
Tabsheet1:ttabsheet;
Label1:tlabel;
Label2:tlabel;
Label3:tlabel;
Listbox1:tlistbox;
Listbox2:tlistbox;
Listbox3:tlistbox;
Tabsheet2:ttabsheet;
Memo1:tmemo;
Procedure Formcreate (Sender:tobject);
Procedure Listbox1click (Sender:tobject);
Procedure Listbox2click (Sender:tobject);
End
Var
Queryform:tqueryform;
Implementation
{$R *. DFM}
Uses rsltform;
Procedure Tqueryform.formcreate (Sender:tobject);
Begin
Screen.cursor: = Crhourglass;
{Populate the alias list}
With ListBox1 do
Begin
Items.clear;
Session.getaliasnames (Items);
End
{Make sure there are aliases defined}
Screen.cursor: = Crdefault;
If ListBox1.Items.Count < 1 Then
Messagedlg (' There are no database aliases currently defined. You ' +
' Need at least-one alias to-use this demonstration ',
Mterror, [Mbok], 0);
End
Procedure Tqueryform.listbox1click (Sender:tobject);
Var
strvalue:string; {holds the alias selected by the user}
Bislocal:boolean; {Indicates whether or not a alias
Slparams:tstringlist; {holds the parameters of the selected alias}
Icounter:integer; {An integer counter variable for loops}
Begin
{Determine the alias name selected by the user}
With ListBox1 do
strvalue: = Items.strings[itemindex];
{Get the names of the "the" tables in the "alias" them in the
Appropriate list box, making sure the user ' s choices are reflected
In the list. }
ListBox2.Items.Clear;
Session.gettablenames (strvalue, {alias to enumerate}
', {pattern to match}