三、一個(gè)實(shí)例 在Form1.FormCreate事件中寫(xiě)入如下代碼,程序啟動(dòng)時(shí),將自動(dòng)檢測(cè)是否存在數(shù)據(jù)庫(kù)別名Cntssamp,假如沒(méi)有則建立之;自動(dòng)檢測(cè)別名Cntssamp中是否存在表格TSK(圖書(shū)庫(kù)),假如沒(méi)有則自動(dòng)建立表格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不存在,現(xiàn)在創(chuàng)建嗎?,BDE信息窗口,mb—OKCancel);{增加一個(gè)名為Cngzsamp的數(shù)據(jù)庫(kù)別名} if answer=IDCANCEL then begin ap.Free; Exit; end;
Session.AddStandardAlias(Cntssamp,c:/delphp11,Paradox); Session.SaveConfigFile; {BDE配置文件存盤(pán)} end ;
ap.Clear; {取得別名Cngzsamp中的所有表格名稱(chēng)列表} Session.GetTableNames(Cntssamp,,False,False,ap); if (ap.IndexOf(TSK)=-1) then {判定表格是否存在} begin answer:=Application.MessageBox(別名Cntssamp中不存在表格TSK,現(xiàn)在創(chuàng)建嗎?,表格信息窗口,mb—OKCancel); if answer=IDCANCEL then
begin ap.Free; Exit; end;
with table1 do begin Active:=false; DatabaseName:=Cntssamp; {數(shù)據(jù)庫(kù)別名} TableName:=TSK; {表格名} TableType:=ttParadox; {數(shù)據(jù)庫(kù)類(lèi)型} with FieldDefs do begin {增加字段} Clear; Add(SH,ftString,30,False); {書(shū)號(hào) String(30)} Add(SM,ftString,30,False); {書(shū)名 String(30)} Add(CBS,ftString,20,False); {出版社 String(20)} Add(CBRQ,ftDate,0,False); {出版日期 Date} Add(YS,ftInteger,0,False); {頁(yè)數(shù) Integer} end;
with IndexDefs do begin {增加索引} Clear; {按書(shū)號(hào)字段建立主索引} Add(SHSY,SH,[Primary,ixUnique]); end;