Order By Asc|Desc
本例效果图:
代码文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB;typeTForm1 = 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;varForm1: TForm1;implementation{$R *.dfm}//根据 Name 字段倒排序 procedure TForm1.Button1Click(Sender: TObject); beginwith ADODataSet1 do beginClose;CommandText := 'SELECT * FROM country ORDER BY Name DESC';Open;end; end;//根据 Name 字段正排序 procedure TForm1.Button2Click(Sender: TObject); beginwith ADODataSet1 do beginClose;CommandText := 'SELECT * FROM country ORDER BY Name ASC';Open;end; end;//根据 Area 字段正排序; 数字字段正排序是: 小-大、反排序是: 大-小 procedure TForm1.Button3Click(Sender: TObject); beginwith ADODataSet1 do beginClose;CommandText := 'SELECT * FROM country ORDER BY Area ASC';Open;end; end;//多个排序条件用 "," 隔开, 排序时先依前者 procedure TForm1.Button4Click(Sender: TObject); beginwith ADODataSet1 do beginClose;CommandText := 'SELECT * FROM country ORDER BY Continent ASC, Capital ASC';Open;end; end;//如果 Where 没有省略, Order By 应该在 Where 后面 procedure TForm1.Button5Click(Sender: TObject); beginwith ADODataSet1 do beginClose;CommandText := 'SELECT * FROM country WHERE Area>200000 ORDER BY Continent ASC';Open;end; end;procedure TForm1.FormCreate(Sender: TObject); varmdbFile: string; beginmdbFile := 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: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 407ClientWidth = 626Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalseOnCreate = FormCreatePixelsPerInch = 96TextHeight = 13object DBGrid1: TDBGridLeft = 0Top = 33Width = 626Height = 374Align = alClientDataSource = DataSource1TabOrder = 0TitleFont.Charset = DEFAULT_CHARSETTitleFont.Color = clWindowTextTitleFont.Height = -11TitleFont.Name = 'Tahoma'TitleFont.Style = []endobject Panel1: TPanelLeft = 0Top = 0Width = 626Height = 33Align = alTopCaption = 'Panel1'TabOrder = 1object Button1: TButtonLeft = 6Top = 5Width = 75Height = 25Caption = 'Button1'TabOrder = 0OnClick = Button1Clickendobject Button2: TButtonLeft = 87Top = 5Width = 75Height = 25Caption = 'Button2'TabOrder = 1OnClick = Button2Clickendobject Button3: TButtonLeft = 168Top = 5Width = 75Height = 25Caption = 'Button3'TabOrder = 2OnClick = Button3Clickendobject Button4: TButtonLeft = 249Top = 5Width = 75Height = 25Caption = 'Button4'TabOrder = 3OnClick = Button4Clickendobject Button5: TButtonLeft = 330Top = 5Width = 75Height = 25Caption = 'Button5'TabOrder = 4OnClick = Button5Clickendendobject DataSource1: TDataSourceDataSet = ADODataSet1Left = 184Top = 112endobject ADODataSet1: TADODataSetCursorType = ctStaticParameters = <>Left = 232Top = 184end end