错误复制DBF / MDX文件

时间:2011-02-07 22:34:21

标签: delphi dbf

我使用以下代码将dbf / mdx文件从一个文件夹复制到另一个文件夹:

procedure TfrmMain.MyCopyFile(S1, S2: string);
begin
   if not FileExists(S2) then
      CopyFile(PCHAR(S1), PCHAR(S2), true)
   else
      if Application.MessageBox(PCHAR('Overwrite existing file ' + S2 + '?'), 'File exists in folder',MB_YESNO + MB_DEFBUTTON1) = IDYES
          then CopyFile(PCHAR(S1), PCHAR(S2), false)
end;

当表名保持不变时,代码工作正常。

如果我更改了表格的名称:

    MyCopyFile(CurPath + '\orders.dbf', NewPath + '\ordly.dbf');
    MyCopyFile(CurPath + '\orders.mdx', NewPath + '\ordly.mdx');

当我尝试打开 ordly.dbf 时,收到错误消息:

  

损坏的表/索引标题。
   文件:C:\ DATA \ 2011 \ ORDLY.MDX

1 个答案:

答案 0 :(得分:7)

问题是由于mdx格式存储在与之关联的数据文件的名称(表名)中。因为当您重命名mdx文件时,索引仍然指向数据文件的名称。

检查此链接以查看mdx文件的结构。

The Structure of Multiple Index files (*.mdx)

 0 | Version number      *1|  ^
   |-----------------------|  |
 1 | Date of creation      |  |
 2 |      YYMMDD           |  |
 3 |                       |  |
   |-----------------------|  |
 4 | Data file name        | File
 5 | (no extension)        | Header
   :                       :  |
   :                       :  |
19 |                       |  |
   |-----------------------|  |
20 | Block size            |  |
   |                       |  |
   |-----------------------|  |
22 | Block size adder N    |  |
   |                       |  |
   |-----------------------|  |
24 | Production index flag |  |
   |-----------------------|  |
25 | No. of entries in tag |  | *2
   |-----------------------|  |
26 | Length of tag         |  | *3
   |-----------------------|  |
27 | (Reserved)            |  |
   |-----------------------|  |
28 | No.of tags in use     |  |
   |                       |  |
   |-----------------------|  |
30 | (Reserved)            |  |
   |                       |  |
   |-----------------------|  |
32 | No.of pages in tagfile|  |
   |                       |  |
   |                       |  |
35 |                       |  |
   |-----------------------|  |
36 | Pointer to first free |  |
   | page                  |  |
   |                       |  |
39 |                       |  |
   |-----------------------|  |
40 | No.of block available |  |
   |                       |  |
   |                       |  |
43 |                       |  |
   |-----------------------|  |
44 | Date of last update   |  |
   |      YYMMDD           |  |
46 |                       |  |
   |-----------------------|  |
47 | (Reserved)            |  |
   |-----------------------|  |
48 | (Garbage)             |  |
   :                       :  |
   :                       :  |
   |                       |  |        ___|=======================|
543|                       | _V___    / 0 | Tag header page no.   |
   |-----------------------|  |      /    |                       |
544| Tag table entries     | Tag    /     |                       |
   |                       | Table |    3 |                       |
   :.......................:  |    |      |-----------------------| Tag
   :                       :  |    |    4 | Tag name              | table
   :.......................:  |    |      :                       :
   :                       :  |   /       :                       :
   :                       :  |  /        |                       |
   :.......................:__|_/      14 |                       |
   :                       :  |           |-----------------------|
   :                       :  |        15 | Key format         *4 |
   :                       :  |           |-----------------------|
   :.......................:__|_       16 | Forward tag thread (<)|
   :                       :  | \         |-----------------------|
   :                       :  |  \     17 | Forward tag thread (>)|
   :                       :  |   \       |-----------------------|
   :                       :  |    |   18 | Backward tag thread *5|
   |                       |  |    |      |-----------------------|
   |                       |  |    |   19 | (Reserved)            |
M*N|                       |__V__  |      |-----------------------|
   |=======================|  ^    |   20 | Key type           *6 |
  0| Pointer to root page  |  |    |      |-----------------------|
   |                       |  |    |   21 | (Reserved)            |
   |                       |  |    |      :                       :
  3|                       |  |    |      :                       :
   |-----------------------|  |    |   31 |                       |
  4| File size in pages    | Tag   |      |-----------------------|
   |                       | header|   32 | (Garbage)             |
   |                       |  |    |      :                       :
  7|                       |  |    |      |                       |
   |-----------------------|  |     \  N  |                       |
  8| Key format         *7 |  |      \____|=======================|
   |-----------------------|  |
  9| Key type           *8 |  |
   |-----------------------|  |
 10| (Reserved)            |  |
   |                       |  |
   |-----------------------|  |
 12| Index key length   *9 |  |
   |                       |  |
   |-----------------------|  |
 14| Max.no.of keys/page   |  |
   |                       |  |
   |-----------------------|  |
 16| Secondary key type *10|  |
   |                       |  |
   |-----------------------|  |
 18| Index key item length |  |
   |                       |  |
   |-----------------------|  |
 20| (Reserved)            |  |
   |                       |  |
   |                       |  |
   |-----------------------|  |
 23| Unique flag           |  |
   |-----------------------|  |
   |                       |  |
   :                       :  |
   :                       :__V__
N*M|=======================|