zoukankan      html  css  js  c++  java
  • ASP.NET中Dataset的table数据合并、数据截取、数据排序

    1、两个相同字段表的合并:

     1 public static DataSet CombineTables(DataSet _ds, DataTable _dt1, DataTable _dt2)
     2         {
     3             DataSet ds = _ds.Clone();
     4             //ds.Tables[0].Clone();
     5             ds.Tables[0].Rows.Clear();
     6             int i = 0;
     7             for ( i = 0; i < _dt1.Rows.Count; i++)
     8             {
     9                 ds.Tables[0].ImportRow(_dt1.Rows[i]);
    10             }
    11             for (int j = 0; j < _dt2.Rows.Count; j++)
    12             {
    13                 ds.Tables[0].ImportRow(_dt2.Rows[j]);
    14             }
    15             return ds;
    16         }
    View Code

    2、对表的数据截取:

    1 public static DataSet CopyScopeRows(DataSet source, int startRowNum, int endRowNum) {
    2             DataSet des = source.Clone();
    3             des.Tables[0].Rows.Clear();
    4             for (int i = startRowNum; i < source.Tables[0].Rows.Count && i <= endRowNum; i++) {
    5                 des.Tables[0].ImportRow(source.Tables[0].Rows[i]);
    6             }
    7             return des;
    8         }
    View Code

    3、数据行的筛选:

     1 public static DataSet CopyScopeRows(DataSet source, string filter) {
     2             DataSet des = source.Clone();
     3             DataSet _ds = null;
     4             des.Tables[0].Rows.Clear();
     5             try
     6                 {
     7             DataRow[] rows = source.Tables[0].Select(filter);
     8             foreach (DataRow row in rows) {
     9                 des.Tables[0].ImportRow(row);
    10             }}
    11             catch (Exception ex) { return _ds; }
    12             finally
    13                 {}
    14             return des;
    15         }
    View Code

    4、数据行的筛选,并可以排序:

     1 public static DataSet CopyScopeRows(DataSet source, string filter, string order) {
     2             DataSet des = source.Clone();
     3             des.Tables[0].Rows.Clear();
     4             DataRow[] rows = source.Tables[0].Select(filter, order);
     5             foreach (DataRow row in rows) 
     6             {
     7                 des.Tables[0].ImportRow(row);
     8             }
     9             return des;
    10         }
    View Code

    5、数据行的筛选,并可截取数据行:

     1 public static DataSet CopyScopeRows(DataSet source, string filter, int startRowNum, int endRowNum) {
     2             DataSet des = source.Clone();
     3             des.Tables[0].Rows.Clear();
     4             DataRow[] rows = source.Tables[0].Select(filter);
     5             for (int i = startRowNum; i < rows.Length && i <= endRowNum; i++)
     6             {
     7                 des.Tables[0].ImportRow(rows[i]);
     8             }
     9             return des;
    10         }
    View Code

    6、数据行的筛选、排序,并可截取数据行:

    1 public static DataSet CopyScopeRows(DataSet source, string filter, string order, int startRowNum, int endRowNum) {
    2             DataSet des = source.Clone();
    3             des.Tables[0].Rows.Clear();
    4             DataRow[] rows = source.Tables[0].Select(filter, order);
    5             for (int i = startRowNum; i < rows.Length && i <= endRowNum; i++) {
    6                 des.Tables[0].ImportRow(rows[i]);
    7             }
    8             return des;
    9         }
    View Code
    1 public static object GetValueByKey(DataTable table, string keyName, object keyValue, string returnName) {
    2             object obj = null;
    3             table.PrimaryKey = new DataColumn[] { table.Columns[keyName] };
    4             DataRow row = table.Rows.Find(keyValue);
    5             if (row != null) { obj = row[returnName]; }
    6             return obj;
    7         }
    View Code
     1 public static object GetValueByKeys(DataTable table,  object[] keyValue, string returnName)
     2         {
     3             object obj = null;
     4            
     5             DataColumn[] keys = new DataColumn[2];
     6             keys[1] = table.Columns[0];
     7             keys[0] = table.Columns[2];
     8             table.PrimaryKey = keys;
     9            
    10             DataRow row = table.Rows.Find(keyValue);
    11             if (row != null) { obj = row[returnName]; }
    12             return obj;
    13         }
    View Code
  • 相关阅读:
    Redis Hashes 巧用sort排序
    Redis 压缩存储的配置
    计算
    关于时间大小判断的坑和网上工具类的看法
    Mysql中字段类型之时间戳大坑2
    Mysql中字段类型之时间戳大坑
    Spring和springmvc父子容器注解扫描问题详解
    JXL导出Excel工具类
    Maven学习
    MySQL之账户管理
  • 原文地址:https://www.cnblogs.com/Iven-zhang/p/3250068.html
Copyright © 2011-2022 走看看