三、一個實例 在Form1.FormCreate事件中寫入如下代碼,程序啟動時,將自動檢測是否存在數據庫別名Cntssamp,假如沒有則建立之;自動檢測別名Cntssamp中是否存在表格TSK(圖書庫),假如沒有則自動建立表格TSK。 PRocedure TForm1.FormCreate(Sender: TObjec var ap:TStringList; {字符串列表變量} answer:Integer; begin ap:=TStringlist.Create; Session.GetAliasNames(ap); {取得別名列表} if (ap.IndexOf(Cntssamp)=-1) then {判定別名是否存在} begin answer:=application.MessageBox(別名Cntssamp不存在,現在創建嗎?,BDE信息窗口,mb—OKCancel);{增加一個名為Cngzsamp的數據庫別名} if answer=IDCANCEL then begin ap.Free; Exit; end;
Session.AddStandardAlias(Cntssamp,c:/delphp11,Paradox); Session.SaveConfigFile; {BDE配置文件存盤} end ;
ap.Clear; {取得別名Cngzsamp中的所有表格名稱列表} Session.GetTableNames(Cntssamp,,False,False,ap); if (ap.IndexOf(TSK)=-1) then {判定表格是否存在} begin answer:=Application.MessageBox(別名Cntssamp中不存在表格TSK,現在創建嗎?,表格信息窗口,mb—OKCancel); if answer=IDCANCEL then
begin ap.Free; Exit; end;
with table1 do begin Active:=false; DatabaseName:=Cntssamp; {數據庫別名} TableName:=TSK; {表格名} TableType:=ttParadox; {數據庫類型} with FieldDefs do begin {增加字段} Clear; Add(SH,ftString,30,False); {書號 String(30)} Add(SM,ftString,30,False); {書名 String(30)} Add(CBS,ftString,20,False); {出版社 String(20)} Add(CBRQ,ftDate,0,False); {出版日期 Date} Add(YS,ftInteger,0,False); {頁數 Integer} end;
with IndexDefs do begin {增加索引} Clear; {按書號字段建立主索引} Add(SHSY,SH,[Primary,ixUnique]); end;