ado.net datatable top 功能!

    public static DataTable Top(DataTable dt, int count, string orderBy1)
    {
        dt = orderBy(dt, orderBy1);
        DataTable dtReturn = dt.Clone();

        for (int i = 0; i < count; i++)
        {
            DataRow dr = dtReturn.NewRow();
            for (int k = 0; k < dt.Columns.Count; k++)
            {
                dr[k] = dt.Rows[i][k].ToString();
            }
            dtReturn.Rows.Add(dr);
        }
        return dtReturn;

    }
    public static DataTable orderBy(DataTable dt, string orderBy)
    {
        DataView dv = dt.DefaultView;
        dv.Sort = orderBy;
        return dv.ToTable();
    }

调用

 ds.Tables.Add(Top(dt.Copy(),2900,"RQ"));

你可能感兴趣的:(Datatable)