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