本文来自:万一
本例效果图:
代码文件:
本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB; type TForm1 = class(TForm) DBGrid1: TDBGrid; DataSource1: TDataSource; ADODataSet1: TADODataSet; Panel1: TPanel; Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //原名 AS 新名 procedure TForm1.Button1Click(Sender: TObject); begin with ADODataSet1 do begin Close; CommandText := 'SELECT Name AS 名称 FROM country'; Open; end; end; //多个字段用 "," 隔开 procedure TForm1.Button2Click(Sender: TObject); begin with ADODataSet1 do begin Close; CommandText := 'SELECT Name AS 名称, Capital AS 首都,' + 'Continent AS 州, Area AS 面积, Population AS 人口 ' + 'FROM country'; Open; end; end; procedure TForm1.FormCreate(Sender: TObject); var mdbFile: string; begin mdbFile := GetEnvironmentVariable('COMMONPROGRAMFILES'); mdbFile := mdbFile + '\CodeGear Shared\Data\dbdemos.mdb'; ADODataSet1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + mdbFile + ';Persist Security Info=False'; DBGrid1.DataSource := DataSource1; DataSource1.DataSet := ADODataSet1; end; end.