C#实现的Table的Merge,以及实现Table的Copy和Clone
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的更多相关文章
- 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 ...
- Truncate table、Delete与Drop table的区别
Truncate table.Delete与Drop table的区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNC ...
- table完美css样式,table的基本样式,table样式
table完美css样式,table的基本样式,table样式 >>>>>>>>>>>>>>>>> ...
- css实现鼠标移入table时出现滚动条且table内容不移位
一般是这样: 表格的标题和内容分别由一个table组成,其中表格内容的table由一个class="table-body"的div包裹.css如下 .tContainer .tab ...
- 14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE
14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE 回收操作系统磁盘空间当truncate 一个InnoDB ...
- 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 ...
- 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 ...
- 解决: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 ...
- 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 ...
随机推荐
- 双指针,BFS和图论(三)
(一)图论 1.大臣的旅费 很久以前,T王国空前繁荣. 为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任 ...
- Can you answer these queries III(线段树)
Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l ...
- 关于DockerFile组件,java8新特性的AWT组件,
前段时间由于公司业务开发了App产品,产品用CocosCreator打出来的App, 用的时候,发现获取验证码,获取不到值,折腾了几天,一开始欢迎安装的jdk有问题,但是几次验证不是jdk的问题, 最 ...
- linux下redis的部署
https://www.cnblogs.com/wangchunniu1314/p/6339416.html https://www.linuxidc.com/Linux/2017-09/146894 ...
- ios--->const 用法总结
const 用法总结 宏.变量.常量区分 宏:只是在预处理器里进行文本替换,没有类型,不做任何类型检查,编译器可以对相同的字符串进行优化.只保存一份到 .rodata 段.甚至有相同后缀的字符串也可以 ...
- Windows10 企业版激活
今天同大家分享一个Windows自带的激活方法(注:适用于win10 企业版 2019长期服务版,其他版本自行测试) 1.首先确保电脑网络通畅(不需要梯子) 2.以管理员方式运行cmd输入: slmg ...
- mybatis 源码分析中的知识点
1. resultMap 和 resultType 之间的优劣 resultMap: 在联合查询的时候, 可以不用写Join (因为在resultMap 的定义里面已经写了这些东西了<asso ...
- 大数据之Kafka史上最详细原理总结
Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实 ...
- JavaScript(4)---BOM详解
JavaScript(4)---BOM详解 之前写过一篇有关DOM的博客:JavaScript(2)---DOM详解 DOM有个顶级对象叫:document.同样BOM中也有顶级对象叫 window. ...
- 全卷积网络FCN
全卷积网络FCN fcn是深度学习用于图像分割的鼻祖.后续的很多网络结构都是在此基础上演进而来. 图像分割即像素级别的分类. 语义分割的基本框架: 前端fcn(以及在此基础上的segnet,decon ...