C# 从sql数据库中选择最新的3条记录

C# 从sql数据库中选择最新的3条记录,c#,sql-server-2008,C#,Sql Server 2008,我的表名是tblEvent,它的列是EventID,name,Description,EventTypeID,TotalBudget,CustomerID,EventStatus,EventDate 我想选择最新的3条记录 我试过这个: public DataTable HomeEvents() { string query = "select TOP 3 tblEvent.*, tblCustomer.Name as 'CustomerName', tblCustomer.Photo

我的表名是
tblEvent
,它的列是
EventID
name
Description
EventTypeID
TotalBudget
CustomerID
EventStatus
EventDate

我想选择最新的3条记录

我试过这个:

public DataTable HomeEvents()
{
    string query = "select TOP 3 tblEvent.*, tblCustomer.Name as 'CustomerName', tblCustomer.Photo AS 'CustomerPhoto' from tblEvent ORDER BY EventID DESC, tblCustomer where tblEvent.CustomerID = tblCustomer.CustomerID";

    List<SqlParameter> lstParams = new List<SqlParameter>();
    DataTable dt = DBUtility.SelectData(query, lstParams);
    return dt;
}
公共数据表HomeEvents() { string query=“按事件ID描述从TBL订单中选择前三名TBL客户。*,TBL客户。名称为‘CustomerName’,TBL客户。照片为‘CustomerPhoto’,TBL客户,其中TBL客户。CustomerID=TBL客户。CustomerID”; List lstParams=新列表(); DataTable dt=DBUtility.SelectData(查询,lstParams); 返回dt; }
按排序的
子句应位于
where
子句之后:

select top 3
  tblEvent.*,
  tblCustomer.Name as 'CustomerName',
  tblCustomer.Photo AS 'CustomerPhoto'
from
  tblEvent
where
  tblEvent.CustomerID = tblCustomer.CustomerID
order by
  EventID desc,
  tblCustomer

注意:如果
EventID
是自动递增的(主键、标识),并且记录实际上是按照发生的顺序创建的,那么该字段将随着时间的推移以递增的顺序出现。否则,您需要使用
EventDate
字段进行排序(正如Tareq Alothman所建议的那样)。

将SQL语句更改为

... ORDER DESC BY ...

这将按相反的顺序排序,前三个现在是最后三个。

正如Guffa所说,顺序在where之后,您需要 “按事件日期描述订购”