MyDAC

Retrieving Data

This tutorial describes how to retrieve data from tables using the TMyQuery and TMyTable components.

  1. Requirements
  2. General Information
  3. TMyQuery
  4. TMyTable
  5. Additional information

Requirements

This walkthrough supposes that you know how to connect to server (tutorials "Connecting To MySQL" and "Connecting To MySQL Embedded 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. MyDAC provides several components that can be used for data retrieving, such as TMyQuery and TMyTable. We will discuss data retrieving using these components.

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

TMyQuery

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

[Delphi]

var
  q: TMyQuery;
begin
  q := TMyQuery.Create(nil);
  try
    // con is either TMyConnection or TMyEmbConnection 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]

{
  TMyQuery* q = new TMyQuery(NULL);
  try
  {
    // con is either TMyConnection or TMyEmbConnection 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();
  }
}

TMyTable

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

[Delphi]

var
  tbl: TMyTable;
begin
  tbl := TMyTable.Create(nil);
  try
    // con is either TMyConnection or TMyEmbConnection 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]

{
  TMyTable* tbl = new TMyTable(NULL);
  try
  {
    // con is either TMyConnection or TMyEmbConnection 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 server. You can find more about using stored procedures in the tutorial "Stored Procedures".

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