Page 1 of 1

return dataset

Posted: 04.08.2011, 07:39
by notebook
How I can return type dataset with data? here is example, but don't work.
I created unit2 with zeoslib classes, manual, and in unit1, I called unit2.

Code: Select all

unit Unit2; 


interface

uses
  Classes, SysUtils,sqldb,db,Types,Dialogs,ZConnection,
  ZDataset, ZSqlProcessor, ZStoredProcedure;
type
  connectiom=class(TObject)
    public
   function q(query1: string): TDataSet;
   procedure z;

   private


  end;
  var
      c : TZConnection;
     qu : TZQuery;
     ds : TDataSource;




implementation

 function connectiom.q(query1 : string):TdataSet;
begin
  c := TZConnection.Create(nil);
  qu:=TZQuery.Create(nil);
  ds:=TDataSource.Create(nil);

  c.Catalog:='postgres';
  c.Connected:=false;
  c.Database:='postgres';
  c.DesignConnection:=false;
  c.HostName:='127.0.0.1';
  c.LoginPrompt:=false;
  c.Password:='postgres';
  c.port:=5432;
  c.Protocol:='postgresql-8';
  c.ReadOnly:=false;
  c.User:='postgres';

  qu.Connection:=c;
  qu.SQL.Text:=query1;
  ds.DataSet:=qu;

end;

 procedure connectiom.z;
 begin

   c.Connect;
   qu.Active:=true;
 end;


 end.



Code: Select all

unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DBGrids, ZConnection, ZDataset, unit2, db;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    DBGrid1: TDBGrid;
    procedure Button1Click(Sender: TObject);
  private
   p : connectiom;
   ds : TDataSource;

  public
    { public declarations }
  end; 

var
  Form1: TForm1; 

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
   p:=connectiom.Create;
   ds:=TDataSource.Create(nil);


  ds.DataSet:= p.q('select * from my_table');


  DBGrid1.DataSource:= ds ;
   p.z;


end;

end.


Posted: 05.08.2011, 12:28
by guidoaerts
in unit1 you are assigning the result of p.q to ds.Dataset, and in p.q you assign qu to ds.dataset, but then q returns without result assigned.
maybe it works better if you replace in unit2

ds.DataSet:=qu;

with :

result := qu;

guido