你的位置:首页 > ASP.net教程

[ASP.net教程]C#的DataTable常用操作方法

1.将泛型集合类转换成DataTable(表中无数据时使用):

 public static DataTable NullListToDataTable(IList list)    {      var result = new DataTable();      if (list.Count <= 0) return result;      var propertys = list[0].GetType().GetProperties();      foreach (var pi in propertys)      {        if (pi != null)        {          result.Columns.Add(pi.Name, pi.PropertyType);        }      }      for (var i = 0; i < list.Count; i++)      {        var tempList = new ArrayList();        foreach (var pi in propertys)        {          var obj = pi.GetValue(list[i], null);          tempList.Add(obj);        }        var array = tempList.ToArray();        result.LoadDataRow(array, true);      }      return result;    }

2.将泛型集合类转换成DataTable(表中有数据时使用):

 public static DataTable NoNullListToDataTable<T>(IList<T> list)    {      var ds = new DataSet();      var dt = new DataTable(typeof(T).Name);      var myPropertyInfo =        typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);      foreach (var t in list)      {        if (t == null) continue;        var row = dt.NewRow();        for (int i = 0, j = myPropertyInfo.Length; i < j; i++)        {          var pi = myPropertyInfo[i];          var name = pi.Name;          if (dt.Columns[name] != null) continue;          DataColumn column;          if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")          {            column = new DataColumn(name, typeof(int));            dt.Columns.Add(column);            if (pi.GetValue(t, null) != null)              row[name] = pi.GetValue(t, null);            else              row[name] = DBNull.Value;          }          else          {            column = new DataColumn(name, pi.PropertyType);            dt.Columns.Add(column);            row[name] = pi.GetValue(t, null);          }        }        dt.Rows.Add(row);      }      ds.Tables.Add(dt);      return ds.Tables[0];    }

3.表中有数据或无数据时使用,可排除DATASET不支持System.Nullable错误:

 public static DataTable ToDataTable<T>(IList<T> list)    {      if (list == null || list.Count <= 0)      {        var result = new DataTable();        object temp;        if (list == null || list.Count <= 0) return result;        var propertys = list[0].GetType().GetProperties();        foreach (var pi in propertys)        {          {            result.Columns.Add(pi.Name, pi.PropertyType);          }        }        for (var i = 0; i < list.Count; i++)        {          var tempList = new ArrayList();          foreach (var pi in propertys)          {            var obj = pi.GetValue(list[i], null);            tempList.Add(obj);          }          var array = tempList.ToArray();          result.LoadDataRow(array, true);        }        return result;      }      var ds = new DataSet();      var dt = new DataTable(typeof(T).Name);      var myPropertyInfo =        typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);      foreach (var t in list)      {        if (t == null) continue;        var row = dt.NewRow();        for (int i = 0, j = myPropertyInfo.Length; i < j; i++)        {          var pi = myPropertyInfo[i];          var name = pi.Name;          if (dt.Columns[name] != null) continue;          DataColumn column;          if (pi.PropertyType.UnderlyingSystemType.ToString() == "System.Nullable`1[System.Int32]")          {            column = new DataColumn(name, typeof(int));            dt.Columns.Add(column);            if (pi.GetValue(t, null) != null)              row[name] = pi.GetValue(t, null);            else              row[name] = DBNull.Value;          }          else          {            column = new DataColumn(name, pi.PropertyType);            dt.Columns.Add(column);            row[name] = pi.GetValue(t, null);          }        }        dt.Rows.Add(row);      }      ds.Tables.Add(dt);      return ds.Tables[0];    }

4.合并相同的DataTable:

  public static DataTable MergeSameDatatable(DataTable dataTable1, DataTable dataTable2)    {      var newDataTable = dataTable1.Clone();      var obj = new object[newDataTable.Columns.Count];      for (var i = 0; i < dataTable1.Rows.Count; i++)      {        dataTable1.Rows[i].ItemArray.CopyTo(obj, 0);        newDataTable.Rows.Add(obj);      }      for (var i = 0; i < dataTable2.Rows.Count; i++)      {        dataTable2.Rows[i].ItemArray.CopyTo(obj, 0);        newDataTable.Rows.Add(obj);      }      return new DataTable();    }

5.将两个列不同的DataTable合并成一个新的DataTable :

 public static DataTable UniteDataTable(DataTable dt1, DataTable dt2, string dtName)    {      var dt3 = dt1.Clone();      for (var i = 0; i < dt2.Columns.Count; i++)      {        dt3.Columns.Add(dt2.Columns[i].ColumnName);      }      var obj = new object[dt3.Columns.Count];      for (int i = 0; i < dt1.Rows.Count; i++)      {        dt1.Rows[i].ItemArray.CopyTo(obj, 0);        dt3.Rows.Add(obj);      }      if (dt1.Rows.Count >= dt2.Rows.Count)      {        for (var i = 0; i < dt2.Rows.Count; i++)        {          for (var j = 0; j < dt2.Columns.Count; j++)          {            dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();          }        }      }      else      {        for (var i = 0; i < dt2.Rows.Count - dt1.Rows.Count; i++)        {          var dr3 = dt3.NewRow();          dt3.Rows.Add(dr3);        }        for (var i = 0; i < dt2.Rows.Count; i++)        {          for (var j = 0; j < dt2.Columns.Count; j++)          {            dt3.Rows[i][j + dt1.Columns.Count] = dt2.Rows[i][j].ToString();          }        }      }      dt3.TableName = dtName;      return dt3;    }

6.Datatable 转 List<Dictionary<string, object>:

 public static List<Dictionary<string, object>> DataTableToListDictory(DataTable table)    {      var ld = new List<Dictionary<string, object>>();      for (var i = 0; i < table.Rows.Count; i++)      {        var dic = new Dictionary<string, object>();        for (var j = 0; j < table.Columns.Count; j++)        {          dic.Add(table.Columns[j].ColumnName, table.Rows[i][j]);        }        ld.Add(dic);      }      return ld;    }

 


深圳旅游攻略必去景点深圳旅游攻略两天路线深圳旅游攻略自由行深圳旅游景点排行榜深圳旅游哪里好玩呢那琴半岛地质海洋公园水上乐园多少钱?台山海洋公园有哪些水上项目? 万达电影乐园与汉秀剧场 那琴半岛地质海洋公园团购多少钱?台山海洋公园另收费项目有什么? 玩转楚河汉街全攻略 逍遥谷怎么走?广东中山逍遥谷自驾攻略? 逍遥谷在哪里?广东中山逍遥谷地址? 从中山港怎么去逍遥谷?中山港到逍遥谷怎么走? 中山逍遥谷一日游?五桂山逍遥谷怎么走? 番禺沙湾古镇一天游攻略?广州番禺沙湾古镇门票预订? 沙湾古镇现在要门票吗?广州沙湾古镇最新价格? 肇庆到广宁竹海怎么走?肇庆到广宁竹海大观要多久? 丽江有什么特色美食?丽江有哪些好吃的? 博鳌亚洲论坛要门票不?博鳌亚洲论坛会址景区凭本地身份证半价吗? 2015腾讯全球合作伙伴大会在哪举办?腾讯全球合作伙伴大会时间? 文昌到椰子大观园如何乘车?文昌坐车到椰子大观园怎么走? 文昌到椰子大观园有直达车吗?文昌到椰子大观园旅游线路? Q65110A8981 Datasheet Q65110A8981 Datasheet Q65110A8982 Datasheet Q65110A8982 Datasheet Q65110A8986 Datasheet Q65110A8986 Datasheet 北京自助游攻略2013 北京自助游攻略2013 北京自助游攻略2013 珠海荷包岛好玩吗 珠海荷包岛好玩吗 珠海荷包岛好玩吗 粤晖园好玩吗 粤晖园好玩吗 粤晖园好玩吗