[fpc-devel] TBufDataSet Stream Error

Joost van der Sluis joost at cnoc.nl
Mon Aug 17 18:19:54 CEST 2009


Op woensdag 22-07-2009 om 00:54 uur [tijdzone +0300], schreef Dimitrios
Chr. Ioannidis:
> Hi,
>  
>   i'm trying to use TBufDataset as a very simple mem dataset but i get a
> "Stream read error". 
> 
>   I can reproduce it, but first is it legitimate to use TBufDataset like
> this ?

Yes, it should work. I tried the following program (just re-arranged
your code somewhat) and that worked. (It created the database-file)
Maybe you should try fpc 2.3.1/2.5.1. (the development-versions)

program Project1;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, db, BufDataset;

const sDBFileName = 'database.db';

var FTest : TBufDataset;

procedure CreateDB;
begin
  FTest.FieldDefs.Add('ID', ftInteger, 0, False);
  FTest.FieldDefs.Add('FirstName', ftString, 20, False);
  FTest.FieldDefs.Add('LastName', ftString, 20, False);
  FTest.CreateDataset;
end;

begin
  FTest := TBufDataset.Create(nil);
  if not FileExists(sDBFileName) then
    CreateDB
  else
    FTest.LoadFromFile(sDBFileName);
//  DataSource1.DataSet := FTest;
  FTest.Open;
  //write
  FTest.Append;
  FTest.Fields[1].AsString:='jojojojojo';
  // or read
  //writeln(FTest.fields[1].asstring);
  if FTest.State in [dsEdit, dsInsert] then
    FTest.Post;
  FTest.SaveToFile(sDBFileName);
end.


> unit Unit2; 
> 
> {$mode objfpc}{$H+}
> 
> interface
> 
> uses
>   Classes, SysUtils, db, FileUtil, LResources, Forms, Controls, Dialogs,
> bufdataset;
> 
> type
> 
>   { TDataModule1 }
> 
>   TDataModule1 = class(TDataModule)
>     Datasource1: TDatasource;
>     procedure DataModuleCreate(Sender: TObject);
>     procedure DataModuleDestroy(Sender: TObject);
>   private
>     { private declarations }
>     FTest: TBufDataset;
>     procedure CreateDB;
>   public
>     { public declarations }
>   end; 
> 
> var
>   DataModule1: TDataModule1; 
> 
> implementation
> 
> { TDataModule1 }
> 
> var
>   sDBFileName: string;
> 
> procedure TDataModule1.DataModuleCreate(Sender: TObject);
> begin
>   FTest := TBufDataset.Create(Self);
>   if not FileExists(sDBFileName) then
>     CreateDB
>   else
>     FTest.LoadFromFile(sDBFileName);
>   DataSource1.DataSet := FTest;
>   FTest.Open;
> end;
> 
> procedure TDataModule1.DataModuleDestroy(Sender: TObject);
> begin
>   if FTest.State in [dsEdit, dsInsert] then
>     FTest.Post;
>   FTest.SaveToFile(sDBFileName);
> end;
> 
> procedure TDataModule1.CreateDB;
> begin
>   FTest.FieldDefs.Add('ID', ftInteger, 0, False);
>   FTest.FieldDefs.Add('FirstName', ftString, 20, False);
>   FTest.FieldDefs.Add('LastName', ftString, 20, False);
>   FTest.CreateDataset;
> end;
> 
> initialization
>   {$I unit2.lrs}
>   sDBFileName := ExtractFilePath(Application.ExeName) + 'TestDB.db';
> 
> end.                                                   
> 
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
> 
-- 




More information about the fpc-devel mailing list