PgDAC

Retrieving Data

  1. Requirements
  2. General Information
  3. TPgQuery
  4. TPgTable
  5. Additional information

Requirements

This walkthrough supposes that you know how to connect to server (tutorial "Connecting To SQL Server"), how to create necessary objects on the server (tutorial "Creating Database Objects"), and how to insert data to created tables (tutorial "Inserting Data Into Tables").

General information

As we know, an original function of any database application is establishing connection to a data source and working with data contained in it. PgDAC provides several components that can be used for data retrieving, such as TPgQuery and TPgTable. We will discuss data retrieving using these components.

The goal of this tutorial is to retrieve data from a table dept.

TPgQuery

The following code demonstrates retrieving of data from the dept table using the TPgQuery component:

[Delphi]

var
  q: TPgQuery;
begin
  q := TPgQuery.Create(nil);
  try
    // con is TPgConnection already set up
    q.Connection := con; 

    // retrieve data
    q.SQL.Text := 'SELECT * FROM dept';
    q.Open;

    // shows the number of records obtained from the server
    ShowMessage(IntToStr(q.RecordCount));
  finally
    q.Free;
  end;
end;

[C++Builder]

{
  TPgQuery* q = new TPgQuery(NULL);
  try
  {
    // con is TPgConnection already set up
    q->Connection = con; 

    // retrieve data
    q->SQL->Text = "SELECT * FROM dept";
    q->Open();

    // shows the number of records obtained from the server
    ShowMessage(IntToStr(q->RecordCount)); 
  }
  __finally
  {
    q->Free();
  }
}

TPgTable

The following code demonstrates retrieving of data from the dept table using the TPgTable component:

[Delphi]

var
  tbl: TPgTable;
begin
  tbl := TPgTable.Create(nil);
  try
    // con is TPgConnection already set up
    tbl.Connection := con; 

    // retrieve data
    tbl.TableName := 'dept';
    tbl.Open;

    // shows the number of records obtained from the server
    ShowMessage(IntToStr(tbl.RecordCount)); 
  finally
    tbl.Free;
  end;
end;

[C++Builder]

{
  TPgTable* tbl = new TPgTable(NULL);
  try
  {
    // con is TPgConnection already set up
    tbl->Connection = con; 
	
    // retrieve data
    tbl->TableName = "dept";
    tbl->Open();

    // shows the number of records obtained from the server
    ShowMessage(IntToStr(tbl->RecordCount));
  }
  __finally
  {
    tbl->Free();
  }
}

Additional Information

It is also possible to use stored procedures for data retrieving. In this case, all data manipulation logic is defined on the server.

© 1997-2020 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback