开发者

C#: retrieve the first n records from a database

I have 500 records in the data table, From that records how can i retrieve 1st 50 records? 开发者_如何学编程


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]);
    }
}


If you have ADO.Net Datatable you can do

DataTable dtNew = dtOld.Clone();

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

If you want to query from the database, you can do

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜