在用Delphi編寫數據庫程序時
二
作為數據報存儲控件
基於此
三
編寫如下代碼單元
unit IbDbFile;
interface
Uses Windows
Const
Flag =
Type
TDsException = Class(Exception);
TIbStorage = class(TComponent)
private
FRptTitle: string; //存儲數據報說明
FPageHead: string; //頁頭說明
FPageFoot: string; //爺腳說明
FFieldNames: TStrings; //字段名表
FStreamIndex: TStrings; //字段索引
FStream: TStream; //存儲字段內容的流
FFieldCount: Integer; //字段數
FRecordCount: Integer; //記錄數
FOpenFlag: Boolean; //流是否創建標志
protected
procedure Reset; //復位
procedure SaveHead(ADataSet: TDataSet; Fp: TStream); //存儲報表頭信息
procedure LoadTableToStream(ADataSet: TDataSet); //存儲記錄數據
procedure IndexFields(ADataSet: TDataSet); //將數據集的字段名保存到列表中
procedure GetHead(Fp: TFileStream); //保存報表頭信息
procedure GetIndex(Fp: TFileStream); //建立記錄流索引
procedure GetFieldNames(Fp: TFileStream); //從流中讀入字段名表
function GetFieldName(AIndex: Integer): string; //取得字段名稱
function GetFieldDataType(AIndex: Integer): TFieldType;
function GetDisplayLabel(AIndex: Integer): string; //取得字段顯示名稱
procedure SaveFieldToStream(AStream: TStream; AField: TField); //將字段存入流中
function GetFieldValue(ARecordNo
public
Constructor Create(AOwner: TComponent);
Destructor Destroy; override;
procedure Open; //創建流以准備存儲數據
procedure SaveToFile(ADataSet: TDataSet; AFileName: string); //存儲方法
procedure LoadFromFile(AFileName: string); //裝入數據
procedure FieldStream(ARecordNo
property FieldNames[Index: Integer]: string read GetFieldName; //字段名
property FieldDataTypes[Index: Integer]: TFieldType read GetFieldDataType;
property FieldDisplayLabels[Index: Integer]: string read GetDisplayLabel;
property Fields[RecNo
//property FieldStreams[RecNo
property RecordCount: Integer read FRecordCount write FRecordCount;
property FieldCount: Integer read FFieldCount write FFieldCount;
published
property RptTitle: string read FRptTitle write FRptTitle;
property PageHead: string read FPageHead write FPageHead;
property PageFoot: string read FPageFoot write FPageFoot;
end;
function ReadAChar(AStream: TStream): Char;
function ReadAStr(AStream: TStream): string;
function ReadBStr(AStream: TStream; Size: Integer): string;
function ReadAInteger(AStream: TStream): Integer;
procedure WriteAStr(AStream: TStream; AStr: string);
procedure WriteBStr(AStream: TStream; AStr: string);
procedure WriteAInteger(AStream: TStream; AInteger: Integer);
procedure Register;
implementation
procedure Register;
begin
RegisterComponents(
end;
function ReadAChar(AStream: TStream): Char;
Var
AChar: Char;
begin
AStream
Result := AChar;
end;
function ReadAStr(AStream: TStream): string;
var
Str: String;
C : Char;
begin
Str :=
C := ReadAChar(AStream);
While C <> #
begin
Str := Str + C;
C := ReadAChar(AStream);
end;
Result := Str;
end;
function ReadBStr(AStream: TStream; Size: Integer): string;
var
Str: String;
C : Char;
I : Integer;
begin
Str :=
For I :=
begin
C := ReadAChar(AStream);
Str := Str + C;
end;
Result := Str;
end;
function ReadAInteger(AStream: TStream): Integer;
var
Str: String;
C : Char;
begin
Result := MaxInt;
Str :=
C := ReadAChar(AStream);
While C <> #
begin
Str := Str + C;
C := ReadAChar(AStream);
end;
try
Result := StrToInt(Str);
except
Application
Mb_Ok + Mb_IconError);
end;
end;
procedure WriteAStr(AStream: TStream; AStr: string);
begin
AStream
end;
procedure WriteBStr(AStream: TStream; AStr: string);
begin
AStream
end;
procedure WriteAInteger(AStream: TStream; AInteger: Integer);
var
S : string;
begin
S := IntToStr(AInteger);
WriteAstr(AStream
end;
Constructor TIbStorage
begin
inherited Create(AOwner);
FOpenFlag := False; //確定流是否創建的標志
end;
Destructor TIbStorage
begin
if FOpenFlag then
begin
FStream
FStreamIndex
FFieldNames
end;
inherited Destroy;
end;
procedure TIbStorage
begin
FOpenFlag := True;
FStream := TMemoryStream
FStreamIndex := TStringList
FFieldNames := TStringList
Reset;
end;
procedure TIbStorage
begin
if FOpenFlag then
begin
FFieldNames
FStreamIndex
FStream
FRptTitle :=
FPageHead :=
FPageFoot :=
FFieldCount :=
FRecordCount :=
end;
end;
//
procedure TIbStorage
var
Fp: TFileStream;
I : Integer;
Ch: Char;
T
Str: string;
begin
if Not FOpenFlag then
begin
showmessage(
Exit;
end;
try
if FileExists(AFileName) then DeleteFile(AFileName);
Fp := TFileStream
Reset;
SaveHead(ADataSet
IndexFields(ADataSet); //將數據集的字段信息保存到FFieldName
LoadTableToStream(ADataSet); //保存數據集的數據信息
WriteAStr(Fp
Ch :=
Fp
WriteAStr(Fp
Ch :=
Fp
Fp
finally
Fp
end;
end;
procedure TIbStorage
Var
I : Integer;
Ch: Char;
begin
if Not ADataSet
WriteAStr(Fp
WriteAStr(Fp
WriteAStr(Fp
WriteAStr(Fp
From:http://tw.wingwit.com/Article/program/Delphi/201311/8408.html