C#实现的对两个Table进行Merge,两表必须存在至少一个公共栏位作为连接项,否则连接就失去了意义。如下是对两个table进行Merge的详细代码:

 private void button1_Click(object sender, EventArgs e)//Button点击触发事件
{
#region Table的Merge
DataTable dt = new DataTable(); DataTable dt1 = new DataTable();//创建Table1
dt1.Columns.Add("ID", typeof(string));
dt1.Columns.Add("NAME",typeof(string));
dt1.Columns.Add("AGE", typeof(int));
dt1.Columns.Add("SEX", typeof(string)); dt1.PrimaryKey = new DataColumn[] { dt1.Columns["ID"]};
for (int i = ; i < ; i++)
{
DataRow dr = dt1.NewRow();
dr["ID"] = "" + i.ToString();
dr["NAME"] = "00-" + i.ToString();
dr["AGE"] = + i;
dr["SEX"] = "M";
dt1.Rows.Add(dr); }
dt = dt1; DataTable dt2 = new DataTable();//创建Table2
dt2.Columns.Add("ID", typeof(string));
dt2.Columns.Add("NAME", typeof(string));
dt2.Columns.Add("Course",typeof(string));
dt2.Columns.Add("Score",typeof(int)); dt2.PrimaryKey = new DataColumn[] { dt2.Columns["ID"] };
for (int i = ; i < ; i++)
{
DataRow dr = dt2.NewRow();
dr["ID"] = "" + i.ToString();
dr["NAME"] = "00-" + i.ToString();
dr["Course"] ="C#";
dr["Score"] = i + ;
dt2.Rows.Add(dr);
}
dt = dt2;
dt1.Merge(dt2); //Copy
var table1 = dt1.Copy();//Copy,复制表的结构以及数据
//添加新的一行
DataRow dr1 = table1.NewRow();
dr1["ID"] = "";
dr1["NAME"] = "00-5";
dr1["AGE"] = ;
dr1["SEX"] = "F";
table1.Rows.InsertAt(dr1,);//表的指定位置插入新增加的一行 table1.Columns.Add(new DataColumn() { ColumnName = "Memo", DataType = typeof(string) });//默認插入到最後一列
table1.Columns["Memo"].SetOrdinal();//把插入的列移动到第一行 int memoIndex = table1.Columns.IndexOf("Memo");//获取列的位置信息
var isContainName = table1.Columns.Contains("NAME");//判断table中是否存在某列 List<string> columnsNameList = new List<string>();//遍历获取table的所有列名
foreach (DataColumn col in table1.Columns)
{
columnsNameList.Add(col.ColumnName);
} table1.Columns.RemoveAt(memoIndex);//通过列名的索引进行移除
table1.Columns.RemoveAt();//通过列名的索引进行移除 table1.Columns.Remove("SEX");//通过列名进行移除,建议使后者 string[] name = new string[table1.Rows.Count];//方法一:对表中的数据进行遍历输出
string[] id = new string[table1.Rows.Count];
for (int i = ; i < table1.Rows.Count; i++)
{
name[i] = table1.Rows[i]["NAME"].ToString();
id[i] = table1.Rows[i]["ID"].ToString();
} table1.Clear();//清空表中的数据 //Clone
var table2 = dt2.Clone();//Clone,复制表的结构、约束信息 #endregion }

C#实现的Table的Merge,以及实现Table的Copy和Clone的更多相关文章

  1. Nodes “-1” are listed in ADOP_VALID_NODES table but not in FND_NODES table

    While trying to apply patches to upgrade to 12.2.4, adop failed due to the below errors. Validating ...

  2. Truncate table、Delete与Drop table的区别

    Truncate table.Delete与Drop table的区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNC ...

  3. table完美css样式,table的基本样式,table样式

    table完美css样式,table的基本样式,table样式 >>>>>>>>>>>>>>>>> ...

  4. css实现鼠标移入table时出现滚动条且table内容不移位

    一般是这样: 表格的标题和内容分别由一个table组成,其中表格内容的table由一个class="table-body"的div包裹.css如下 .tContainer .tab ...

  5. 14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE

    14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE 回收操作系统磁盘空间当truncate 一个InnoDB ...

  6. ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.

    1.L_Monitoring有这么些字段,ID,Collecttime,PlateType,PlateNO以及其他一些这段.建立这个表的时候是个非分区表,其中ID是主键,并在Collecttime,P ...

  7. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -

    mysql -A不预读数据库信息(use dbname 更快)—Reading table information for completion of table and column names Y ...

  8. 解决:Reading table information for completion of table and column names

    mysql -A不预读数据库信息(use dbname 更快)—Reading table information for completion of table and column names Y ...

  9. Reading table information for completion of table and column names

    mysql> use ad_detail_page;Reading table information for completion of table and column namesYou c ...

随机推荐

  1. 【模板】普通平衡树(权值splay)

    安利splay讲解: [洛谷日报第62期]Splay简易教程 [模板]普通平衡树(luogu) Description 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下 ...

  2. libc.so.6修改链接指向后导致系统无法使用的原因及解决方法

    https://www.cnblogs.com/weijing24/p/5890031.html http://man.linuxde.net/ldconfig

  3. Docker(一) 简介

    简介 Docker是一款针对程序开发人员和系统管理员来开发.部署.运行应用的一款虚拟化平台.Docker 可以让你像使用集装箱一样快速的组合成应用,并且可以像运输标准集装箱一样,尽可能的屏蔽代码层面的 ...

  4. time 模块 和 random 模块常用方法讲解

    import timeprint(help(time))print(time.time())#时间戳 1573991312.5361328print(time.perf_counter())#计算CP ...

  5. React使用antd按需引入报错

    引言 按照antd官网配置按需引入,还是出现一系列的报错: 原因 在网上搜了一下,大部分说是react-scripts以及react-app-rewired版本不兼容的问题,我果断把下载低版本 npm ...

  6. 利用AppMetrics对Web进行监控教程

    利用AppMetrics对Web进行监控教程 一.基础准备 1. 安装依赖 这里可以通过nuget或使用命令行进行安装,具体需要安装的类库如下(注意版本): Install-Package App.M ...

  7. windows 安裝 gcc 編譯器 - MinGW

    MinGW 介紹 MinGW 全稱 Minimalist GNU For Windows,是個精簡的Windows平台C/C++.ADA及Fortran編譯器,相比Cygwin而言,體積要小很多,使用 ...

  8. usaco1.1

    Your Ride Is Here #include <iostream> #include <string> #include <vector> using na ...

  9. 尝试用 Python 写了个病毒传播模拟程序

    病毒扩散仿真程序,用 python 也可以. 概述 事情是这样的,B 站 UP 主 @ele 实验室,写了一个简单的疫情传播仿真程序,告诉大家在家待着的重要性,视频相信大家都看过了,并且 UP 主也放 ...

  10. The current test process

    样机测试 测试前: 工作内容: 1.需求分析.编写.评审: 项目开工会由项目负责人参加,参加会议时做好笔记,对项目的功能类似,功能模块,测试时间点有个大致的了解. 原始需求进行需求文档细化:按照模块进 ...