Delphi(程序人生)

Delphi Programer DoubleCat

导航

如何手工创建连接关Bind到控件显示?

uses System.Data.OleDb 

这是用sqlDataAdapter方法

-----------------------------------

unit Default;

interface

uses
  System.Collections, System.ComponentModel,System.Data.SqlClient,System.Data.OleDb,
  System.Data, System.Drawing, System.Web, System.Web.SessionState,
  System.Web.UI, System.Web.UI.WebControls, System.Web.UI.HtmlControls,
  System.Web.Security, System.Web.UI.WebControls.WebParts, System.Configuration;

type
  TDefault = class(System.Web.UI.Page)
  {$REGION 'Designer Managed Code'}
  strict private
    procedure InitializeComponent;
    procedure Button1_Click(sender: System.Object; e: System.EventArgs);
    procedure Button2_Click(sender: System.Object; e: System.EventArgs);
  {$ENDREGION}
  strict private
    procedure Page_Load(sender: System.Object; e: System.EventArgs);
  strict protected
    Button1: System.Web.UI.WebControls.Button;
    GridView1: System.Web.UI.WebControls.GridView;
    Button2: System.Web.UI.WebControls.Button;
  protected
    procedure OnInit(e: EventArgs); override;
  private
    { Private Declarations }
  public
    { Public Declarations }
  end;

implementation

{$REGION 'Designer Managed Code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TDefault.InitializeComponent;
begin
  Include(Self.Button1.Click, Self.Button1_Click);
  Include(Self.Button2.Click, Self.Button2_Click);
  Include(Self.Load, Self.Page_Load);
end;
{$ENDREGION}

procedure TDefault.Page_Load(sender: System.Object; e: System.EventArgs);
begin
  // TODO: Put user code to initialize the page here
end;

procedure TDefault.OnInit(e: EventArgs);
begin
  //
  // Required for Designer support
  //
  InitializeComponent;
  inherited OnInit(e);
end;


var s:string;
    CNN:SqlConnection;
    DataAdaper:SqlDataAdapter;
    DS:DataSet;

procedure TDefault.Button1_Click(sender: System.Object; e: System.EventArgs);

begin
    s:='Data Source=LocalHost"MSSQL;Initial Catalog=pubs;User ID=sa;Password=admin'; //连接字串
    CNN:=SqlConnection.Create(s); //初使化连接
    DataAdaper:=SqlDataAdapter.Create('select * from employee',CNN);//相当于delphi中的sqlClientDataSet,对它进行初使化
                                                                    //在创退时自动打开数据表
    DS:=DataSet.Create; //相当于Delphi中的ClientDataSet
    DataAdaper.Fill(DS,'HaHa'); //把数据灌进来,并为数据表取个名字,因为它的DataSet可以放很多表
    GridView1.DataSource:=DS.Tables['HaHa'].DefaultView; //把ClientDataSet中的数据视图,挂到控件的dataSource中
    GridView1.DataBind();//进行Bind
 end;

end.

-----------------------

DoubleCat 

 

posted on 2008-12-16 00:40  DoubleCat  阅读(192)  评论(0编辑  收藏  举报