Translate

2013/04/17

自製懶人版的DbxDAC

因為SQLClientDataSet已經作廢,取而代之的SimpleDataSet 又搞不清那神奇的「InternalConnection」和詭異的「InternalDataSet」……

今天突發奇想,如果我自己做一個DbxDataSet呢?
製作上很簡單,底下來幾張簡圖:
一、新增VCL專案
二、新增Frame
Delphi XE的物件寶庫,選擇Frame
三、把Dbexpress(ADO亦可)相依的元件放進來並作好綁定工作
記得要把 DataSetProvider.Option的poAllowCommandText 設 True
DataSetProvider.Option的poAllowCommandText 設 True,這樣就可以直接對ClientDataSet下SQL指令。

為了讓它更像DataSet,所以我寫了以下的Code:
unit DbxDACUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, DBClient, Provider, DB, SqlExpr;

type
  TDbxDataSet = class(TFrame)
    qy: TSQLQuery;
    dsp: TDataSetProvider;
    cds: TClientDataSet;
    procedure cdsAfterPost(DataSet: TDataSet);
    procedure cdsAfterDelete(DataSet: TDataSet);
    procedure cdsAfterCancel(DataSet: TDataSet);
  private
    function GetSQLConnection: TSQLConnection;
    procedure SetSQLConnection(const Value: TSQLConnection);
    procedure ApplyUpdates(DataSet: TDataSet);
    { Private declarations }
  public
    { Public declarations }
    property SQLConnection: TSQLConnection read GetSQLConnection write SetSQLConnection;
  end;

implementation

{$R *.dfm}

{ TDbxDataSet }

procedure TDbxDataSet.ApplyUpdates(DataSet: TDataSet);
begin
  cds.ApplyUpdates(0);
end;

procedure TDbxDataSet.cdsAfterCancel(DataSet: TDataSet);
begin
  cds.CancelUpdates();
end;

procedure TDbxDataSet.cdsAfterDelete(DataSet: TDataSet);
begin
  ApplyUpdates(DataSet);
end;

procedure TDbxDataSet.cdsAfterPost(DataSet: TDataSet);
begin
  ApplyUpdates(DataSet);
end;

function TDbxDataSet.GetSQLConnection: TSQLConnection;
begin
  Result := qy.SQLConnection;
end;

procedure TDbxDataSet.SetSQLConnection(const Value: TSQLConnection);
begin
  qy.SQLConnection := Value;
end;

end.

如此一來,在實作上就可以簡單用以下代碼:
procedure TForm1.Button1Click(Sender: TObject);
begin
  SQLConnection1.Open();
  DbxDataSet1.SQLConnection := SQLConnection1;
  DbxDataSet1.cds.CommandText := 'SELECT * FROM CDS ';
  DbxDataSet1.cds.Open();
end;

以上就是突發奇想的DbxDAC
如果有什麼想法和意見,請盡情地反饋一下吧!












其實,有個叫 TSQLClientDataSet 的元件,完全符合這個概念。

沒有留言:

張貼留言

Delphi in Web Development: WebBroker + EasyUI (01)

I have been received many demand about web development for long time. But, I don't wont to use ASP.NET develop the web project. M...