delphi排序降序

时间:2012-01-07 17:44:09

标签: delphi sorting

我正在尝试对记录进行排序(按降序排列)并在网格中显示数据,该数据通过数据源连接到数据集Vw_EmpVacations 这是代码,请告诉我我做错了什么 顺便说一下,当我在数据库中执行视图时,视图按照start_date降序排序

Vw_EmpVacations.Active:=false;
Vw_EmpVacations.SQL.Text:='select * from Vw_EmpVacations where Branch_ID=:x and emp_id=:y and vac_id=:z order by Start_Date  Desc ';
Vw_EmpVacations.Parameters[0].Value:=branch_ID;
Vw_EmpVacations.Parameters[1].Value:=emp_Id;
Vw_EmpVacations.Parameters[2].Value:=Vac_ID;
Vw_EmpVacations.Active:=true;

谢谢

1 个答案:

答案 0 :(得分:0)

尽管你的问题细节很少,但我会尽力帮助你; - )

请创建一个新的VCL表单应用程序 - Delphi For Win32项目。 然后,用以下代码替换项目源:

program SortTest;

uses
  Forms, ADODB, DB, DBGrids;

var
  qrySortTest: TADOQuery;
  conSQL2005: TADOConnection;
  dsSortTest: TDataSource;
  grdTest: TDBGrid;
  MainForm: TForm;
begin

  Application.Initialize;

  conSQL2005 := TADOConnection.Create(Application);
  with conSQL2005 do
  begin
    Name := 'conSQL2005';
    //Do not forget to change the connection string
    ConnectionString :=
      'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security In' +
      'fo=False;Data Source=WODZU-LAPTOP\SQL2005S';
    LoginPrompt := False;
    Provider := 'SQLOLEDB.1';
    Connected := True;
  end;

  qrySortTest := TADOQuery.Create(Application);
  with qrySortTest do
  begin
    Name := 'qrySortTest';
    Connection := conSQL2005;
    CursorType := ctStatic;
    SQL.Clear;
    SQL.Add('SELECT '#39'Kowalsky'#39' as Surname, 25 as Age');
    SQL.Add('UNION');
    SQL.Add('SELECT '#39'Smith'#39', 38');
    SQL.Add('UNION');
    SQL.Add('SELECT '#39'Jensen'#39', 11');
    SQL.Add('UNION');
    SQL.Add('SELECT '#39'Doe'#39', 26');
    SQL.Add('UNION');
    SQL.Add('SELECT '#39'Clarke'#39', 45');
    SQL.Add('ORDER BY AGE DESC');
    Active := True;
  end;

  dsSortTest := TDataSource.Create(Application);
  with dsSortTest do
  begin
    Name := 'dsSortTest';
    DataSet := qrySortTest;
  end;

  MainForm := TForm.Create(Application);
  MainForm.Position := poScreenCenter;

  grdTest := TDBGrid.Create(Application);

  with grdTest do
  begin
    Name := 'grdTest';
    Parent := MainForm;
    Left := 8;
    Top := 8;
    Width := 320;
    Height := 120;
    DataSource := dsSortTest;
    TabOrder := 0;
  end;


  MainForm.ShowModal;

  Application.Run;
end.

请记住将正确的连接字符串添加到SQL Server,否则将无效。 运行测试,它是否按降序显示按Age列排序的行?如果是,那么这就是你开始的地方。

将qrySortTest.SQL替换为您的无参数查询,并检查它是否有效。如果没有,那么问题在于您的查询而不是在组件设置中。

希望这有帮助。