delphi DataModule1 使用方法

数据显示页面:

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls, DBCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

 

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
       // DataModule1.Destroy;
         DataModule1.ADOQuery1.Connection:=DataModule1.ADOConnection1;
         DataModule1.ADOQuery1.SQL.Text:='select * from ts_cbsb';
         DataModule1.ADOQuery1.Active:=true;
         DataModule1.DataSource1.DataSet:=DataModule1.ADOQuery1;
         dbgrid1.DataSource:= DataModule1.DataSource1;
end;

end.

 

 

 

Unit2页面

 

unit Unit2;

interface

uses
  SysUtils, Classes, DB, ADODB;

type
  TDataModule1 = class(TDataModule)
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DataModule1: TDataModule1;

implementation

{$R *.dfm}

end.

 

 

工程文件 :

 

 

program Project1;


uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {DataModule1: TDataModule};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TDataModule1, DataModule1);

  Application.Run;
end.

你可能感兴趣的:(DELPHI技术)