Author: Tomas Rutkauskas
Copy/delete a BDE table
Answer:
Here is an example of a routine that I use for copying and deleting tables. It uses
DB, DBTables, DbiProcs,DbiErrs, and DbiTypes.
You simply provide the directory to copy from, the source table name, the directory
to copy to, and the destination table name, and the BDE will copy the entire table,
indexes and all to the new file.
The delete function takes the path to delete from and the name of the table to
delete, the BDE takes care of deleting all associated files (indexes, etc.).
These procedures have been pulled off a form of mine, and I've edited them to
remove some dependencies that existed with that form. They should now be completely
stand-alone.
1 procedure TConvertForm.CopyTable(FromDir, SrcTblName, ToDir, DestTblName:
2 string);
3 var
4 DBHandle: HDBIDB;
5 ResultCode: DBIResult;
6 Src, Dest, Err: array[0..255] of Char;
7 SrcTbl, DestTbl: TTable;
8 begin
9 SrcTbl := TTable.Create(Application);
10 DestTbl := TTable.Create(Application);
11 try
12 SrcTbl.DatabaseName := FromDir;
13 SrcTbl.TableName := SrcTblName;
14 SrcTbl.Open;
15 DBHandle := SrcTbl.DBHandle;
16 SrcTbl.Close;
17 ResultCode := DbiCopyTable(DBHandle, false,
18 StrPCopy(Src, FromDir + '\' + SrcTblName), nil,
19 StrPCopy(Dest, ToDir + '\' + DestTblName));
20 if ResultCode <> DBIERR_NONE then
21 begin
22 DbiGetErrorString(ResultCode, Err);
23 raise EDatabaseError.Create('While copying ' +
24 FromDir + '\' + SrcTblName + ' to ' +
25 ToDir + '\' + DestTblName + ', the '
26 + ' database engine generated the error '''
27 + StrPas(Err) + '''');
28 end;
29 finally
30 SrcTbl.Free;
31 DestTbl.Free;
32 end;
33 end;
34
35 procedure TConvertForm.DeleteTable(Dir, TblName: string);
36 var
37 DBHandle: HDBIDB;
38 ResultCode: DBIResult;
39 tbl, Err: array[0..255] of Char;
40 SrcTbl, DestTbl: TTable;
41 begin
42 SrcTbl := TTable.Create(Application);
43 try
44 SrcTbl.DatabaseName := Dir;
45 SrcTbl.TableName := TblName;
46 SrcTbl.Open;
47 DBHandle := SrcTbl.DBHandle;
48 SrcTbl.Close;
49 ResultCode := DbiDeleteTable(DBHandle,
50 StrPCopy(Tbl, Dir + '\' + TblName), nil);
51 if ResultCode <> DBIERR_NONE then
52 begin
53 DbiGetErrorString(ResultCode, Err);
54 raise EDatabaseError.Create('While deleting ' +
55 Dir + '\' + TblName + ', the database ' +
56 'engine generated the error ''' + StrPas(Err) + '''');
57 end;
58 finally
59 SrcTbl.Free;
60 end;
61 end;
|