今天突發奇想,如果我自己做一個DbxDataSet呢?
製作上很簡單,底下來幾張簡圖:
一、新增VCL專案
二、新增Frame
Delphi XE的物件寶庫,選擇Frame |
記得要把 DataSetProvider.Option的poAllowCommandText 設 True |
為了讓它更像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
如果有什麼想法和意見,請盡情地反饋一下吧!