随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


//选择 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.


窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 407
  ClientWidth = 645
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGrid1: TDBGrid
    Left = 0
    Top = 35
    Width = 645
    Height = 372
    Align = alClient
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 645
    Height = 35
    Align = alTop
    Caption = 'Panel1'
    TabOrder = 1
    object Button1: TButton
      Left = 16
      Top = 4
      Width = 75
      Height = 25
      Caption = 'Button1'
      TabOrder = 0
      OnClick = Button1Click
    end
    object Button2: TButton
      Left = 97
      Top = 4
      Width = 75
      Height = 25
      Caption = 'Button2'
      TabOrder = 1
      OnClick = Button2Click
    end
  end
  object DataSource1: TDataSource
    DataSet = ADODataSet1
    Left = 184
    Top = 112
  end
  object ADODataSet1: TADODataSet
    CursorType = ctStatic
    Parameters = <>
    Left = 232
    Top = 184
  end
end

posted on 2009-05-28 01:40  万一  阅读(...)  评论(...编辑  收藏