在delphi.net中,VCL.net有兩點蠻遺憾的:
1.不能使用ADO(dbGo),不過據李維說以後將會有這個組件。
2.不能使用ADO.net和BDP,這將是我這片文章的主題。
在Borland的delphi交流區內,曾經看到Danny說過,"在delphi.net中VCL.net可以呼叫Winform元件,同樣Winform也可以呼叫VCL.net元件"。
為了驗證第一句話,我試了下,在Vcl.net中是可以使用.Net的元件的,如可以直接uses System.Data.SqlClient,並直接使用SqlConnection類別。也就是說,雖然VCL.net的組件面板中無法看到.net組件,但是所有的.net組件的類,VCl.net都可以使用! 但是,Ado.net的dataset並沒有和VCl.net的Dataset組件相容,所以無法直接呼叫資料感知組件。不過,看了李維的Inside Vcl知道原來有ADONETConnector元件,用了這個元件,可以讓Ado.net支援使用資料感知元件了。
首先,VCL.net元件的dll在BDS/2.0/Bin 下有一個Borland.Vcl.Design.AdoNet.dll,點擊Install .net component選單,然後在窗體的.net vcl components頁中把這個dll Add一下,就可以看見ADONETConnector元件。然後加一個Dbgrid,db....,datasoure....,只要datasource.dataset:=ADONETConnector1。其它的和原來的delphi一樣,就可以了。同樣改方法對BDP也有效。
我的具體程式碼如下,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,
System.Data.SqlClient,
System.Data, System.ComponentModel, Borland.Vcl.StdCtrls,
Borland.Vcl.ExtCtrls, Borland.Vcl.DBCtrls, Borland.Vcl.Grids,
Borland.Vcl.DBGrids, Borland.Vcl.Db, Borland.Vcl.ADONETDb;
type
TForm1 = class(TForm)
Button1: TButton;
ADONETConnector1: TADONETConnector;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
PRocedure Button1Click(Sender: TObject);
private
{ Private declarations }
Connection:SqlConnection;
ProDataSet : DataSet;
Adapter : SqlDataAdapter;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.nfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Connection := SqlConnection.Create('...');
Connection.Open;
ProDataSet := DataSet.Create;
Adapter := SqlDataAdapter.Create('select * from Product', Connection);
Adapter.Fill(ProDataSet, 'Product');
ADONETConnector1.DataTable:=ProDataSet.Tables[0];
end;
end.