全國(guó)計(jì)算機(jī)等級(jí)考試二級(jí)Delphi輔導(dǎo)講義 148

字號(hào):

15.2.3 TSession部件應(yīng)用舉例
    例15.1:我們創(chuàng)建一個(gè)應(yīng)用程序,通過(guò)調(diào)用TSession有關(guān)的方法獲取當(dāng)前應(yīng)用程序可以進(jìn)行連接的數(shù)據(jù)庫(kù)的名字以及獲取其中任意一個(gè)數(shù)據(jù)庫(kù)中的全部數(shù)據(jù)庫(kù)表的名字。
    通過(guò)TSession部件獲取數(shù)據(jù)庫(kù)的有關(guān)信息
    窗體中主要使用了兩個(gè)列表框,其中列表框DatabaselistBox用于顯示數(shù)據(jù)庫(kù)的名字,列表框TablelistBox用于顯示數(shù)據(jù)庫(kù)中的表名。程序運(yùn)行完后數(shù)據(jù)庫(kù)的名字顯示在DatabaselistBox列表框中,當(dāng)用戶單擊DatabaselistBox列表框中的數(shù)據(jù)庫(kù)名時(shí),該數(shù)據(jù)庫(kù)全部的數(shù)據(jù)庫(kù)表的名字將會(huì)顯示在TablelistBox列表框中。有關(guān)的程序代碼如下:
    程序清單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 an alias is local }
    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 tables in the alias and put 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 }