执行 DbiWriteBlock 函数后 iRecords 变量没有变化

iRecords variable no change after execute DbiWriteBlock function

执行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;

变量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() 填充字段值。

下面是关于 DbiWriteBlock 函数的文档(来自 BDE 帮助文件):

Function definition:

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

Description:

DbiWriteBlock writes a block of records to the table associated with hCursor.

Parameters:

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.

Usage:

This function is similar to calling DbiAppendRecord for the specified number of piRecords. DbiWriteBlock can access data in blocks larger than 64Kb, depending on the size you allocate for the buffer.

Note:

This function cannot be used if the records contain non-empty BLOBs.

Paradox:

This function verifies any referential integrity requirements or validity checks that may be in place. If either fails, the write operation is canceled.

Completion state:

The cursor is positioned at the last record that was inserted.

Result:

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.

Example from Delphi 7 help:

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