学习 SQL 语句 - Select(4): 排序


Order By Asc|Desc

本例效果图:

学习 SQL 语句 - Select(4): 排序

代码文件:

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;

    Button3: TButton;

    Button4: TButton;

    Button5: TButton;

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);

    procedure Button4Click(Sender: TObject);

    procedure Button5Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



//根据 Name 字段倒排序

procedure TForm1.Button1Click(Sender: TObject);

begin

  with ADODataSet1 do begin

    Close;

    CommandText := 'SELECT * FROM country ORDER BY Name DESC';

    Open;

  end;

end;



//根据 Name 字段正排序

procedure TForm1.Button2Click(Sender: TObject);

begin

  with ADODataSet1 do begin

    Close;

    CommandText := 'SELECT * FROM country ORDER BY Name ASC';

    Open;

  end;

end;



//根据 Area 字段正排序; 数字字段正排序是: 小-大、反排序是: 大-小

procedure TForm1.Button3Click(Sender: TObject);

begin

  with ADODataSet1 do begin

    Close;

    CommandText := 'SELECT * FROM country ORDER BY Area ASC';

    Open;

  end;

end;



//多个排序条件用 "," 隔开, 排序时先依前者

procedure TForm1.Button4Click(Sender: TObject);

begin

  with ADODataSet1 do begin

    Close;

    CommandText := 'SELECT * FROM country ORDER BY Continent ASC, Capital ASC';

    Open;

  end;

end;



//如果 Where 没有省略, Order By 应该在 Where 后面

procedure TForm1.Button5Click(Sender: TObject);

begin

  with ADODataSet1 do begin

    Close;

    CommandText := 'SELECT * FROM country WHERE Area>200000 ORDER BY Continent ASC';

    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.


 
   

窗体文件:

object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 407

  ClientWidth = 626

  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 = 33

    Width = 626

    Height = 374

    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 = 626

    Height = 33

    Align = alTop

    Caption = 'Panel1'

    TabOrder = 1

    object Button1: TButton

      Left = 6

      Top = 5

      Width = 75

      Height = 25

      Caption = 'Button1'

      TabOrder = 0

      OnClick = Button1Click

    end

    object Button2: TButton

      Left = 87

      Top = 5

      Width = 75

      Height = 25

      Caption = 'Button2'

      TabOrder = 1

      OnClick = Button2Click

    end

    object Button3: TButton

      Left = 168

      Top = 5

      Width = 75

      Height = 25

      Caption = 'Button3'

      TabOrder = 2

      OnClick = Button3Click

    end

    object Button4: TButton

      Left = 249

      Top = 5

      Width = 75

      Height = 25

      Caption = 'Button4'

      TabOrder = 3

      OnClick = Button4Click

    end

    object Button5: TButton

      Left = 330

      Top = 5

      Width = 75

      Height = 25

      Caption = 'Button5'

      TabOrder = 4

      OnClick = Button5Click

    end

  end

  object DataSource1: TDataSource

    DataSet = ADODataSet1

    Left = 184

    Top = 112

  end

  object ADODataSet1: TADODataSet

    CursorType = ctStatic

    Parameters = <>

    Left = 232

    Top = 184

  end

end


 
   

你可能感兴趣的:(select)