学习 SQL 语句 - Select(2): 指定表中的字段

学习 SQL 语句 - Select(2): 指定表中的字段

//选择 country 表中的 Name 字段
SELECT Name FROM country

//选择 country 表中的 Name、Area 和 Population 字段
SELECT Name,Area,Population FROM country

{多个字段时, 字段名是用 "," 隔开的}

本例效果图:

学习 SQL 语句 - Select(2): 指定表中的字段

代码文件:
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}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT Name FROM country';
    Open;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  with ADODataSet1 do begin
    Close;
    CommandText := 'SELECT Name,Area,Population 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.
posted on 2009-05-28 01:51  万一  阅读(5110)  评论(4)  编辑  收藏
上一篇:TWebBrowser 与 MSHTML(1): 从 TWebBrowser 获取 DOM 中的 window 对象


下一篇:多线程编程(17) - 多线程同步之 WaitableTimer (等待定时器对象)[续三]