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
精彩评论