当前位置:主页>Delphi教程>文章内容
在delphi.net的VCL.net里使用Ado.net
来源: 作者: 发布时间:2007-04-29  

在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.
   


 
上一篇:使用Delphi创建IIS虚拟目录   下一篇:通用查询组件设计(续二)
 
  相关文章
·使用Delphi创建IIS虚拟目录
·通用查询组件设计(续二)
·在WIN2000与WIN98中创建共享文件夹的方
·GSM规范中的部分编码转换
·Delphi实现对注册表的监视和扫描
·取得图片的透明区域
·DELPHI8操作符重载的例子
·使用IntraWeb进行Web编程
·今天做了一个小软件,总结了一些经验与
·搜索字符串在流中的位置
·区分保留字跟关键字
·流的压缩和解压
 
【关闭窗口】
推荐本站资源
最新文章