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. 基于Flask框架搭建视频网站的学习日志(一)

    ------------恢复内容开始------------ 基于Flask框架搭建视频网站的学习日志(一)2020/02/01 一.Flask环境搭建 创建虚拟环境 初次搭建虚拟环境 搭建完虚拟环境 ...

  2. ReactNative---ref的用法和技巧

    1.获取上下文的组件 2. ref属性不仅接受string类型的参数,而且它还可以接受一个function 作为callback.如:将组件view作为参数赋值给this._view <View ...

  3. Navicat premium 12 for mac 无限试用

    Mac 终端输入 sudo rm -Rf ~/Library/Application\ Support/PremiumSoft\ CyberTech 重启电脑 成功

  4. ArrayAccess 接口(源码)

    The ArrayAccess interface (PHP 5 >= 5.0.0, PHP 7) Introduction Interface to provide accessing obj ...

  5. sublime sftp插件安装及时更新网站

    Sublime Text 2 本身并不强大,但是它方便使用插件扩展功能,所以变得很强大.今天介绍一个很实用的插件 SFTP ,可以大大提高前端工作效率. 常见的工作流程 有时候修改一些网站上的文件,通 ...

  6. linux下oracle调试小知识

    1.oracle 安装下的/u01/diag/rdbms/orcl/HOF/incident目录下是什么文件?答:每当一个错误发生的时候,oracle会创建一个incident,并且分配一个INCID ...

  7. python安装BeautifulSoup4

    今天学习使用python爬虫,书上说让安装一个BeautifulSoup的插件,网上找了,说是下载压缩文件,直接解压然后,通过两行代码就实现了,我下载了两个,还是无法使用,后来就使用在在Pycharm ...

  8. GDI+ Hello World

    学WTL/MFC,学不会了去看WIN32. 学C/C++,学不会了去看汇编. 使用VS,不明白了去用cl.exe/link.exe 控制台下输出图片的分辨率: #include <windows ...

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

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

  10. Exchange2010安装指南

    安装流程参考下面的: https://jingyan.baidu.com/article/2c8c281d6893680008252a9c.html 安装环境:windows server 2008( ...