zoukankan      html  css  js  c++  java
  • 引用[C#]合併兩個DataTable (更新)

    引用http://www.dotblogs.com.tw/kennyshu/archive/2009/06/16/8853.aspx

    也許有人和我一樣有這樣的需求,所以分享給有需要的人。

    01 public static void AppendDataTable(DataTable hostDt, DataTable clientDt)
    02 {
    03   if (hostDt != null && hostDt.Rows.Count > 0)
    04   {
    05     DataRow dr;
    06
    07     for (int i = 0; i < clientDt.Columns.Count; i++)
    08     {
    09       hostDt.Columns.Add(new DataColumn(clientDt.Columns[i].ColumnName));
    10
    11       if (clientDt.Rows.Count > 0)
    12         for (int j = 0; j < clientDt.Rows.Count; j++)
    13         {
    14           dr = hostDt.Rows[j];
    15           dr[hostDt.Columns.Count - 1] = clientDt.Rows[j][i];
    16           dr = null;
    17         }

    18     }

    19   }

    20 }

    之所以沒有回傳是因為Reference Type Object原本就是passed by reference。然而,如果你的來源資料表(例如這邊的hostDt和clientDt)在呼叫過後會被disposed,那此方法就要改成如下:

    01 public static DataTable AppendDataTable(DataTable hostDt, DataTable clientDt)
    02 {
    03   if (hostDt != null && hostDt.Rows.Count > 0)
    04   {
    05     DataRow dr;
    06
    07     for (int i = 0; i < clientDt.Columns.Count; i++)
    08     {
    09       hostDt.Columns.Add(new DataColumn(clientDt.Columns[i].ColumnName));
    10
    11       if (clientDt.Rows.Count > 0)
    12         for (int j = 0; j < clientDt.Rows.Count; j++)
    13         {
    14           dr = hostDt.Rows[j];
    15           dr[hostDt.Columns.Count - 1] = clientDt.Rows[j][i];
    16           dr = null;
    17         }

    18     }

    19   }

    20
    21   return hostDt.Copy();
    22 }
  • 相关阅读:
    TransportClient基于Elasticsearch6.8.6 X-PACK
    elasticsearch6.8.6配置xpack(生成密钥)
    Java8 List排序
    ssh 免密码登录自动设置脚本
    Linux grep命令用于查找文件里符合条件的字符串
    [译]如何防止elasticsearch的脑裂问题
    APScheduler定时任务使用
    storm本地python开发环境搭建
    关于python反射的getattr,我终于想通了!
    利用sqlalchemy 查询视图
  • 原文地址:https://www.cnblogs.com/sandy_liao/p/2445621.html
Copyright © 2011-2022 走看看