help!!!(200分)

  • 主题发起人 主题发起人 tourist
  • 开始时间 开始时间
T

tourist

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在Delphi中对后台数据库进行物理删除(如:paradox)?
 
你是指删除库而不是删除记录吗?
 
简单的,DELETEFILE<br>不过,你一定要保证没人正在使用此数据库
 
直接删除文件.
 
如果只是想删除文件,什么都行了,好几种方法吧。
 
是想删除程序建立的临时库吧,程序启动后用FindFirstFile,FindNextFile将一个<br>Alias下的文件记录到一个TStringlist中,关闭程序前在检测如果有不在TStringLst<br>中的文件就用DeleteFile删除之。<br><br>FindFirstFile, TStringList, DeleteFile的用法都可以用问题检索在本论坛上找<br>到。
 
假如没人正在使用此数据库的话<br>用 DELETEFILE 删除, all of *.db or *.dbf<br>如果是网络数据库,用drop table 即可<br>
 
以下文字引自 http://www.borland.com/devsupport/bde/bdeapiex/index.html<br><br>This example will pack a Paradox or dBASE table therfore removing <br>already deleted rows in a table. This function will also regenerate <br>all out-of-date indexes (maintained indexes) This example uses the <br>following input:PackTable(Table1) <br><br>// Pack a Paradox or dBASE table<br>// The table must be opened execlusively before calling this function...<br>procedure PackTable(Table: TTable);<br>var<br>&nbsp; Props: CURProps;<br>&nbsp; hDb: hDBIDb;<br>&nbsp; TableDesc: CRTblDesc;<br><br>begin<br>&nbsp; // Make sure the table is open exclusively so we can get the db handle...<br>&nbsp; if Table.Active = False then<br>&nbsp; &nbsp; raise EDatabaseError.Create('Table must be opened to pack');<br>&nbsp; if Table.Exclusive = False then<br>&nbsp; &nbsp; raise EDatabaseError.Create('Table must be opened exclusively to pack');<br><br>&nbsp; // Get the table properties to determine table type...<br>&nbsp; Check(DbiGetCursorProps(Table.Handle, Props));<br><br>&nbsp; // If the table is a Paradox table, you must call DbiDoRestructure...<br>&nbsp; if Props.szTableType = szPARADOX then<br>&nbsp; begin<br>&nbsp; &nbsp; // Blank out the structure...<br>&nbsp; &nbsp; FillChar(TableDesc, sizeof(TableDesc), 0);<br>&nbsp; &nbsp; // &nbsp;Get the database handle from the table's cursor handle...<br>&nbsp; &nbsp; Check(DbiGetObjFromObj(hDBIObj(Table.Handle), objDATABASE, hDBIObj(hDb)));<br>&nbsp; &nbsp; // Put the table name in the table descriptor...<br>&nbsp; &nbsp; StrPCopy(TableDesc.szTblName, Table.TableName);<br>&nbsp; &nbsp; // Put the table type in the table descriptor...<br>&nbsp; &nbsp; StrPCopy(TableDesc.szTblType, Props.szTableType);<br>&nbsp; &nbsp; // Set the Pack option in the table descriptor to TRUE...<br>&nbsp; &nbsp; TableDesc.bPack := True;<br>&nbsp; &nbsp; // Close the table so the restructure can complete...<br>&nbsp; &nbsp; Table.Close;<br>&nbsp; &nbsp; // Call DbiDoRestructure...<br>&nbsp; &nbsp; Check(DbiDoRestructure(hDb, 1, @TableDesc, nil, nil, nil, FALSE));<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; // If the table is a dBASE table, simply call DbiPackTable...<br>&nbsp; &nbsp; if Props.szTableType = szDBASE then<br>&nbsp; &nbsp; &nbsp; Check(DbiPackTable(Table.DBHandle, Table.Handle, nil, szDBASE, TRUE))<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; // Pack only works on PAradox or dBASE; nothing else...<br>&nbsp; &nbsp; &nbsp; raise EDatabaseError.Create('Table must be either of Paradox or dBASE ' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'type to pack');<br><br>&nbsp; Table.Open;<br>end;
 
DeleteFile将连数据库的结构也给删除了。若要保留结构就不能用该过程!
 
这份又让yysun得了。
 
paradox表有许多相关文件,<br>就算用“DELETEFILE”删除, all of *.db or *.dbf<br>还可能有*.px、*.val、*.xg0、*.xg1........<br>实际上TTable提供了“正常”的方法删除表,<br>就是"Table1.DeleteTable"。<br>它的好处如下:<br>1。不管你的表有多少配套文件,统统删除,决不多删一个,也决不少删一个;<br>2。不管你的表是本地表,还是C/S表,都能搞定,而你的代码却差别不大;<br>3。保留Table1中的结构信息,可以马上重建新表(如果你开发时曾用字段编辑器<br>  或运行时通过编程将结构信息加入Table1中的话)。<br><br>我建议你能用VCL提供的“正常”途径解决问题,就不要用“非常规”的方法来做。<br>(原因:可移植、可读性、通用性、代码简洁。。。。。。。你也知道,不说了)<br>
 
仔细读读yysun的代码,那是怎么PackTable,不是删除表,<br>学过dBase或Fox的都知道PACK命令吧,<br>实际上对于Paradox和dBase表,当你执行Table1.Delete时,<br>真正的记录并未删除,直到你做PackTable(Paradox表)或Pack(类dBase语言)时,<br>才真正删除。<br>二者不同的是:对Paradox表执行Table1.Append(或Table1.Insert)时,若有已<br>删除且未PACK掉的记录,就利用其空间存放新记录,而dBase始终分配新空间,原<br>已删除且未PACK掉的记录,可用“RECALL”命令(类dBase语言)恢复。<br>yysun的这段程序就是干PACK那事儿的。<br>
 
时间太久,强制结束。 &nbsp; wjiachun<br>
 
后退
顶部