首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi TSession 實戰應用:動態列舉資料庫別名與資料表名稱完整範例

     編輯:Delphi

15.2.3 TSession 應用範例:資料庫瀏覽器雛形

建立表單含兩個 TListBox:DatabaseListBox 顯示資料庫別名,TableListBox 顯示所選資料庫的所有資料表。

核心程式碼

procedure TQueryForm.FormCreate(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  with DatabaseListBox do
  begin
    Items.Clear;
    Session.GetAliasNames(Items);  // 取得所有 BDE 別名
  end;
  Screen.Cursor := crDefault;
  if DatabaseListBox.Items.Count < 1 then
    MessageDlg('目前未定義任何資料庫別名...', mtError, [mbOK], 0);
  ComboBox1.ItemIndex := 0;  // 預設查詢類型
end;

procedure TQueryForm.DatabaseListBoxClick(Sender: TObject);
var
  AliasName: string;
begin
  AliasName := DatabaseListBox.Items[DatabaseListBox.ItemIndex];
  TableListBox.Items.Clear;
  Session.GetTableNames(AliasName, '',      // 別名、樣式(空=全部)
    CheckBox1.Checked,  // 顯示副檔名
    CheckBox2.Checked,  // 顯示系統表
    TableListBox.Items);
  if TableListBox.Items.Count < 1 then
    MessageDlg('所選別名無資料表...', mtError, [mbOK], 0);
end;

關鍵方法說明

  • Session.GetAliasNames(Strings):填入所有 BDE 定義的資料庫別名
  • Session.GetTableNames(Alias, Pattern, ShowExt, ShowSys, Strings):列舉指定別名下的資料表,支援萬用字元篩選、副檔名/系統表顯示開關

此範例展示 TSession 全域方法無需元件實例即可呼叫,適合建立動態資料庫探索介面。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved