【问题标题】:C#: retrieve the first n records from a databaseC#:从数据库中检索前 n 条记录
【发布时间】:2011-08-20 02:33:31
【问题描述】:

我在数据表中有 500 条记录, 从该记录中,我如何检索第 50 条记录?

【问题讨论】:

  • 你缺乏细节使这个问题变得很糟糕。
  • 您需要提供很多更多信息:您使用的是什么技术? Linq2SQL、EF、数据集、普通 SQL 命令?如果计划 SQL 命令:什么数据库?
  • 正如您在问题中所说的“来自数据库”,您使用的是哪种类型的数据库 Sql-server?甲骨文? MsAccess?
  • 然后,在从数据库中获取记录到数据集之前,使用“Top”关键字从数据库中只访问您想要的记录数。
  • 你说的是ADO.Net数据表还是数据库表??

标签: c# dataset


【解决方案1】:

如果你有 ADO.Net 数据表,你可以这样做

DataTable dtNew = dtOld.Clone();

for(int i=0; i<50; i++)
{
dtNew.ImportRow(dtOld.Rows[i]);
}

如果你想从数据库中查询,你可以这样做

Select Top 50 col1, col2 From Table Order By col1 //replace col1, col2 with your orignal database column names and Table with your orignal table name

【讨论】:

  • 我的荣幸 :) 但我想知道为什么我的答案左侧的向上和向下箭头之间有“0”。
【解决方案2】:
DataTable GetTopN(int n, DataTable content)
{
    DataTable dtNew = content.Clone();
    if (n > content.Rows.Count)
        n = content.Rows.Count;

    for(int i=0; i<n; i++)
    {
        dtNew.ImportRow(content.Rows[i]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多