(Gast)
n/a Beiträge
|
Re: Datenbank realisieren, aber ohne Server und Treiber ?
1. Jun 2005, 14:22
hmm ich schreib dir erstmaln beispiel her wie ichs >dann< machen würde ...
Delphi-Quellcode:
uses
Classes, // TStream
Contnrs; // TObjectList
type
TDatensatz = class
private
FSpalte1: string; // oder machst aus allem nen string, je nachdem wie deine datensaetze aufgebaut werden sollen
FSpalte2: integer;
FSpalte3: double;
public
procedure LoadFromStream(Stream: TStream);
procedure SaveToStream(Stream: TStream);
property
Spalte1: string read FSpalte1 write FSpalte1; // oder string read FSpalte1 write SetSpalte1; - dann müsstest dir noch private procedure SetSpalte1(const Value: string); reinbasteln
// SpalteX...
end;
TDatensaetze = class(TObjectList)
private
function GetItems(Index: integer): TDatensatz;
procedure SetItems(Index: integer; const Value: TDatensatz);
public
procedure LoadFromFile(const FileName: string);
procedure LoadFromStream(Stream: TStream);
procedure SaveToFile(const FileName: string);
procedure SaveToStream(Stream: TStream);
property Items[Index: integer]: TDatensatz read GetItems write SetItems; default;
end;
implementation
procedure WriteStringToStream(Stream: TStream; const sText: string);
begin
Stream.Write(Length(sText), 4); // 4 = SizeOf(Integer);
Stream.Write(Pointer(sText)^, Length(sText));
// oder Stream.Write(sText[1], Length(sText));
end;
procedure ReadStringFromStream(Stream: TStream; var sText: string);
var
iLength: integer;
begin
Stream.Read(iLength, 4);
SetLength(sText, iLength);
Stream.Read(Pointer(sText)^, iLength);
// oder wieder Stream.Read(sText[1], iLength);
end;
{ TDatensatz }
procedure TDatensatz.LoadFromStream(Stream: TStream);
begin
WriteStringToStream(Stream, FSpalte1);
Stream.Write(FSpalte2, 4); // 4 = SizeOf(Integer)
Stream.Write(FSpalte3, 8); // 8 = SizeOf(Double)
end;
procedure TDatensatz.SaveToStream(Stream: TStream);
begin
ReadStringFromStream(Stream, FSpalte1);
Stream.Read(FSpalte2, 4);
Stream.Read(FSpalte3, 8);
end;
{ TDatensaetze }
function TDatensaetze.GetItems(Index: integer): TDatensatz;
begin
if (Index > -1) and (Index < Count) then
Result := TDatensatz(inherited Items[Index])
else
Result := nil;
end;
procedure TDatensaetze.LoadFromFile(const FileName: string);
var
xFS: TFileStream;
begin
if not FileExists(FileName) then
Exit;
xFS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
try
LoadFromStream(xFS);
finally
FreeAndNil(xFS);
end;
end;
procedure TDatensaetze.LoadFromStream(Stream: TStream);
var
iCount: integer;
iIndex: integer;
xDatensatz: TDatensatz;
begin
Stream.Read(iCount, 4);
for iIndex := 0 to iCount - 1 do begin
xDatensatz := TDatensatz.Create;
xDatensatz.LoadFromStream(Stream);
Add(xDatensatz);
end;
end;
procedure TDatensaetze.SaveToFile(const FileName: string);
var
xFS: TFileStream;
begin
xFS := TFileStream.Create(FileName, fmCreate);
try
SaveToStream(xFS);
finally
FreeAndNil(xFS);
end;
end;
procedure TDatensaetze.SaveToStream(Stream: TStream);
var
iIndex: integer;
begin
Stream.Write(Count, 4); // Count = Self.Count
for iIndex := 0 to Count - 1 do
Items[iIndex].SaveToStream(Stream);
end;
procedure TDatensaetze.SetItems(Index: integer; const Value: TDatensatz);
begin
if (Index > -1) and (Index < Count) then
Items[Index] := Value;
end;
p00h - ich hoffs iss soweit verständlich
|
|
Zitat
|