15.2.2 TSession部件的方法:
TSession部件中的大部分方法是用于向用戶提供與應用程序相連接的數(shù)據(jù)庫的信息,如數(shù)據(jù)庫的名字及別名,數(shù)據(jù)庫中的表名以及數(shù)據(jù)庫引擎BDE的有關參數(shù)等,在設計數(shù)據(jù)庫應用程序時,想要獲取有關數(shù)據(jù)庫的信息,調用TSession部件的下列方法,將會大大簡化程序的設計。
GetAliasNames方法:調用該方法,我們可以獲得數(shù)據(jù)庫引擎BDE中定義的數(shù)據(jù)庫別名。
GetAliasParams方法:該方法主要用于獲取我們在BDE中定義數(shù)據(jù)庫別名時所說明的參數(shù)值,如BDE所在的目錄路徑以及實際名稱等。
GetDatabaseNames 方法:調用該方法可以幫助我們獲得當前應用程序可以進行連接的所有數(shù)據(jù)庫的名字,數(shù)據(jù)庫的名字是用戶使用BDE工具定義的實際數(shù)據(jù)庫的別名。
GetDriverNames方法:數(shù)據(jù)庫引擎BDE可以與多種數(shù)據(jù)庫管理系統(tǒng)相連接,如客戶/服務器數(shù)據(jù)庫管理系統(tǒng)Oracle、Sybase以及本地數(shù)據(jù)庫管理系統(tǒng)dBASE,Paradox等,BDE與每一種數(shù)據(jù)庫管理系統(tǒng)進行連接時,都有相應的驅動程序,而且這些驅動程序都可以選擇地安裝。通過調用GetDriverNames方法。我們可以獲得當前BDE安裝的數(shù)據(jù)庫驅動程序的名字。
GetDriverParams方法:BDE的數(shù)據(jù)庫驅動程序中包含著多個參數(shù),如支持的民族語言、DBMS的版本號、文件塊大小等,對于服務器上的DBMS,還有數(shù)據(jù)庫服務器的名字等等。
GetTableNames方法:因為每一個數(shù)據(jù)庫都是由多個數(shù)據(jù)庫表組成的,我們通過說明數(shù)據(jù)庫名,然后調用GetTableNames方法,便可以獲得該數(shù)據(jù)庫中全部的數(shù)據(jù)庫表的名字。
上述這些方法在調用時都需要一個字符串列表作為參數(shù),而且都返回一個字符串列表的值。
TSession部件還有一個叫DropConnections的方法用于控制應用程序與數(shù)據(jù)庫的連接,當調用DropConnections方法時,應用程序與所有的數(shù)據(jù)庫的連接將會切斷。
15.2.3 TSession部件應用舉例
例15.1:我們創(chuàng)建一個應用程序,通過調用TSession有關的方法獲取當前應用程序可以進行連接的數(shù)據(jù)庫的名字以及獲取其中任意一個數(shù)據(jù)庫中的全部數(shù)據(jù)庫表的名字。
通過TSession部件獲取數(shù)據(jù)庫的有關信息
窗體中主要使用了兩個列表框,其中列表框DatabaselistBox用于顯示數(shù)據(jù)庫的名字,列表框TablelistBox用于顯示數(shù)據(jù)庫中的表名。程序運行完后數(shù)據(jù)庫的名字顯示在DatabaselistBox列表框中,當用戶單擊DatabaselistBox列表框中的數(shù)據(jù)庫名時,該數(shù)據(jù)庫全部的數(shù)據(jù)庫表的名字將會顯示在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 }
相關推薦:2010年9月計算機等級考試試題及答案解析專題北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內蒙古 |