|
导读在delphi.net中,VCL.net有两点蛮遗憾的:1.不能使用ADO(dbGo),不过据李维说本月的delphi8.1将会有这个组件。2.不能使用ADO.net和BDP,这将是我这片文章的主题... 在delphi.net中,VCL.net有两点蛮遗憾的:1.不能使用ADO(dbGo),不过据李维说本月的delphi8.1将会有这个组件。 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一样,就可以了。我的具体代码如下, 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( 'data source= 192.168.76.170;'+ 'initial catalog=SfiecErp;'+ 'password=qwert;'+ 'persist security info=True;'+ 'user id=sa;'+ 'packet size=4096;'+ 'Connection Lifetime=0;'+ 'Connection Reset=False;'+ 'Pooling=False;'+ 'Max Pool Size=100;Min Pool Size=0'); Connection.Open; ProDataSet := DataSet.Create; Adapter := SqlDataAdapter.Create('select * from TProduct', Connection); Adapter.Fill(ProDataSet, 'Product'); ADONETConnector1.DataTable:=ProDataSet.Tables[0]; end; end. |
温馨提示:喜欢本站的话,请收藏一下本站!