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 ...
随机推荐
- 求LCM(a,b)=n的(a,b)的总对数(a<=b)
\(a={p_1} ^ {a_1} *{p_1} ^ {a_1} *..........*{p_n} ^ {a_n}\) \(b={p_1} ^ {b_1} *{p_1} ^ {b_1} *..... ...
- springboot之整合基本的jdbc并操作Mysql数据库
对于数据访问层,无论是SQL还是NOSQL,springboot默认采用整合spring data方式进行统一处理,添加大量自动配置,屏蔽了许多设置,引入各种xxxTemplate,xxxReposi ...
- K8S部署遇到的问题处理汇总
第一个: node节点注册提示:failed to get config map: Unauthorized 代码如下: [root@node1 ~]# kubeadm join --token ll ...
- 聊聊CMDB的前世今生
CMDB,Configuration Management DataBase,配置管理数据库,是与 IT 系统所有组件相关的信息库,它包含 IT 基础架构配置项的详细信息. 传统运维思路下的CMDB, ...
- 【动手学pytorch】softmax回归
一.什么是softmax? 有一个数组S,其元素为Si ,那么vi 的softmax值,就是该元素的指数与所有元素指数和的比值.具体公式表示为: softmax回归本质上也是一种对数据的估计 二.交叉 ...
- WeChall_PHP 0817 (PHP, Exploit)
I have written another include system for my dynamic webpages, but it seems to be vulnerable to LFI. ...
- 用pyinstaller打包时的图标问题
前言 因为昨天重新研究了下python的打包方法,今天一番准备把之前写的一个pdf合并软件重新整理一下,打包出来. 但在打包的过程中仍然遇到了一些问题,半年前一番做打包的时候也遇到了一些问题,现在来看 ...
- Go语言实现:【剑指offer】斐波那契数列
该题目来源于牛客网<剑指offer>专题. 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0) n<=39 Go语言实现: 递归: ...
- 【笔记】Git简明教程
前言 Git这个东西我曾经有学过,但学的内容太多了,有点懵,不太理解,磕磕碰碰的,走了不少弯路.不过最近我在B站上发现了一个讲的很好的教程:<表严肃讲Git>.因此,我决定用文字的方式分享 ...
- CodeForces 429B Working out DP
E - Working out Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...