Jumat, 27 April 2012

Database Coding TQuery dan TTable BDE di Delphi


Teknologi BDE dengan komponen TQuery maupun TTable dapat digunakan untuk membuat program database menggunakan Paradox, FoxPro, Dbase maupun Database yang mendukung ODBC antara lain : Microsoft Access, SQL Server, Interbase/Firebird, MySQL, PostgreSQL dan Oracle. Untuk membuat program database dengan Paradox menggunakan BDE dengan kode program ikuti langkah-langkah berikut :

1. Buka Code Gear Delphi. Klik menu File – New VCL Forms Application – Delphi for Win32.

2. Akan ditampilkan Form1 yang masih kosong. Tambahkan komponen TLabel, TEdit dan TButton dari Tab Standard, TQuery dari Tab BDE, TDBGrid dari Tab Data Controls, TDataSource dari Tab Data Access.

3. Klik dua kali pada Form1 kemudian pada Private Sub Form1_Load tambahkan kode program berikut ::

procedure TFParadoxBDE.FormCreate(Sender: TObject);
begin
  qContacts.Close;
  qContacts.DatabaseName := ExtractFilePath(Application.ExeName);
  qContacts.RequestLive := True;
  qContacts.SQL.Text := 'SELECT Name, Address, Phone FROM Contacts ORDER BY Name';
  qContacts.Open;

  ds.DataSet := qContacts;

  dg.DataSource := ds;
  dg.Columns[0].Width := 100;
  dg.Columns[1].Width := 100;
  dg.Columns[2].Width := 100;
end;

4. Untuk event-event yang lain, program selengkapnya sebagai berikut :

unit uContacts;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;

type
  TFParadoxBDE = class(TForm)
    qContacts: TQuery;
    ds: TDataSource;
    dg: TDBGrid;
    Label1: TLabel;
    eName: TEdit;
    Label2: TLabel;
    eAddress: TEdit;
    Label3: TLabel;
    ePhone: TEdit;
    bFirst: TButton;
    bPrev: TButton;
    bNext: TButton;
    bLast: TButton;
    bAdd: TButton;
    bEdit: TButton;
    bSave: TButton;
    bCancel: TButton;
    bRefresh: TButton;
    bDelete: TButton;
    eFind: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure qContactsAfterScroll(DataSet: TDataSet);
    procedure bFirstClick(Sender: TObject);
    procedure bPrevClick(Sender: TObject);
    procedure bNextClick(Sender: TObject);
    procedure bLastClick(Sender: TObject);
    procedure bAddClick(Sender: TObject);
    procedure bEditClick(Sender: TObject);
    procedure bCancelClick(Sender: TObject);
    procedure bSaveClick(Sender: TObject);
    procedure bRefreshClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FParadoxBDE: TFParadoxBDE;

implementation

{$R *.dfm}

procedure TFParadoxBDE.bAddClick(Sender: TObject);
begin
  qContacts.Insert;
end;

procedure TFParadoxBDE.bCancelClick(Sender: TObject);
begin
  qContacts.Cancel;
end;

procedure TFParadoxBDE.bEditClick(Sender: TObject);
begin
  qContacts.Edit;
end;

procedure TFParadoxBDE.bFirstClick(Sender: TObject);
begin
  qContacts.First;
end;

procedure TFParadoxBDE.bLastClick(Sender: TObject);
begin
  qContacts.Last;
end;

procedure TFParadoxBDE.bNextClick(Sender: TObject);
begin
  qContacts.Next;
end;

procedure TFParadoxBDE.bPrevClick(Sender: TObject);
begin
  qContacts.Prior;
end;

procedure TFParadoxBDE.bRefreshClick(Sender: TObject);
begin
  qContacts.Close;
  qContacts.SQL.Text := 'SELECT Name, Address, Phone FROM Contacts WHERE Name LIKE ''%' + eFind.Text + '%''';
  qContacts.Open;

  dg.Columns[0].Width := 100;
  dg.Columns[1].Width := 100;
  dg.Columns[2].Width := 100;
end;

procedure TFParadoxBDE.bSaveClick(Sender: TObject);
begin
  qContacts.FieldByName('Name').AsString := eName.Text;
  qContacts.FieldByName('Address').AsString := eAddress.Text;
  qContacts.FieldByName('Phone').AsString := ePhone.Text;
  qContacts.Post;
end;

procedure TFParadoxBDE.FormCreate(Sender: TObject);
begin
  qContacts.Close;
  qContacts.DatabaseName := ExtractFilePath(Application.ExeName);
  qContacts.RequestLive := True;
  qContacts.SQL.Text := 'SELECT Name, Address, Phone FROM Contacts ORDER BY Name';
  qContacts.Open;

  ds.DataSet := qContacts;

Tidak ada komentar: