本文来自:万一
//选择 country 表中的所有字段 SELECT * FROM country {虽然 SQL 并不区分大小写, 但应习惯把关键字大写}
本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB; type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; Button2: TButton; DBGrid1: TDBGrid; DataSource1: TDataSource; ADODataSet1: TADODataSet; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {查看数据库 dbdemos.mdb 中的 country 表} procedure TForm1.Button1Click(Sender: TObject); begin ADODataSet1.Close; ADODataSet1.CommandText := 'SELECT * FROM country'; ADODataSet1.Open; end; {查看数据库 dbdemos.mdb 中的 customer 表} procedure TForm1.Button2Click(Sender: TObject); begin with ADODataSet1 do begin Close; CommandText := 'SELECT * FROM customer'; Open; end; end; {控件关联、连接数据库: dbdemos.mdb} procedure TForm1.FormCreate(Sender: TObject); var mdbFile: string; begin {获取 CodeGear 共享安装的数据文件: dbdemos.mdb 的路径} mdbFile := GetEnvironmentVariable('COMMONPROGRAMFILES'); {C:\Program Files\Common Files} 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.