执行DbiWriteBlock函数后,iRecords变量保持不变

时间:2018-09-06 12:13:56

标签: delphi bde

执行DbiWriteBlock函数后,iRecords变量的值不变。请给我解释一下。谢谢!

这是我的代码:

procedure TMainForm.btnBDICheckClick(Sender: TObject);
var
  Table       : TTable;
  PTable      : PByte;
  RecordSize  : Integer;
  RecordCount : Integer;
  iRecords    : Integer;
begin
  Table              := TTable.Create(Self);
  Table.DatabaseName := 'D:\Temp';
  Table.TableName    := 'SrcTable.db';
  Table.Active       := True;

  RecordSize  := Table.RecordSize;
  RecordCount := Table.RecordCount;

  PTable   := nil;
  iRecords := 0;

  GetMem(PTable, RecordSize * RecordCount);
  DbiWriteBlock(Table.Handle, iRecords, PTable);

  // iRecords = 0 at here

  Table.Close;
end;

1 个答案:

答案 0 :(得分:2)

说明:

变量iRecords是指向要写入的记录数的指针。输出时,iRecords将写入实际的记录数。您的代码应如下所示:

procedure TMainForm.btnBDICheckClick(Sender: TObject);
var
  Table       : TTable;
  PTable      : PByte;
  RecordSize  : Integer;
  RecordCount : Integer;
  iRecords    : Integer;
begin
   Table              := TTable.Create(Self);
   Table.DatabaseName := 'D:\Temp';
   Table.TableName    := 'SrcTable.db';
   Table.Active       := True;

   RecordSize  := Table.RecordSize;
   RecordCount := Table.RecordCount;

   //PTable   := nil;
   //iRecords := 0;

   iRecords := RecordCount;
   GetMem(PTable, RecordSize * RecordCount);
   DbiWriteBlock(Table.Handle, iRecords, PTable);

   Table.Close;
   ShowMessage('Records: ' + IntToStr(iRecords));
end;

使用此代码,您将添加空记录。使用DbiInitRecord()DbiPutField()填写字段值。

  

下面是BDE帮助文件中的文档:

     

功能定义:

function DbiWriteBlock (hCursor: hDBICur; var iRecords: Longint; pBuf: Pointer): DBIResult stdcall;
     

说明:

     

DbiWriteBlock将记录块写入到与   hCursor。

     

参数:

hCursor Type: hDBICur (Input) Specifies the cursor handle to the table.
piRecords Type: pUINT32 (Input/Output) On input, piRecords is a pointer to the number of records to write. On output, pointer to the client variable that receives the actual number of records written. The number actually written may be less than requested if an integrity violation or other error occurred.
pBuf Type: pBYTE (Input) Pointer to the buffer containing the records to be written.
     

用法:

     

此函数类似于为指定的对象调用DbiAppendRecord   piRecords的数量。 DbiWriteBlock可以访问更大的块中的数据   大于64Kb,具体取决于您为缓冲区分配的大小。

     

注意:

     

如果记录包含非空的BLOB,则不能使用此功能。

     

Paraxx:

     

此功能验证所有参照完整性要求或   可能进行的有效性检查。如果任何一个失败,则写   操作被取消。

     

完成状态:

     

光标位于插入的最后一条记录上。

     

结果:

DbiResult                   Meaning
DBIERR_NONE                 The block of records contained in pBuf has been successfully written to the table specified by hCursor.
DBIERR_INVALIDHNDL          The specified cursor handle is invalid or NULL, or piRecords is NULL, or pBuf is NULL.
DBIERR_TABLEREADONLY        The table is opened read-only; cannot write to it.
DBIERR_NOTSUFFTABLERIGHTS   Insufficient table rights to insert a record. (Paradox only.)
DBIERR_NODISKSPACE          Insertion failed due to insufficient disk space.
     

Delphi 7帮助中的示例:

procedure fDbiWriteBlock(Customer: TTable; var RecordsToInsert: Longint);

var
  pRecordsBuf, pTmpBuf: pBYTE;
  Rec: Longint;
  CustNo: Double;
begin
  Randomize;
  GetMem(pRecordsBuf, Customer.RecordSize * RecordsToInsert);
  pTmpBuf := pRecordsBuf;
  try
    for Rec := 1 to RecordsToInsert do begin
      CustNo := Random(1000000);
      // Iterate through the entire record buffer filling each
      // individual record with information
      with Customer do begin
        Check(DbiInitRecord(Handle, pTmpBuf));

        Check(DbiPutField(Handle, FieldByName('CustNo').Index + 1, pTmpBuf,
          pBYTE(@CustNo)));
        Check(DbiPutField(Handle, FieldByName('Company').Index + 1, pTmpBuf,
          PChar('INPRISE Corporation')));
        Inc(pTmpBuf, RecordSize);
      end;
    end;
    Check(DbiWriteBLock(Customer.Handle, RecordsToInsert, pRecordsBuf));
  finally
    FreeMem(pRecordsBuf, Customer.RecordSize * RecordsToInsert);
  end;
end