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. c/python 的区别

    c             python                                                                                ...

  2. Python变量、方法、类的命名规则

    1. 变量命名总结: - 1.单下划线开头变量:protected - 2.双下划线开头变量:private - 3.双下划线开头,双下划线结尾:系统内置变量 2. 函数命名总结: - 1.私有方法: ...

  3. SSH(二)

    SSH框架整合的系统架构,Action.Service.Dao.SessionFactory.DataSource都可以作为Spring的Bean组件管理 使用HibernateDaoSupport基 ...

  4. 死磕mysql

    数据库创建语句 create database new; 创建一个名为new 的数据库 drop database new; 删除名为new的数据库 数据库名为小写,当初教我的那个人对我说在某个系统中 ...

  5. LIBCMTD.lib与libcpmtd冲突的解决方法。

    error: 1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int) ...

  6. usaco1.1

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

  7. Codeforces_462_B

    http://codeforces.com/problemset/problem/462/B 简单的贪心,排序即可看出来. #include<cstdio> #include<ios ...

  8. 【译文连载】 理解Istio服务网格(第二章 安装)

    全书目录 第一章 概述 本文目录 1.命令行工具安装 2. Kubernetes/OpenShift安装 3. Istio安装 4.示例Java微服务安装 4.1 源码概览 4.2 编译和部署cust ...

  9. Go语言实现:【剑指offer】求1+2+3+...+n

    该题目来源于牛客网<剑指offer>专题. 求1+2+3+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). Go ...

  10. Spring Cloud第十四篇 | Api网关Zuul

    ​ 本文是Spring Cloud专栏的第十四篇文章,了解前十三篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring C ...