Welcome

首页 / 软件开发 / Delphi / 第十五章-数据访问部件的应用及编程(一)(2)

第十五章-数据访问部件的应用及编程(一)(2)2007-05-0715.2.3 TSession部件应用举例

例15.1:我们创建一个应用程序,通过调用TSession有关的方法获取当前应用程序可以进行连接的数据库的名字以及获取其中任意一个数据库中的全部数据库表的名字。

通过TSession部件获取数据库的有关信息

窗体中主要使用了两个列表框,其中列表框DatabaselistBox用于显示数据库的名字,列表框TablelistBox用于显示数据库中的表名。程序运行完后数据库的名字显示在DatabaselistBox列表框中,当用户单击DatabaselistBox列表框中的数据库名时,该数据库全部的数据库表的名字将会显示在TablelistBox列表框中。有关的程序代码如下:

程序清单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 }